poj 3259 Wormholes spfa算法
来源:互联网 发布:唯品会 淘宝 编辑:程序博客网 时间:2024/06/05 08:26
点击打开链接
Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Sample Input
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample Output
NOYES
Hint
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
spaf是bellman算法的一个优化,每次松弛的不是全图的点,而是与最短距离有变化的点相连的那些点,题目用到一个队列,每个点如果最短路径更新了,就扔到队列里,然后从队列中拿出点来松弛与其相连的点,如果一个点入队列的次数达到n此,说明图中有负圈
注意两个点之间可能有多条路径,选择权值较小的那条边留下
#include<stdio.h>#include<string.h>#include<utility>#include<queue>using namespace std;int map[501][501];int dis[501];int n, m, w;int s, e, t;bool spfa(){bool flag[501] = {0};int count[501] = {0};queue<int > q;q.push(s);dis[s] = 0;int curr;int i;while(!q.empty()){curr = q.front();q.pop();for(i = 1; i <= n; i++){if(map[curr][i] < 100000){if(dis[i] > map[curr][i] + dis[curr] ){dis[i] = map[curr][i] + dis[curr];if(flag[i] == 0)q.push(i);count[i] ++ ;flag[i] = 1;if(count[i] >= n)return 0;}}}flag[curr] = 0;}return 1;}int main(){int f;scanf("%d", &f);while(f--){memset(dis,63, sizeof(dis));memset(map, 127, sizeof(map));scanf("%d %d %d", &n, &m, &w);int i;for(i = 0; i < m; i++){scanf("%d %d %d", &s, &e, &t);map[s][e] = map[s][e] > t? t : map[s][e];map[e][s] = map[e][s] > t? t : map[e][s];}for(i = 0; i < w; i++){scanf("%d %d %d", &s, &e, &t);map[s][e] = -t;}if(spfa())printf("NO\n");elseprintf("YES\n");}return 0;}
- poj 3259 Wormholes spfa算法
- POJ 3259 Wormholes SPFA算法题解
- poj 3259 Wormholes //SPFA
- poj 3259 Wormholes SPFA
- POJ 3259 Wormholes(SPFA)
- Wormholes - POJ 3259 spfa
- poj 3259 Wormholes(spfa)
- POJ 3259:Wormholes 【SPFA】
- Wormholes POJ 3259【SPFA】
- poj 3259-- Wormholes(SPFA)
- POJ-3259 Wormholes(SPFA)
- Poj 3259 Wormholes ( SPFA
- POJ 3259 Wormholes spfa
- [POJ 3259] Wormholes [SPFA]
- 【POJ 3259】Wormholes 【spfa判负环】
- poj 3259--Wormholes 【spfa判负环】
- POJ 3259-Wormholes-SPFA判负环
- POJ 3259 Wormholes SPFA判负环
- Java的两种异常的处理
- wait queue in linux kernel
- BUREK
- 在对话框显示图标
- android Bitmap 转化成字节数组与流
- poj 3259 Wormholes spfa算法
- IExternalCommand和IExternalApplication的简单用法
- Pie Chart 学习例子
- Visual Studio 2010 安装出现问题记录
- 如何提高SQL2000性能
- unity3d android
- Caused by: java.net.SocketException: Software caused connection abort: socket write error。
- PHP SEVER预定义变量
- HDU4686【矩阵快速幂】