Til the Cows Come Home
思路
dijkstra 模板题 不作解释 .
Code
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
bool st[N];
int dist[N];
int g[N][N];
int n,m;
int dijkstra(){
memset(dist,0x3f,sizeof dist);
dist[n]=0;
for(int i=0;i<n;i++){
int t=-1;
for(int j=1;j<=n;j++)
if(!st[j]&&(t==-1||dist[j]<dist[t])) t=j;
st[t]=true;
for(int j=1;j<=n;j++)
dist[j]=min(dist[j],dist[t]+g[t][j]);
}
return dist[1];
}
int main(){
cin>>m>>n;
memset(g,0x3f,sizeof g);
while(m--){
int a,b,w;
cin>>a>>b>>w;
g[a][b]=g[b][a]=min(w,g[a][b]);
}
cout<<dijkstra()<<endl;
return 0;
}