标签:
Description
In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.
Output
For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.
Sample Input
2
Sample Output
46
Source
Central Europe 1998
题目大意:给你N个结点和M条边。问:从结点1出发分别到2、3、4、…、N结点的路程以及
从这些结点返回结点1的总路程和。
思路:求源点到各结点的距离问题,用Bellman-Ford时间复杂度为O(N*M),这道题的数据规
模是1000000,所以果断不行。我用了链式前向星(类似邻接表)存储图,因为要分别求出去时的
最短总路程和回来时的最短总路程,所以用了二维的链式前向星,一个存正边,一个存反边,然
后以结点1为起点,对正边做一次SPFA算法,再以结点1为起点,对反边做一次SPFA算法。
看讨论区貌似有人用getchar(),putchar()可以达到625ms,貌似是输入输出外挂,没有试过。。。
改天试一下
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; const int MAXM = 1000100; const int MAXN = 1000100; struct EdgeNode { int to; int w; int next; }Edges[2][MAXM]; int Head[2][MAXN],vis[MAXN],queue[MAXN],outque[MAXN]; __int64 Dist[MAXN]; bool SPFA(int S,int N,int flag) { for(int i = 2; i <= N; ++i) Dist[i] = 0xffffffff; memset(vis,0,sizeof(vis)); memset(outque,0,sizeof(outque)); int iq = 0; queue[iq++] = S; vis[S] = 1; Dist[S] = 0; int i = 0,top,k; while(i != iq) { top = queue[i]; vis[top] = 0; outque[top]++; if(outque[top] > N) return false; k = Head[flag][top]; while(k >= 0) { if(Dist[Edges[flag][k].to] - Edges[flag][k].w > Dist[top]) { Dist[Edges[flag][k].to] = Dist[top] + Edges[flag][k].w; if( !vis[Edges[flag][k].to]) { vis[Edges[flag][k].to] = 1; queue[iq++] = Edges[flag][k].to; } } k = Edges[flag][k].next; } i++; } return true; } int main() { int T,N,M,u,v,w; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&M); memset(Head,-1,sizeof(Head)); for(int i = 0; i < M; ++i) { scanf("%d%d%d", &u,&v,&w); Edges[0][i].to = v; Edges[0][i].w = w; Edges[0][i].next = Head[0][u]; Head[0][u] = i; Edges[1][i].to = u; Edges[1][i].w = w; Edges[1][i].next = Head[1][v]; Head[1][v] = i; } __int64 ans = 0; SPFA(1,N,0); for(int i = 1; i <= N; ++i) if(Dist[i] != 0xffffffff) ans += Dist[i]; SPFA(1,N,1); for(int i = 1; i <= N; ++i) if(Dist[i] != 0xffffffff) ans += Dist[i]; printf("%I64d\n",ans); } return 0; }
POJ1511 Invitation Cards【SPFA】
标签:
原文地址:http://blog.csdn.net/lianai911/article/details/43112769