UVA 10986 Sending email Solution
Solution in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include <bits/stdc++.h>
using namespace std;
#define infinity 1<<30 //2^30
int k=0;
struct node
{
int u;
int cost;
node(int _u, int _cost)
{
u = _u;
cost = _cost;
}
bool operator < ( const node& p ) const
{
return cost > p.cost;
}
};
void dijstkra(int n, vector<int>g[], vector<int>cost[], int source,int t)
{
k++;
int distance[n+1];
for(int i=0; i<=n; i++)
{
distance[i] = infinity;
}
priority_queue<node>q;
q.push(node(source,0));
distance[source] = 0;
while(!q.empty())
{
node top = q.top();
q.pop();
int u=top.u;
for(int i=0; i<(int)g[u].size(); i++)
{
int v=g[u][i];
if(distance[u] + cost[u][i] < distance[v])
{
distance[v] = distance[u] + cost[u][i];
q.push(node(v, distance[v]));
}
}
}
if(distance[t]==infinity)
printf("Case #%d: unreachable\n",k);
else
{
printf("Case #%d: %d\n",k,distance[t]);
}
}
int main()
{
vector<int>g[50009], cost[50009];
int numNodes, numEdges,source,t,test;
cin>>test;
while(test--)
{
cin>>numNodes>>numEdges>>source>>t;
for(int i=0; i<numEdges; i++)
{
int u, v, w;
cin>>u>>v>>w;
g[u].push_back(v);
g[v].push_back(u);
cost[u].push_back(w);
cost[v].push_back(w);
}
dijstkra(numNodes, g, cost, source,t);
for(int l =0; l<50009; l++)
{
g[l].clear();
}
for(int m =0; m<50009; m++)
{
cost[m].clear();
}
}
return 0;
}
No comments