POJ 1511(邻接表和最短路 SPFA)
来源:互联网 发布:淘宝联盟官方网站首页 编辑:程序博客网 时间:2024/05/17 07:03
Invitation Cards
Time Limit: 8000MS Memory Limit: 262144K
Total Submissions: 25052 Accepted: 8273
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
SPFA即shotest path faster algorithm,由意思可以看出该算法效率比较高。
大概意思呢,求源点到各点的最短路之和,然后再求各点到源点的最短路之和。感觉就是把用邻接矩阵的最短路改变成了用邻接表加上队列,这个用途可以用来求那些数据很大的最短路,感觉真是越学东西越厉害
#include <cstdio>#include <cstring>#include <queue>using namespace std;#define M 1000010#define INF 0x3f3f3f#define RCL(a, b) memset(a, b, sizeof(a))#define LL long longstruct node{ int to, val, next;};node edge1[M*2], edge2[M*2];int head1[M*2], edgen, head2[M*2], p;LL dist[M];void addedge(node *edge, int *head, int u, int v, int val){ node e = {v, val, head[u]}; edge[edgen] = e; head[u] = edgen;}void init(){ RCL(head1, -1); RCL(head2, -1); edgen = 0;}bool vis[M];LL spfa(node *edge, int *head, int start)//永远的模板{ RCL(vis, false); RCL(dist, INF); queue<int> q; dist[start] = 0;/* 永远从这个地方开始~~~,因为别的都dist都特别大,在后面的判断中是无法压入队列的*/ q.push(start); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false;//允许再次连接到这个点,或许从这里出发的路不通,但是不代表来这里的路不能走 for(int i=head[u]; i!=-1; i=edge[i].next) { int v = edge[i].to; int val = edge[i].val; if(dist[v] > dist[u] + val)//这里就可以不用担心无限循环了 { dist[v] = dist[u] + val; if(!vis[v])//u 与 v 可能有多条路连接 ,只压进去一个就好了 { vis[v] = true; q.push(v); } } } } LL ans = 0; for(int i=1; i<=p; i++) { ans += dist[i]; } return ans;}int main(){ int t, v, u, val, q; scanf("%d", &t); while(t--) { init(); scanf("%d%d", &p, &q); for(int i=1; i<=q; i++) { scanf("%d%d%d", &u, &v, &val); addedge(edge1, head1, u, v, val);//在这里是单向边连接 //当想要双向时直接 u v 反过来就好了 addedge(edge2, head2, v, u, val); edgen++; } LL ans = spfa(edge1, head1, 1) + spfa(edge2, head2, 1); printf("%lld\n", ans); } return 0;}
- POJ 1511(邻接表和最短路 SPFA)
- 邻接表和SPFA(POJ 1511)
- poj Wormholes (最短路 spfa Bellman-Ford 算法 邻接表实现)
- 旧代码 - 最短路 Spfa(邻接表)
- 最短路SPFA算法 (通过邻接表来实现)
- spfa求最短路模板(邻接表)
- HDU 2544 最短路(spfa邻接表)
- POJ 1511 双向单源最短路 SPFA+邻接表
- 最短路 SPFA 判断负环 静态邻接表(链式前向星) HDU 2544 最短路 POJ 3259 Wormholes
- POJ 1511 邻接表+spfa
- poj 1511(SPFA+邻接表)
- SPFA求单源最短路(邻接表)
- poj1511 spfa最短路+手写邻接表+反向建图
- HDU2544_最短路(Dijkstra)(Bellman-Ford)(SPFA+邻接表/邻接矩阵)
- 最短路模板——dijkstra,SPFA(邻接表实现)
- HDU 2544 最短路 SPFA 邻接表 模板
- hdoj 2544 最短路 【dijkstra + floyd + spfa邻接表实现】
- hdu 1874 畅通工程续(最短路spfa邻接表)
- C#入门6.6——字符串的截取
- 一个挺好的Window系统的小工具 —— mytodolist
- android学习笔记 单位转换工具类
- Android 解决ListView 和 ScrollView 共存冲突的问题
- scala编程实践---类
- POJ 1511(邻接表和最短路 SPFA)
- Go语言之常量与运算符
- poj-1611
- 中文分词工具的初步使用图解
- iOS常用的一些类库
- eclipse怎么设置字体大小
- JMeter学习 JMeter使用中遇到的问题:Jmeter Debug - "Unrecognized VM option '+HeapDumpOnOutOfMemoryError"
- jQuery总结
- SP_CREATE_TIME_DIMENSION 自动生成周末日期