POJ 1511 Invitation Cards(双向最短路)
来源:互联网 发布:小猪微电商系统源码 编辑:程序博客网 时间:2024/06/06 07:16
Invitation Cards
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
22 21 2 132 1 334 61 2 102 1 601 3 203 4 102 4 54 1 50
Sample Output
46210题目大意:给出一个有向图,求节点1到各节点的最短路以及各节点到节点1的最短路之和。
解题思路:正向建图跑Dijkstra(1)求节点1到各节点的最短路,反向建图跑Dijkstra(1)求各节点到节点1的最短路,然后两者相加即可。
代码如下:
#include <cstdio>#include <queue>#include <cstring>#include <climits>#include <algorithm>#define INF 1000000000typedef std::pair<int,int> P;typedef long long ll;const int maxp = 1000005;const int maxq = 1000005;ll first[maxp],e;ll next[maxq],v[maxq],w[maxq];ll a[maxq],b[maxq],c[maxq];long long d[maxp];int p,q;void init(){memset(first,-1,sizeof(first));e = 1;}void add_edge(int from,int to,int dis){v[e] = to;next[e] = first[from];w[e] = dis;first[from] = e++;}std::priority_queue<P,std::vector<P>,std::greater<P> > que;void dijkstra(int s){while(que.size()) que.pop();for(int i = 1;i <= p;i++) d[i] = INF;d[s] = 0;que.push(P(0,s));while(que.size()){P pp = que.top();que.pop();int u = pp.second;if(d[u] < pp.first) continue;for(int i = first[u];i != -1;i = next[i]){if(d[v[i]] > d[u] + w[i]){d[v[i]] = d[u] + w[i];que.push(P(d[v[i]],v[i]));}}}return ;}int main(){int n;scanf("%d",&n);while(n--){scanf("%d %d",&p,&q);for(int i = 0;i < q;i++)scanf("%lld %lld %lld",&a[i],&b[i],&c[i]);init();long long ans = 0;for(int i = 0;i < q;i++)add_edge(a[i],b[i],c[i]);dijkstra(1);for(int i = 1;i <= p;i++)ans += d[i];init();for(int i = 0;i < q;i++)add_edge(b[i],a[i],c[i]);dijkstra(1);for(int i = 1;i <= p;i++)ans += d[i];printf("%lld\n",ans);}return 0;}
0 0
- POJ 1511 Invitation Cards(双向最短路)
- poj 1511 Invitation Cards (最短路)
- POJ-1511 Invitation Cards【最短路】
- poj 1511 Invitation Cards(spfa最短路)
- POJ 1511 Invitation Cards (最短路)
- POJ 1511 Invitation Cards 最短路SPFA
- poj 1511 Invitation Cards(最短路)
- POJ 1511 Invitation Cards 求来回最短路(spfa)
- POJ 1511——Invitation Cards(最短路)
- POJ 题目1511 Invitation Cards(最短路)
- POJ 1511 Invitation Cards(spfa最短路)
- Invitation Cards(最短路)
- poj 1511&&zoj 2008 Invitation Cards 最短路+SPFA
- POJ 1511Invitation Cards SPFA最短路问题
- POJ 1511 Invitation Cards【来回最短路+SPFA】
- poj 1511 Invitation Cards 最短路SPFA(模板题)
- POJ 1511 Invitation Cards-2016.12月计科院赛(最短路)
- POJ 1511 Invitation Cards(最短路spfa算法)
- Mule与Servicemix比较
- hdu 3549
- Publisher Confirms and Returns(spring amqp中文翻译)
- 排序算法
- SAP 货物移动 BAPI 的简单使用 BAPI_GOODSMVT_CREATE
- POJ 1511 Invitation Cards(双向最短路)
- 源码编译安装httpd及其常见错误
- ActionScript3(AS3)类库学习之(十)——DisplayObject等显示对象基类
- 滑动菜单[slidingMove.html]
- NYOJ 47 过河问题
- 华为笔试题:地铁换乘问题
- 汇编语言之寻址方式
- Mysql海量数据存储和解决方案之一—分布式DB方案
- Mule,目前综合状态最良好的开源ESB方案