HDU 1535 Invitation Cards【SPFA最短路】【正反向建边求单源最短路之和】
来源:互联网 发布:java数据存储方式 编辑:程序博客网 时间:2024/05/17 09:26
Invitation Cards
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2535 Accepted Submission(s): 1211
Problem 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
题意:有n个车站(n为10^6范围),第一个车站是出发点。现在要从第一个车站安排志愿者去2-n号车站服务,每个车站一人。志愿者早上坐公交车从一号车站出发,晚上从各车站坐公交车回到一号车站。每条线路都有各自的费用。求最小总开销。(假设所有车站一定能到达,每次需要出发或者返回时公交车只载志愿者中的一人)
思路 有两种思路: 1.以1号车站为源点求SPFA,然后把从1号车站到各公交车站的最小花费累加。再分别以2-n号车站为源点,求出他们以1号车站为终点的最小花费并累加,所有累加值即为最小总开销。但是由于题目范围很大,所以这种思路会TLE。 2.注意到1中以2-n号车站为源点时均以1号车站为终点,而这步可以通过反向建图实现,即把所有u->v变成v->u,然后以1号车站为源点,可以一次性得到所有结果,而且复杂度也降低了。于是这种做法需要建两幅图,一幅正向,一幅反向,分别以1号车站为源点求SPFA,然后把2-n号车站的两个图的结果全部相加就可以得到最小总开销。
以上来自网络
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define MAXN 1000100#define INF 0x3f3f3fusing namespace std;struct node {int u, v, w, next;};int n, m, cnt1, cnt2;node edge1[MAXN], edge2[MAXN];int dist1[MAXN], dist2[MAXN];int head1[MAXN], head2[MAXN];bool vis[MAXN];void init(){cnt1 = 0;cnt2 = 0;memset(head1, -1, sizeof(head1));memset(head2, -1, sizeof(head2));}void add(int u, int v, int w){edge1[cnt1] = {u, v, w, head1[u]};//正建图 head1[u] = cnt1++;edge2[cnt2] = {v, u, w, head2[v]};//反建图 head2[v] = cnt2++;}void SPFA1(int sx){queue<int>q;for(int i = 1; i <= n; ++i){dist1[i] = INF;vis[i] = 0; }dist1[sx] = 0;q.push(sx);vis[sx] = 1;while(!q.empty()){int u = q.front();q.pop();vis[u] = 0;for(int i = head1[u]; i != -1; i = edge1[i].next){int v = edge1[i].v;int w = edge1[i].w;if(dist1[v] > dist1[u] + w){dist1[v] = dist1[u] + w;if(!vis[v]){vis[v] = 1;q.push(v);}}}}}void SPFA2(int sx){queue<int >q;for(int i = 1; i <= n; ++i){dist2[i] = INF;vis[i] = 0;}dist2[sx] = 0;q.push(sx);vis[sx] = 1;while(!q.empty()){int u = q.front();q.pop();vis[u] = 0;for(int i = head2[u]; i != -1; i = edge2[i].next){int v = edge2[i].v;int w = edge2[i].w;if(dist2[v] > dist2[u] + w){dist2[v] = dist2[u] + w;if(!vis[v]){vis[v] = 1;q.push(v);} }}}}int main (){int T;scanf("%d", &T);while(T--){scanf("%d%d", &n, &m);init();while(m--){int u, v, w;scanf("%d%d%d", &u, &v, &w);add(u, v, w);}SPFA1(1);SPFA2(1);int sum = 0;for(int i = 2; i <= n; ++i){sum += dist1[i] + dist2[i]; }printf("%d\n", sum);}return 0;}
0 0
- HDU 1535 Invitation Cards【SPFA最短路】【正反向建边求单源最短路之和】
- hdoj 1535 Invitation Cards 【最短路径SPFA】【正反向建边求单源最短路之和】
- HDU 1535 Invitation Cards (最短路 建正反图)
- hdu 1535 Invitation Cards 最短路spfa+逆向建图
- HDOJ 1535 Invitation Cards 【最短路 SPFA】
- hdu 1535 Invitation Cards(最短路)
- 【HDU】1535 Invitation Cards 最短路
- HDU 1535 Invitation Cards 有向图的来回最短路(邻接表反向建图)+spfa
- [最短路spfa]pku1511-Invitation Cards
- poj 1511 Invitation Cards(spfa最短路)
- POJ 1511 Invitation Cards 最短路SPFA
- HDU1535 Invitation Cards(最短路,spfa)
- HDU ACM 1535 Invitation Cards单点到多源最短路->SPFA算法
- hdu 1535 Invitation Cards spfa求最短路,反向建图再来一次
- hdu 1535 Invitation Cards(有向图的来回最短路,要反向建图)
- HDU 1535 Invitation Cards 2次Dijkstra来回最短路
- (POJ 1511)Invitation Cards 有向图来回最短路 + SPFA (复习必看题)
- 【最短路】 HDOJ 1535 Invitation Cards
- javascript验证案例
- druid连接池配置
- 详解Fedora Samba配置安装
- Spark Streaming和Flume集成指南V1.4.1
- C#学习笔记——控件篇
- HDU 1535 Invitation Cards【SPFA最短路】【正反向建边求单源最短路之和】
- 字符串拼接,追加字符串
- javascript验证案例
- 【机房收费系统】从MSHflexgrid控件导入到Excel
- 总结Codeigniter的一些优秀特性
- Android虚拟机上安装和卸载APK程序
- 截取double型数据的小数点后两位的几种方法
- 创建字符串,字符串的获取
- 计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接