POJ 1511 Invitation Cards【来回最短路+SPFA】
来源:互联网 发布:网络二层 三层区别 编辑:程序博客网 时间:2024/05/16 09:35
Invitation Cards
Time Limit: 8000MS
Memory Limit: 262144K
Total Submissions: 24357
Accepted: 8049
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
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
题目大意:求一个来回最短路
思路:正向、反向两次建图两次SPFA即可。
注意数据比较大,要使用long long。
AC代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define INF 10000000000LL#define ll long long intint head2[1000005];int head[1000005];struct EdgeNode{ int from; int to; int w; int next;}e[1000005],ee[1000005];ll dis[1000005];int vis[1000005];int n,m,cont,cont2;void add(int from,int to,int w){ e[cont].from=from; e[cont].to=to; e[cont].w=w; e[cont].next=head[from]; head[from]=cont++;}void add2(int from,int to,int w){ ee[cont2].from=from; ee[cont2].to=to; ee[cont2].w=w; ee[cont2].next=head2[from]; head2[from]=cont2++;}void SPFA(){ memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++)dis[i]=INF; dis[1]=0; vis[1]=1; queue<int >s; s.push(1); while(!s.empty()) { int u=s.front(); s.pop();vis[u]=0; for(int k=head[u];k!=-1;k=e[k].next) { int v=e[k].to; int w=e[k].w; if(dis[v]>dis[u]+w) { dis[v]=dis[u]+w; if(vis[v]==0) { vis[v]=1; s.push(v); } } } }}void SPFA2(){ memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++)dis[i]=INF; dis[1]=0; vis[1]=1; queue<int >s; s.push(1); while(!s.empty()) { int u=s.front(); s.pop();vis[u]=0; for(int k=head2[u];k!=-1;k=ee[k].next) { int v=ee[k].to; int w=ee[k].w; if(dis[v]>dis[u]+w) { dis[v]=dis[u]+w; if(vis[v]==0) { vis[v]=1; s.push(v); } } } }}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); cont=0; cont2=0; memset(head,-1,sizeof(head)); memset(head2,-1,sizeof(head2)); for(int i=0;i<m;i++) { int x,y,w; scanf("%d%d%d",&x,&y,&w); add(x,y,w); add2(y,x,w); } SPFA(); ll ans=0; for(int i=1;i<=n;i++)ans+=dis[i]; SPFA2(); for(int i=1;i<=n;i++)ans+=dis[i]; printf("%lld\n",ans); }}
- POJ 1511 Invitation Cards 求来回最短路(spfa)
- POJ 1511 Invitation Cards【来回最短路+SPFA】
- poj 1511 Invitation Cards(spfa最短路)
- POJ 1511 Invitation Cards 最短路SPFA
- (POJ 1511)Invitation Cards 有向图来回最短路 + SPFA (复习必看题)
- poj 1511&&zoj 2008 Invitation Cards 最短路+SPFA
- POJ 1511 Invitation Cards(spfa最短路)
- POJ 1511Invitation Cards SPFA最短路问题
- poj 1511 Invitation Cards 最短路SPFA(模板题)
- POJ 1511 Invitation Cards(最短路spfa算法)
- POJ 1511 Invitation Cards(最短路spfa算法)
- POJ-1511 Invitation Cards【最短路】
- POJ 1511 Invitation Cards (最短路)
- poj 1511 Invitation Cards(最短路)
- poj 1511 Invitation Cards (最短路)
- POJ 1511 Invitation Cards 【最短路,spfa算法,Dijkstra算法堆优化】
- [最短路spfa]pku1511-Invitation Cards
- HDOJ 1535 Invitation Cards 【最短路 SPFA】
- 数据结构与算法之五 链接列表
- 斜杠/和反斜杠\的区别
- macbook 新手可以参考下(随记的常用操作,很乱,多少有点用)
- 山东第7届省赛 K题 Reversed Words
- 计算机网络的总结[2]
- POJ 1511 Invitation Cards【来回最短路+SPFA】
- iOS安全编码指南 Secure Coding Guide -- 02 Types of Security Vulnerabilities 上
- Metal着色语言编程指南 二五
- leetcode - Largest Rectangle in Histogram
- JQuery全局篇
- SlidingMenu的属性详解
- Mongodb的副本集节点角色介绍及选举过程浅析
- SQLServer中服务器角色和数据库角色权限详解
- 山东第7届省赛 J题 Execution of Paladin