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了一次,没注意输出no answer;tle一次,因为spfa用了栈,改用队列610ms水过。代码如下#include<cstdio> #include<cstring> using namespace std; const long long inf=9999999999; int first[50001],nxt[100001],edge[100001],val[100001],w[50001],e,v,num; long long dis[50001]; bool vis[50001]; void add(int a,int b,int c) { edge[++num]=b; val[num]=c; nxt[num]=first[a]; first[a]=num; } void spfa() { int q[50000],head,tail,i,j; for(i=1;i<=v;i++) dis[i]=inf,vis[i]=0; q[1]=1; head=1; tail=2; dis[1]=0; vis[1]=1; while(head!=tail) { i=q[head]; head=(head+1)%50000; j=first[i]; vis[i]=0; while(j) { if(dis[edge[j]]>dis[i]+val[j]) { dis[edge[j]]=dis[i]+val[j]; if(!vis[edge[j]]) { q[tail]=edge[j]; tail=(tail+1)%50000; vis[edge[j]]=1; } } j=nxt[j]; } } } int main() { int i,t,a,b,c; long long ans; bool f; scanf("%d",&t); while(t--) { scanf("%d%d",&v,&e); for(i=1;i<=v;i++) scanf("%d",&w[i]); memset(first,0,sizeof(first)); memset(nxt,0,sizeof(nxt)); num=0; for(i=1;i<=e;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,c); } spfa(); ans=0; f=0; for(i=2;i<=v;i++) if (dis[i]<inf) ans+=dis[i]*w[i]; else { f=1; break; } if(f) printf("No Answer\n"); else printf("%lld\n",ans); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator