Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
感觉自己这题已经写的很好了,不知道为什么总是wa,求大神指导,代码如下#include<iostream> #include<stdio.h> #include<queue> #include<string.h> #include<algorithm> #include<vector> #include<stack> using namespace std; vector<int> s1[100010],s2[100010]; queue<int> q; stack<int> p,r; int dp[100010],vis[100010],s[100010]; int main() { int n,m,i,j,k,a,b,c,t; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) dp[i]=100010; memset(vis,0,sizeof(vis)); for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); if(a==b) continue; s1[a].push_back(b); s2[a].push_back(c); s1[b].push_back(a); s2[b].push_back(c); } vis[n]=1; dp[n]=0; while(!q.empty()) q.pop(); for(i=0;i<s1[n].size();i++) { j=s1[n][i]; if(vis[j]==0) { dp[j]=1; vis[j]=1; q.push(j); } } while(!q.empty()) { k=q.front(); q.pop(); for(i=0;i<s1[k].size();i++) { j=s1[k][i]; if(vis[j]==0) { dp[j]=dp[k]+1; vis[j]=1; q.push(j); } } } printf("%d\n",dp[1]); while(!q.empty()) { q.pop(); } memset(vis,0,sizeof(vis)); q.push(1); vis[1]=1; t=dp[1]; for(i=1;i<=t;i++) s[i]=1000000000; while(!p.empty()) p.pop(); while(!r.empty()) r.pop(); while(!q.empty()) { if(t==0) break; k=q.front(); q.pop(); for(i=0;i<s1[k].size();i++) { a=s1[k][i]; b=s2[k][i]; if(b<s[t]&&dp[a]==t-1&&vis[a]==0) s[t]=b; } for(i=0;i<s1[k].size();i++) { a=s1[k][i]; b=s2[k][i]; if(b==s[t]&&dp[a]==t-1&&vis[a]==0) { vis[a]=1; p.push(a); r.push(s[t]); } } if(q.empty()) { if(!p.empty()&&!r.empty()) { c=p.top(); p.pop(); j=r.top(); r.pop(); q.push(c); } while(!p.empty()&&!r.empty()) { a=p.top(); b=r.top(); p.pop(); r.pop(); if(b==j) q.push(a); } t--; } } t=dp[1]; for(i=t;i>1;i--) printf("%d ",s[i]); printf("%d\n",s[1]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator