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 |
再来个,SPFA,79MS,为毛还慢些?In Reply To:Bellman_ford,32MS,优化中…… Posted by:speedcell4 at 2011-08-21 00:50:07 #include<iostream> #include<queue> using namespace std; #define SIZE 10000 #define INF ((1<<31)-1) struct edge { int u; int v; int w; }a[SIZE]; bool v[SIZE]; int dist[SIZE]; queue<int> node; int t,n; int x,y,z,Ne=0; void Init(int s0) { memset(v,false,sizeof(v)); for(int i=0;n-i>0;i++) dist[i]=INF; dist[s0]=0; v[s0]=true; node.push(s0); } int SPFA(int s0) { Init(s0); while(!node.empty()) { int now=node.front();node.pop();v[now]=false; for(int i=0;Ne-i>0;i++) { if(a[i].u==now&&dist[a[i].v]>dist[a[i].u]+a[i].w) { dist[a[i].v]=dist[a[i].u]+a[i].w; if(v[a[i].v]==false) { v[a[i].v]=true; node.push(a[i].v); } } } } return dist[n-1]; } int main() { scanf("%d %d",&t,&n); for(int i=0;t-i>0;i++) { scanf("%d %d %d",&x,&y,&z); a[Ne].u=x-1; a[Ne].v=y-1; a[Ne++].w=z; a[Ne].u=y-1; a[Ne].v=x-1; a[Ne++].w=z; } printf("%d\n",SPFA(0)); //system("pause"); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator