Invitation Cards
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3129 Accepted Submission(s):
1456
Problem 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.
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
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
Recommend
第一次使用spfa算法,完全不知道是什么。。。看了网上的代码,明白了思路,先计算从1为起点到各点的距离之和,再重置地图,计算各点到1点的距离之和。
题意:有编号1~P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费。有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1。 求所有人来回的最小费用之和。
附上代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #define M 1000010 6 #define inf 0x3f3f3f3f 7 using namespace std; 8 struct node 9 { 10 int now,to,w; 11 } e[M]; 12 int first[M],nexts[M]; 13 int n,m,dis[M],vis[M]; 14 void init() 15 { 16 int i,j; 17 for(i=0; i<=m; i++) 18 first[i]=nexts[i]=-1; 19 for(i=0; i<m; i++) 20 { 21 scanf("%d%d%d",&e[i].now,&e[i].to,&e[i].w); 22 nexts[i]=first[e[i].now]; //spfa()算法 23 first[e[i].now]=i; 24 } 25 } 26 27 void spfa(int src,int flag) 28 { 29 int i,j; 30 for(i=0; i<=n; i++) 31 dis[i]=inf; 32 dis[src]=0; 33 for(i=0; i<=n; i++) 34 vis[i]=0; 35 queue<int> q; 36 q.push(src); 37 while(!q.empty()) 38 { 39 src=q.front(); 40 q.pop(); 41 vis[src]=0; 42 for(i=first[src]; i!=-1; i=nexts[i]) 43 { 44 int to=(flag?e[i].to:e[i].now); 45 if(dis[to]>dis[src]+e[i].w) //每次比较更新 46 { 47 dis[to]=dis[src]+e[i].w; 48 if(!vis[to]) 49 { 50 vis[to]=1; 51 q.push(to); 52 } 53 } 54 } 55 } 56 } 57 58 void set_map() 59 { 60 int i,j; 61 for(i=0; i<=m; i++) 62 first[i]=nexts[i]=-1; 63 for(i=0; i<m; i++) 64 { 65 int now=e[i].now; 66 int to=e[i].to; 67 nexts[i]=first[to]; 68 first[to]=i; 69 } 70 } 71 int main() 72 { 73 int T,i,j,sum; 74 scanf("%d",&T); 75 while(T--) 76 { 77 sum=0; 78 scanf("%d%d",&n,&m); 79 init(); 80 spfa(1,1); 81 for(i=2; i<=n; i++) //先将1到每个车站的最小花费加起来 82 sum+=dis[i]; 83 set_map(); //重置图 84 spfa(1,0); 85 for(i=2; i<=n; i++) //将所有点到1的最小花费加起来 86 sum+=dis[i]; 87 printf("%d ",sum); 88 } 89 return 0; 90 }