poj 3259 Wormholes spfa算法
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25582 | Accepted: 9186 |
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
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
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.
bellman-ford或者SPFA都能做,就是求负回路,bellman-ford的判断方法就是全图松弛 n - 1 次,如果还能继续松弛,说明有负圈回路,否则就说明没有负圈回路,bellman-ford有一个优化就是松弛的过程中如果在一次松弛过程中任何点都没有改变最短路径,那么就可以提前结束全图的松弛操作得到结果
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");
else
printf("YES\n");
}
return 0;
}
最新文章
- Example of ConcurrentHashMap in Java--转
- CSS基础01
- 解决Cookie乱码问题
- Netty4.x中文教程系列(四) ChannelHandler
- 如何在程序里模拟在cmd里用管理员权限运行一条指令
- iOS 基础日记-修饰符
- twisted的defer模式和线程池
- Linux下nice/renice命令小结
- 个人笔记--struts2对Action的权限拦截
- C++之对象存储空间_对象模型
- XML学习经验实例总结2
- iOS 设置UILable字体,及计算长度和位置
- 开源免费接口管理平台eoLinker AMS开源版 V3.2.0更新,增加批量导出导入接口功能!
- Play框架--初学笔记
- 15.2-uC/OS-III资源管理(信号量)
- delphi XML简单处理
- Linux内核分析 第七周 可执行程序的装载
- protobuffer、gRPC、restful gRPC的相互转化
- IDEA相关知识整理
- Selenium之ActionChains(一)
热门文章
- 【转】WinForm不同版本覆盖安装
- [MVC] 深入浅出Spring MVC
- WAMP Server助你在Windows上快速搭建PHP集成环境
- 对 COM+ 组件进行了方法调用,但该组件有一个已被中止的或正在被中止的事务。 (异常来自 HRESULT:0x8004E003)
- [Hibernate] - Annotations - Many To Many
- visual studio 编译时 出现 Files 的值 乱码
- LintCode ";Copy Books";
- fiddler Android下https抓包全攻略
- IntelliJ IDEA设置自动导入包
- RadTextBox允许输入整形数字以及退格键