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 AC过,刚好1500ms一个代码刷了三次第一次1500ms,第二次1454ms,第三次1469ms #include <iostream> #define Vex_Size 30005 using namespace std; struct Edge { long e,c; Edge *next; }; Edge edge[Vex_Size]; bool visited[Vex_Size]; long d[Vex_Size],size[Vex_Size]; long SPFA(long n) { long tq; Edge *te; long stack[30005],top=0; memset(visited,0,sizeof(visited)); memset(d,0x3f,sizeof(d)); d[1]=0; stack[top++]=1,visited[1]=1; while(top!=0) { tq=stack[--top]; te=edge[tq].next; while(te) { if(d[tq]+te->c<d[te->e]) { d[te->e]=d[tq]+te->c; if(visited[te->e]==0) { stack[top++]=te->e; visited[te->e]=1; } } te=te->next; } visited[tq]=0; } return d[n]; } int main() { long n,m,i,s,e,c; Edge *te; //freopen("in.txt","r",stdin); scanf("%ld%ld",&n,&m); for(i=1;i<=n;i++) edge[i].next=NULL; for(i=0;i<m;i++) { scanf("%ld%ld%ld",&s,&e,&c); te=new Edge; te->e=e,te->c=c; //初值 te->next=edge[s].next; edge[s].next=te; } printf("%ld\n",SPFA(n)); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator