[POJ] 1511 Invitation Cards
2014-03-21 21:59
295 查看
[b]Invitation Cards[/b]
[b]Description[/b]
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.
[b]Input[/b]
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.
[b]Sample Input[/b]
[b]Sample Output[/b]
Source
Central Europe 1998
[b]题目地址:[/b]http://poj.org/problem?id=1511
[b]题解:由题目可知,spfa即可解决。根据spfa的特点,从源点1作spfa以后,各点的dist都是最短路径长,求和。然后求其他n-1个点到1的最短路。根据数据规模,如果作n-1次spfa肯定难以承受,单向道路反向建图,再从源点1作spfa,各点dist再求和,与原图n-1个点到1的dist之和等价。两次和相加即为所求。[/b]
[b] 要注意和sum的范围。int sum的话结果会越界导致WA。[/b]
[b]代码:[/b]
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 18198 | Accepted: 5969 |
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.
[b]Input[/b]
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.
[b]Sample Input[/b]
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
[b]Sample Output[/b]
46 210
Source
Central Europe 1998
[b]题目地址:[/b]http://poj.org/problem?id=1511
[b]题解:由题目可知,spfa即可解决。根据spfa的特点,从源点1作spfa以后,各点的dist都是最短路径长,求和。然后求其他n-1个点到1的最短路。根据数据规模,如果作n-1次spfa肯定难以承受,单向道路反向建图,再从源点1作spfa,各点dist再求和,与原图n-1个点到1的dist之和等价。两次和相加即为所求。[/b]
[b] 要注意和sum的范围。int sum的话结果会越界导致WA。[/b]
[b]代码:[/b]
#include<stdio.h> #include<string.h> #include<stdbool.h> #include<limits.h> int i,j,n,m,p,tot, q[2100000],toit[1100000],list[1100000],next[1100000],cost[1100000],dist[1100000], ai[1100000],bi[1100000],ci[1100000]; double sum; bool can[1100000]; int pre(void) { memset(q,0,sizeof(q)); memset(toit,0,sizeof(toit)); memset(next,0,sizeof(next)); memset(list,0,sizeof(list)); memset(cost,0,sizeof(cost)); memset(can,true,sizeof(can)); memset(ai,0,sizeof(ai)); memset(bi,0,sizeof(bi)); memset(ci,0,sizeof(ci)); sum=0;tot=0; return 0; } int add(int x,int y,int z) { tot++; cost[tot]=z; next[tot]=list[x]; list[x]=tot; toit[tot]=y; ai[tot]=y; bi[tot]=x; ci[tot]=z; return 0; } void spfa(int s) { int i,j,head,tail,v,k; q[1]=s; head=0;tail=1; for(i=1;i<=n;i++) dist[i]=INT_MAX >> 1; dist[s]=0; can[s]=false; while(head!=tail) { head=head%2000000+1; v=q[head]; k=list[v]; while(k!=0) { if((dist[v]+cost[k])<dist[toit[k]]) { dist[toit[k]]=dist[v]+cost[k]; if (can[toit[k]]) { tail=tail%2000000+1; q[tail]=toit[k]; can[toit[k]]=false; } } k=next[k]; } can[v]=true; } } int main() { int x,y,z,ca; scanf("%d\n",&p); for(ca=1;ca<=p;ca++) { pre(); scanf("%d%d\n",&n,&m); for(i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&z); add(x,y,z); } spfa(1); for(i=1;i<=n;i++) sum+=dist[i]; memset(q,0,sizeof(q)); memset(toit,0,sizeof(toit)); memset(next,0,sizeof(next)); memset(list,0,sizeof(list)); memset(cost,0,sizeof(cost)); memset(can,true,sizeof(can)); tot=0; for(i=1;i<=m;i++) add(ai[i],bi[i],ci[i]); spfa(1); for(i=1;i<=n;i++) sum+=dist[i]; printf("%.f\n",sum); } return 0; }
相关文章推荐
- (POJ 1511)Invitation Cards 有向图来回最短路 + SPFA (复习必看题)
- poj 1511 Invitation Cards spfa比基础题难一些!!练练手挺好
- POJ 1511 【heap+dij】
- POJ-1511(Invitation Cards )——spfa+邻接表
- POJ-1511 Invitation Cards(最短路)
- poj 1511 Invitation Cards
- POJ 1511(链式前向星)
- POJ 1511 Invitation Cards
- POJ 1511 Invitation Cards //spfa
- poj 1511 Invitation Cards(2遍spfa)
- POJ 1511 双向单源最短路 SPFA+邻接表
- POJ-1511-Invitation Cards(SPFA 反向建图)
- poj 1511 Invitation Cards (spfa+邻接表)
- POJ 1511 Invitation Cards 最短路SPFA
- POJ 1511 Invitation Cards
- Invitation Cards(POJ-1511)
- poj 1511 Invitation Cards 最短路SPFA(模板题)
- poj 1511 Invitation Cards spfa 基础题!!虽然我做的比较坎坷!!可以练手啊!!
- poj 1511 Invitation Cards
- POJ 1511 Invitation Cards