最短路径__Invitation Cards( poj 1511 )
来源:互联网 发布:cpu温度监控软件 编辑:程序博客网 时间:2024/06/08 05:18
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.
题目大意:给出n个点和n条有向边,求所有点到源点1的来回最短路之和(保证每个点都可以往返源点1)
解题思路:这个数据范围太大,明显的不能用floyd,dijstra,bellman-ford这些算法,用spfa的话也不能用邻接矩阵存,因为点太多了,所以采用spfa的邻接表存储搞定
#include<cstdio>#include<algorithm>#include<queue>#include<vector>#include<string.h>#include<iostream>#include<stdlib.h>#define N 1000100#define INF 0x7fffffffusing namespace std;struct edge{ int u,next; int s;}Edge1[N*10],Edge2[N*10];int visit[N];int head1[N],head2[N];int dis[N];int n,m,Len;void Insert(int u,int v,int w,edge* Edge,int* head){ Edge[Len].u = v; Edge[Len].s = w; Edge[Len].next = head[u]; head[u] = Len;}void SPFA(edge* Edge,int* head,int x){ for(int i = 1; i <= n; i ++)visit[i] = false; for(int i = 1; i <= n; i ++)dis[i] = INF; dis[x] = 0; queue<int>team; team.push(x); visit[1] = 1; while(!team.empty()) { int now = team.front(); team.pop(); visit[now] = false; for(int i = head[now]; i != - 1; i = Edge[i].next) { int next = Edge[i].u; if(dis[next] > dis[now] + Edge[i].s) { dis[Edge[i].u] = dis[now] + Edge[i].s; if(visit[next])continue; visit[next] = 1; team.push(next); } } }}int main(){ int t,i,j,k; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(head1,-1,(n+2)*sizeof(int)); memset(head2,-1,(n+2)*sizeof(int)); Len = 0; for(i = 0 ; i < m ; i ++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); Insert(a,b,c,Edge1,head1); Insert(b,a,c,Edge2,head2); Len++; } SPFA(Edge1,head1,1); long long ans = 0; for(i = 1; i <= n ; i ++) ans += dis[i]; SPFA(Edge2,head2,1); for(i = 1; i <= n ; i ++) ans += dis[i]; printf("%lld\n",ans); } return 0;}
0 0
- 最短路径__Invitation Cards( poj 1511 )
- pku 1511 Invitation Cards(最短路径)
- POJ 最短路径
- poj 最短路径 汇总
- POJ 3259 最短路径
- poj 1125 最短路径
- 【最短路径】poj 2387
- 【最短路径】poj 1062
- 最短路径__Wormholes( Poj )
- 最短路径__Candies ( Poj )
- poj 2253(最短路径)
- 最短路径-POJ-2387
- PKU1511(Invitation Cards)最短路径-邻接表+SPFA算法
- hdu 1535 Invitation Cards (最短路径)
- 变形的最短路径 POJ 3621
- POJ 2457 Part Acquisition 最短路径
- 最短路径 POJ 1135 Domino Effect
- POJ 2253 最短路径变形
- HDU 2063 匈牙利算法
- 如何解决1万个并发连接,用每个客户一个线程的方法
- Android虚拟SD卡
- ECharts总结的很好
- 【软考】软件开发模型
- 最短路径__Invitation Cards( poj 1511 )
- 最小的K个数 (冒泡和最小堆)
- 升级win10后无法听到声音的问题
- wireshark插件开发小结
- 【C#】C#对象的方法
- 软件工程之相关文档
- PHP与MYSQL事务处理
- 最短路径__Candies ( Poj )
- MySQL索引背后的数据结构及算法原理