- //Dijkstra
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- using namespace std;
-
- #define N 1005
- #define T 2005
- #define INF 0x3f3f3f3f
- int t,n;
- int vis[N];
- int g[N][N];
- int d[N];
-
- int dijkstra(int u){
- memset(vis,0,sizeof(vis));
- memset(d,0x3f,sizeof(d));
- d[u]=0;
-
- for (int i=0;i<n;i++){
- int sum=INF,k;
- for (int j=1;j<=n;j++)
- if (!vis[j] && d[j]<sum)
- sum=d[k=j];
- vis[k]=1;
- //cout<<k<<endl;
- for (int j=1;j<=n;j++)
- if (!vis[j] && d[j]>d[k]+g[k][j])
- d[j]=d[k]+g[k][j];
- }
-
- return d[1];
- }
-
- int main(){
- while (~scanf(“%d%d”,&t,&n)){
- memset(g,0x3f,sizeof(g));
- for (int i=0;i<t;i++){
- int u,v,d;
- scanf(“%d%d%d”,&u,&v,&d);
- if (g[u][v]>d)
- g[u][v]=g[v][u]=d;
- }
-
- printf(“%d\n”,dijkstra(n));
- }
-
- return 0;
- }