首页 > 代码库 > POJ1511 Invitation Cards【SPFA】
POJ1511 Invitation Cards【SPFA】
Invitation Cards
Time Limit: 8000MS Memory Limit: 262144K
Total Submissions: 20229Accepted: 6612
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.The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where ‘X‘ denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.
All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.
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
22 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
46210
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】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。