题目链接:http://poj.org/problem?id=1511
Invitation Cards
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 29286 | Accepted: 9788 |
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
题解:
1.可知最短路分为两段, 点1到各个点的最短距离,以及各个点到点1的最短距离。
2.先求出点1到各个点的最短距离, 记为dis1。
3.将各条边的方向取反, 然后再求出点1到各个点的最短距离,记为dis2。因为边取反了,所以dis2实际是各个点到点1的最短距离。
4.对(dis1+dis2)求和,即为答案。
5.同样的题型:POJ3268
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 9e18; 18 const int MOD = 1e9+7; 19 const int MAXN = 1e6+10; 20 21 int n, m; 22 23 struct edge 24 { 25 int from, to, w, next; 26 }edge[MAXN*4]; 27 int cnt, head[MAXN]; 28 29 void add(int u, int v, int w) 30 { 31 edge[cnt].from = u; 32 edge[cnt].to = v; 33 edge[cnt].w = w; 34 edge[cnt].next = head[u]; 35 head[u] = cnt++; 36 } 37 38 void init() 39 { 40 cnt = 0; 41 memset(head, -1, sizeof(head)); 42 } 43 44 LL dis1[MAXN], dis2[MAXN]; 45 int times[MAXN], inq[MAXN]; 46 void spfa(int st, LL dis[]) 47 { 48 memset(inq, 0, sizeof(inq)); 49 memset(times, 0, sizeof(times)); 50 for(int i = 1; i<=n; i++) 51 dis[i] = LNF; 52 53 queue<int>Q; 54 Q.push(st); 55 inq[st] = 1; 56 dis[st] = 0; 57 while(!Q.empty()) 58 { 59 int u = Q.front(); 60 Q.pop(); inq[u] = 0; 61 for(int i = head[u]; i!=-1; i = edge[i].next) 62 { 63 int v = edge[i].to; 64 if(dis[v]>1LL*dis[u]+1LL*edge[i].w) 65 { 66 dis[v] = 1LL*dis[u]+1LL*edge[i].w; 67 if(!inq[v]) 68 { 69 Q.push(v); 70 inq[v] = 1; 71 } 72 } 73 } 74 } 75 } 76 77 int main() 78 { 79 int T; 80 scanf("%d",&T); 81 while(T--) 82 { 83 init(); 84 scanf("%d%d", &n, &m); 85 for(int i = 0; i<m; i++) 86 { 87 int u, v, w; 88 scanf("%d%d%d", &u, &v, &w); 89 add(u,v,w); 90 } 91 92 spfa(1, dis1); 93 init(); 94 /**一开始想直接取反,即swap(edge[i].from, edge[i].to), 95 后来发现处理不了head[]数组, 所以还是重新建边 */ 96 for(int i = 0; i<m; i++) //m为原来的边数, 即cnt 97 add(edge[i].to, edge[i].from, edge[i].w); 98 spfa(1, dis2); 99 100 LL ans = 0; 101 for(int i = 1; i<=n; i++) 102 ans += dis1[i]+dis2[i]; 103 104 printf("%lld ", ans); 105 } 106 }