首页 > 代码库 > POJ 1511 Invitation Cards (最短路)
POJ 1511 Invitation Cards (最短路)
Invitation Cards
Time Limit: 8000MS | Memory Limit: 262144K | |
Total Submissions: 19215 | Accepted: 6311 |
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
Central Europe 1998
代码还是百度的,自己打了一遍,还是有些不懂,希望有人能够帮忙解决,THX.
毕竟初学,没人教,有点摸不着路的感觉。
题目的意思抽象出来就是给一个有向图..求 1到所有点的最小距离之和与所有点到1最小距离之和相加的最小值....用一个正向的原图做一次SPFS..再将所有边反过来做一次SPFA
代码:1876MS (好像哪里出错了,只能纯文本复制过来了)
代码还是百度的,自己打了一遍,还是有些不懂,希望有人能够帮忙解决,THX.
毕竟初学,没人教,有点摸不着路的感觉。
题目的意思抽象出来就是给一个有向图..求 1到所有点的最小距离之和与所有点到1最小距离之和相加的最小值....用一个正向的原图做一次SPFS..再将所有边反过来做一次SPFA
代码:1876MS (好像哪里出错了,只能纯文本复制过来了)
#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[N], ophead[N]; long long dis[N]; bool vis[N]; 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)); }//add这个函数,还是要自己用手画一下,反正我光看是没看明白。//其实就是构建了链表,将起点一样的边用链表链接起来,这也就是head数组为什么用起点值做下标。在模板里是不需要edge[].u这个结构体变量的。 //因为正反要各跑一遍,又是单向图。//(其实用两个结构体是因为链表链接的是同起点,现在要反过来,那么终点变起点,又要另外构建一个反链表)。void add(int u, int v, int w){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++;}//C++引用数组的方法,避免降值问题的发生(以前用的都是结构体名,涨姿势了)。
//其实也就是一个模板。void spfa(int (&hh)[N], Node (&Edge)[M]){ 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(){ 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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。