标签:des style blog color os java io strong for
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2听ysj说spfa秒杀一切。。#include <cstdio> #include <iostream> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int INF=1<<27; const int maxn=100010; int n,m,dis[1100]; vector< pair<int,int> > eg[maxn]; bool inq[1100]; void spfa(int src) { queue <int> Q; for(int i=0;i<=n;i++){dis[i]=INF;inq[i]=0;} dis[src]=0; Q.push(src); while(!Q.empty()) { int u=Q.front();Q.pop(); inq[u]=0; for(int i=0;i<eg[u].size();i++) { int v=eg[u][i].first; int w=eg[u][i].second; if(dis[v]>dis[u]+w) { dis[v]=dis[u]+w; if(!inq[v]) { inq[v]=1; Q.push(v); } } } } } int main() { while(~scanf("%d%d",&n,&m)) { if(!n&&!m)break; for(int i=0;i<=n;i++) eg[i].clear(); while(m--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); eg[u].push_back(make_pair<int,int>(v,w)); eg[v].push_back(make_pair<int,int>(u,w)); } spfa(1); printf("%d\n",dis[n]); } return 0; }
标签:des style blog color os java io strong for
原文地址:http://blog.csdn.net/qq_16255321/article/details/38848091