Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 18198 | Accepted: 5969 |
Description
Input
Output
Sample Input
2 2 2 1 2 13 2 1 33 4 6 1 2 10 2 1 60 1 3 20 3 4 10 2 4 5 4 1 50
Sample Output
46 210
Source
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdbool.h> 4 #include<limits.h> 5 int i,j,n,m,p,tot, 6 q[2100000],toit[1100000],list[1100000],next[1100000],cost[1100000],dist[1100000], 7 ai[1100000],bi[1100000],ci[1100000]; 8 double sum; 9 bool can[1100000]; 10 11 int 12 pre(void) 13 { 14 memset(q,0,sizeof(q)); 15 memset(toit,0,sizeof(toit)); 16 memset(next,0,sizeof(next)); 17 memset(list,0,sizeof(list)); 18 memset(cost,0,sizeof(cost)); 19 memset(can,true,sizeof(can)); 20 memset(ai,0,sizeof(ai)); 21 memset(bi,0,sizeof(bi)); 22 memset(ci,0,sizeof(ci)); 23 sum=0;tot=0; 24 return 0; 25 } 26 27 int 28 add(int x,int y,int z) 29 { 30 tot++; 31 cost[tot]=z; 32 next[tot]=list[x]; 33 list[x]=tot; 34 toit[tot]=y; 35 36 ai[tot]=y; bi[tot]=x; ci[tot]=z; 37 return 0; 38 } 39 40 void 41 spfa(int s) 42 { 43 int i,j,head,tail,v,k; 44 q[1]=s; 45 head=0;tail=1; 46 for(i=1;i<=n;i++) 47 dist[i]=INT_MAX >> 1; 48 dist[s]=0; 49 can[s]=false; 50 51 while(head!=tail) 52 { 53 head=head%2000000+1; 54 v=q[head]; 55 k=list[v]; 56 57 while(k!=0) 58 { 59 if((dist[v]+cost[k])<dist[toit[k]]) 60 { 61 dist[toit[k]]=dist[v]+cost[k]; 62 if (can[toit[k]]) 63 { 64 tail=tail%2000000+1; 65 q[tail]=toit[k]; 66 can[toit[k]]=false; 67 } 68 69 } 70 k=next[k]; 71 } 72 can[v]=true; 73 } 74 75 } 76 77 78 int 79 main() 80 { 81 int x,y,z,ca; 82 scanf("%d\n",&p); 83 for(ca=1;ca<=p;ca++) 84 { 85 pre(); 86 scanf("%d%d\n",&n,&m); 87 for(i=1;i<=m;i++) 88 { 89 scanf("%d%d%d",&x,&y,&z); 90 add(x,y,z); 91 } 92 spfa(1); 93 94 for(i=1;i<=n;i++) 95 sum+=dist[i]; 96 97 memset(q,0,sizeof(q)); 98 memset(toit,0,sizeof(toit)); 99 memset(next,0,sizeof(next)); 100 memset(list,0,sizeof(list)); 101 memset(cost,0,sizeof(cost)); 102 memset(can,true,sizeof(can)); 103 tot=0; 104 105 for(i=1;i<=m;i++) 106 add(ai[i],bi[i],ci[i]); 107 108 spfa(1); 109 for(i=1;i<=n;i++) 110 sum+=dist[i]; 111 112 printf("%.f\n",sum); 113 } 114 return 0; 115 } 116 117 118 119