求1到N的最短路
注意有重边 跑一遍dijkstra就行
/* ***********************************************
Author :Sun Yuefeng
Created Time :2016/10/22 14:18:06
File Name :A.cpp
************************************************ */ #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<queue>
#include<list>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e3+;
const int mod=1e7+;
int dx[]= {,,,-,,-,,-};
int dy[]= {,-,,,-,,,-}; int n,m;
int way[maxn][maxn];
int dis[maxn];
bool vis[maxn]; void dijkstra(int x){
for(int i=;i<=m;i++){
dis[i]=inf;
vis[i]=false;
}
dis[x]=;
for(int i=;i<=m;i++){
int k=-;
int min=inf;
for(int j=;j<=m;j++){
if(!vis[j]&&dis[j]<min){
min=dis[j];
k=j;
}
}
if(k==-) break;
vis[k]=true;
for(int j=;j<=m;j++){
if(!vis[j]&&dis[j]>dis[k]+way[j][k]){
dis[j]=dis[k]+way[j][k];
}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d%d",&n,&m)){
int a,b,v;
M(way,inf);
for(int i=;i<n;i++){
scanf("%d%d%d",&a,&b,&v);
if(way[a][b]>v){
way[a][b]=v;
way[b][a]=v;
}
}
dijkstra();
int min=inf;
printf("%d\n",dis[m]);
}
return ;
}