标签:
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
#include <iostream> #include<cstdio> #include<vector> #include<cstring> #include<queue> using namespace std; bool vis[202]; struct node { int num,d; node(int a,int b){num=a; d=b;} }; vector<node> s[202]; int dis[202]; int i,j,n,u,v,m; const int inf=0x7fffffff; void spfa() { int i,j; for(i=0;i<n;i++)dis[i]=inf; memset(vis,0,sizeof(vis)); vis[u]=1; dis[u]=0; queue<int>Q; Q.push(u); while(!Q.empty()) { int p=Q.front(); Q.pop(); vis[p]=0; for(i=0;i<s[p].size();i++) { if(dis[s[p][i].num]<=dis[p]+s[p][i].d) continue; dis[s[p][i].num]=dis[p]+s[p][i].d; if(!vis[s[p][i].num]) { Q.push(s[p][i].num); vis[s[p][i].num]=1; } } } return; } int main() { while(~scanf("%d%d",&n,&m)) { for(i=0;i<n;i++)s[i].clear(); for(i=1;i<=m;i++) { int x,y,d; scanf("%d%d%d",&x,&y,&d); s[x].push_back(node(y,d)); s[y].push_back(node(x,d)); } scanf("%d%d",&u,&v); spfa(); if(dis[v]==inf) printf("-1\n"); else printf("%d\n",dis[v]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5723537.html