标签:des style blog http io color ar os sp
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int next[20001]={0},point[10001]={0},en[20001]={0},va[20001]={0},que[2010]={0},num[10001]={0}; long long dis[1001]={0}; bool visit[10001]={false}; int main() { int n,m,i,j,tot,head,tail,g1,g2,c1,c2,x,y; long long maxn; bool ff; maxn=2100000000; while(true) { tot=0; memset(que,0,sizeof(que)); memset(next,0,sizeof(next)); memset(point,0,sizeof(point)); memset(en,0,sizeof(en)); memset(va,0,sizeof(va)); memset(num,0,sizeof(num)); memset(visit,false,sizeof(visit)); ff=false; scanf("%d%d",&n,&m); if (n==0&&m==0) break; if (m==0&&n==1) { printf("0\n"); continue; } if (m==0) { printf("No such path\n"); continue; } for (i=1;i<=m;++i) { scanf("%d%d%d%d",&g1,&g2,&c1,&c2); ++tot; next[tot]=point[g1]; point[g1]=tot; en[tot]=g2; va[tot]=c1; ++tot; next[tot]=point[g2]; point[g2]=tot; en[tot]=g1; va[tot]=c2; } head=0;tail=1;que[tail]=1; for (i=0;i<=n;++i) dis[i]=maxn; dis[1]=0; visit[1]=true; num[1]++; do { ++head; head=(head-1)%2000+1; x=que[head]; visit[x]=false; y=point[x]; while(y!=0) { if (dis[en[y]]>dis[x]+va[y]) { dis[en[y]]=dis[x]+va[y]; if (!visit[en[y]]) { ++tail; tail=(tail-1)%2000+1; que[tail]=en[y]; ++num[en[y]]; visit[en[y]]=true; if (num[en[y]]>n) { ff=true; break; } } } y=next[y]; } if (ff) break; }while(head!=tail); if (ff) printf("No such path\n"); else { if (dis[n]!=maxn) printf("%lld\n",dis[n]); else printf("No such path\n"); } } }
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/Rivendell/p/4080761.html