Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 55515 | Accepted: 18850 |
Description
Input
Output
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
Source
#include
#include
#include
#include
#include
using namespace std;
#define MAX 4004
const int inf = 1000000000;
int p[MAX];
int nEdge;
bool vis[MAX];
int dis[MAX];
struct edge{
int v,w,pre;
}e[MAX];
void addEdge(int u,int v,int w)
{
nEdge++;
e[nEdge].v = v;
e[nEdge].w = w;
e[nEdge].pre = p[u];
p[u] = nEdge;
}
struct node{
int u,dis;
node(int x,int y){u = x,dis = y;}
friend bool operator< ( node a,node b)
{
return a.dis>b.dis;
}
};
void dijkstra(int st,int en)
{
int i,j;
for(i = 0;i <= st;i++)
{
dis[i] = inf;
vis[i] = false;
}
dis[st] = 0;
priority_queue qu;
qu.push(node(st,0));
while(!qu.empty())
{
int u = qu.top().u;
vis[u] = true;
qu.pop();
for(j = p[u];j;j = e[j].pre)
{
int v = e[j].v;
int w = e[j].w;
if(!vis[v] && dis[u] + w < dis[v])
{
dis[v] = dis[u] + w;
qu.push(node(v,dis[v]));
}
}
}
printf("%d\n",dis[en]);
}
int main()
{
int t,n;
int a,b,c;
while(~scanf("%d%d",&t,&n))
{
nEdge = 0;
memset(p,0,sizeof(p));
for(int i = 0;i < t;i++)
{
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c);
addEdge(b,a,c);
}
dijkstra(n,1);
}
return 0;
}