标签:des style class code java tar
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 <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn=220; struct Edge { int to,w,next; }edge[maxn*maxn]; int Adj[maxn],Size; void init() { Size=0; memset(Adj,-1,sizeof(Adj)); } void Add_Edge(int u,int v,int w) { edge[Size].to=v; edge[Size].w=w; edge[Size].next=Adj[u]; Adj[u]=Size++; } int n,m; int dist[maxn],inq[maxn],cq[maxn]; bool spfa(int src) { memset(dist,63,sizeof(dist)); memset(inq,0,sizeof(inq)); memset(cq,0,sizeof(cq)); queue<int> q; dist[src]=0,inq[src]=1;cq[src]=1; q.push(src); while(!q.empty()) { int u=q.front(),v; q.pop(); for(int i=Adj[u];~i;i=edge[i].next) { v=edge[i].to; if(dist[v]>dist[u]+edge[i].w) { dist[v]=dist[u]+edge[i].w; if(!inq[v]) { inq[v]=1; cq[v]++; if(cq[v]>=n) return false; q.push(v); } } } inq[u]=0; } return true; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { init(); int a,b,c; for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); Add_Edge(a,b,c); Add_Edge(b,a,c); } int u,v; scanf("%d%d",&u,&v); if(spfa(u)==false) { puts("-1"); } else { if(dist[v]==0x3f3f3f3f) dist[v]=-1; printf("%d\n",dist[v]); } } return 0; }
HDOJ 1874 畅通工程续,布布扣,bubuko.com
标签:des style class code java tar
原文地址:http://blog.csdn.net/ck_boss/article/details/25341611