POJ-1511 Invitation Cards
2014-08-12 02:08
281 查看
[align=center]Invitation Cards[/align]
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
Sample Output
————————————————————集训12.1的分割线————————————————————
前言:睡前秒了。。。。。。8s的时限1.8s过了是不是运气好了点……
思路:每个车站去一个人,完了还要回来。首先注意到数据范围,点数1e6,边数1e6。稀疏图。(矩阵你也存不下)然后就考虑了一下,Dij+临接表……复杂度有点高?所以就想起来SPFA。见效快疗效好。建了两张图,空间换时间。这样正着跑一遍,得到所有的最短路,反着只需要再跑一遍,得到所有逆向最短路。一步不多一步不少正正好。根本不需要Floyd吧?毕竟我不会。另外要注意溢出问题,INF要设置1e10那么大,dis开成LL。
代码如下:
Time Limit: 8000MS | Memory Limit: 262144K |
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 2 1 60 1 3 20 3 4 10 2 4 5 4 1 50
Sample Output
46 210
————————————————————集训12.1的分割线————————————————————
前言:睡前秒了。。。。。。8s的时限1.8s过了是不是运气好了点……
思路:每个车站去一个人,完了还要回来。首先注意到数据范围,点数1e6,边数1e6。稀疏图。(矩阵你也存不下)然后就考虑了一下,Dij+临接表……复杂度有点高?所以就想起来SPFA。见效快疗效好。建了两张图,空间换时间。这样正着跑一遍,得到所有的最短路,反着只需要再跑一遍,得到所有逆向最短路。一步不多一步不少正正好。根本不需要Floyd吧?毕竟我不会。另外要注意溢出问题,INF要设置1e10那么大,dis开成LL。
代码如下:
/* ID: j.sure.1 PROG: LANG: C++ */ /****************************************/ #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #include <queue> #include <vector> #include <map> #include <string> #include <iostream> #include <climits> #define INF 10000000000LL using namespace std; /****************************************/ const int N = 1e6+5, M = 1e6+5; int n, m, tot, head , ophead ; long long dis ; bool vis ; struct Node { int u, v, w; int next; }edge[M], opedge[M]; void init() { tot = 0; memset(head, -1, sizeof(head)); memset(ophead, -1, sizeof(ophead)); } void add(int u, int v, int w)//本来写的是隐函数声明,POJ不给编译过,因此写了一大坨 { edge[tot].u = u; edge[tot].v = v; edge[tot].w = w; edge[tot].next = head[u]; head[u] = tot; opedge[tot].u = v; opedge[tot].v = u; opedge[tot].w = w; opedge[tot].next = ophead[v]; ophead[v] = tot; tot++; } void spfa(int (&hh) , Node (&Edge)[M])//C++引用数组的方法,避免降值问题的发生 { for(int i = 1; i <= n; i++) { dis[i] = INF; } memset(vis, 0, sizeof(vis)); queue <int> q;//局部队列,不需清空 q.push(1); dis[1] = 0; vis[1] = 1; while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for(int i = hh[u]; i != -1; i = Edge[i].next) { int v = Edge[i].v; if(dis[v] > dis[u] + Edge[i].w) {//松弛操作 dis[v] = dis[u] + Edge[i].w; if(!vis[v]) { q.push(v); vis[v] = true; } } } } } int main() { #ifdef J_Sure freopen("222.in", "r", stdin); // freopen(".out", "w", stdout); #endif int T; scanf("%d", &T); while(T--) { init(); scanf("%d%d", &n, &m); int a, b, c; for(int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); add(a, b, c); } long long ans = 0; spfa(head, edge); for(int i = 2; i <= n; i++) { ans += dis[i]; } spfa(ophead, opedge); for(int i = 2; i <= n; i++) { ans += dis[i]; } printf("%lld\n", ans);//正反各跑一次,统计答案 } return 0; }
相关文章推荐
- POJ 1511 最短路径之和(spfa或dijkstra+heap)
- POJ 1511 Invitation Cards (SPFA)
- POJ 1511 Invitation Cards 邻接表 spfa算法
- poj 1511 spfa水题
- POJ 1511 - Invitation Cards
- POJ 1511
- Poj 3268 Silver Cow Party + Poj 1511 Invitation Cards (最短路反向建图)
- poj 1511
- POJ-1511(Invitation Cards )——spfa+邻接表
- poj 1511 正反向建图
- poj 1511 Invitation Cards(2遍spfa)
- POJ 1511 Invitation Cards
- POJ 1511 邻接表加spfa,刚开始未用邻接表构图,然后TLE到死啊、、、、、、
- POJ 1511 spfa求最短回路
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
- poj 1511 Invitation Cards(spfa + 邻接表 + 反向思维t)
- poj---1511
- poj 1511 Invitation Cards 静态邻接表的SPFA
- 【poj 1511】 Invitation Cards 【Central Europe 1998】
- poj 1511 Invitation Cards spfa 邻接矩阵