Invitation Cards
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
2
2 2
1 2 13
2 1 33
4 6
1 2 10
21 60
1 3 20
3 4 10
2 4 5
4 1 50
Sample Output
46
210
Source
题目类型:SPFA+邻接表
算法分析:直接构建一个邻接表和一个逆邻接表,然后使用SPFA算法计算两次即可,注意由于数据笔比较大,所以最大值要使用long long型!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define i64 long long const i64 INF = 0x7FFFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e6 + 66; struct Node { i64 v, w, next; }; Node edge[maxn]; i64 head[maxn], edgelen; Node redge[maxn]; i64 rhead[maxn], redgelen; void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); memset (rhead, -1, sizeof (rhead)); memset (redge, -1, sizeof (redge)); edgelen = 0; redgelen = 0; } void AddEdge (i64 u, i64 v, i64 w) { edge[edgelen].v = v; edge[edgelen].w = w; edge[edgelen].next = head[u]; head[u] = edgelen++; redge[redgelen].v = u; redge[redgelen].w = w; redge[redgelen].next = rhead[v]; rhead[v] = redgelen++; } //分别表示节点到源点的最距离和顶点个数 i64 dis[maxn], inq[maxn], rdis[maxn], rinq[maxn]; i64 n, m; //数组下标从1开始 void spfa (i64 s) { for (i64 i = 1; i <= n; i++) { dis[i] = INF; inq[i] = 0; } dis[s] = 0; queue <i64> qu; qu.push (s); inq[s]++; while (!qu.empty ()) { i64 tt = qu.front(); qu.pop (); inq[tt]--; i64 pa = head[tt]; while (pa != -1) { i64 pb = edge[pa].v; if (dis[tt] < INF && dis[pb] > dis[tt] + edge[pa].w) { dis[pb] = dis[tt] + edge[pa].w; if (!inq[pb]) { qu.push (pb); inq[pb]++; } } pa = edge[pa].next; } } } void rspfa (i64 s) { for (i64 i = 1; i <= n; i++) { rdis[i] = INF; rinq[i] = 0; } rdis[s] = 0; queue <i64> qu; qu.push (s); rinq[s]++; while (!qu.empty ()) { i64 tt = qu.front(); qu.pop (); rinq[tt]--; i64 pa = rhead[tt]; while (pa != -1) { i64 pb = redge[pa].v; if (rdis[tt] < INF && rdis[pb] > rdis[tt] + redge[pa].w) { rdis[pb] = rdis[tt] + redge[pa].w; if (!rinq[pb]) { qu.push (pb); rinq[pb]++; } } pa = redge[pa].next; } } } int main() { // CFF; i64 t; scanf ("%lld", &t); while (t--) { Init (); scanf ("%lld%lld", &n, &m); i64 u, v, w; for (i64 i = 1; i <= m; i++) { scanf ("%lld%lld%lld", &u, &v, &w); AddEdge (u, v, w); } spfa (1); rspfa (1); i64 res = 0; for (i64 i = 2; i <= n; i++) if (dis[i] < INF && rdis[i] < INF) res += dis[i] + rdis[i]; printf ("%lld\n", res); } return 0; } |
- « 上一篇:poj1496
- poj1523:下一篇 »