ACM: POJ 3259 Wormholes - SPFA负环判定
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.
/*/
这题是判断是否产生了负环。 由于边权出现了负数这个题目就不能用普通的最短路来算,用到Floyd 或者SPFA算法。 由于题目意思要判断是否产生了负环,可以直接用SPFA算法来判断负环。 下面这个SPFA算法,d[n]为终点,如果起点u开始到达不了某个点,那么d等于INF 如果到达的了,且路上会经过负环,所以到达那个点的时候,最短路会等于-INF 如果到达那个点的时候,是正常到达的,没有经过负环,而且可以到达,那么就是介于两者之间的一个正常的值。 AC代码: /*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"queue"
#include"cmath"
using namespace std;
typedef long long LL ;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define FK(x) cout<<"["<<x<<"]\n"
#define bigfor(x) for(int qq=1;qq<= T ;qq++) const int spfa_v=10005;
const int spfa_edge=10005; template <class T>
struct SPFA { struct Edge {
int v,nxt;
T w;
} E[spfa_edge<<1]; int Head[spfa_v],erear; T p[spfa_v],INF; typedef pair< T , int > PII; void edge_init() {
memset(Head,-1);
memset(E,0);
memset(dis,0);
erear=0;
} void edge_add(int u,int v,T w) {
E[erear].v=v;
E[erear].w=w;
E[erear].nxt=Head[u];
Head[u]=erear++;
} bool dis[spfa_edge];
bool vis[spfa_edge];
int flag[spfa_edge]; void init() {
memset(vis,0);
memset(p,0x3f);
memset(flag,0);
memset(dis,0);
INF=p[0];
} void run(int u,int n) { //u为起点
init();
queue<int > Q;
while(!Q.empty())Q.pop();
p[u]=0;
Q.push(u);
while(!Q.empty()) {
int a=Q.front();
Q.pop();
vis[a]=0,dis[a]=1;
for(int i=Head[a]; ~i; i=E[i].nxt) {
int v=E[i].v;
T w=E[i].w;
int s = p[a] == -INF?-INF:w+p[a]; //如果已经是负环了,后面的也赋值-INF
if(s<p[v]) {
p[v]=s;
if(!vis[v]) { //判断是否已经走过这条边了。
vis[v]=1;
flag[v]++;
if(flag[v]>n)p[v]=-INF; //如果超过n次则说明已经形成了负环,值赋为-INF
Q.push(v);
}
}
}
}
}
};
SPFA<int > sp; int main() {
int T;
int n,m,k,u,v,w;
while(~scanf("%d",&T)) {
bigfor(T) {
sp.edge_init();
int sign=0;
scanf("%d%d%d",&n,&m,&k);
for(int i=0; i<m; i++) {
scanf("%d%d%d",&u,&v,&w);
sp.edge_add(u,v,w);
sp.edge_add(v,u,w);
}
for(int i=0; i<k; i++) {
scanf("%d%d%d",&u,&v,&w);
sp.edge_add(u,v,-w);
}
for(int i=1; i<=n; i++) {
if(sp.dis[i])continue; //如果这个点已经查过那就不需要再查。
sp.run(i,n);
if(sp.p[i]<0) {//产生了负环sp.p[i]的值才可能为负 而且是 -INF
sign=1;break;
}
}
printf("%s\n",sign?"YES":"NO") ;
}
}
return 0;
}
ACM: POJ 3259 Wormholes - SPFA负环判定的更多相关文章
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- POJ 3259 Wormholes SPFA算法题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- poj 3259 (Bellman_Ford判断负环)
题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ...
- poj 3259 Wormholes spfa算法
点击打开链接 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25582 Accepted: 9186 ...
- POJ 3259 Wormholes(负权环路)
题意: 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单 ...
- poj 3259 Wormholes 判断负权值回路
Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...
- POJ 3259 Wormholes Bellman_ford负权回路
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
随机推荐
- location url 反向代理到来机的其它端口 gitlab
location /nexus { proxy_pass http://127.0.0.1:8081/nexus; } [root@GitMaven conf]# pwd /var/opt/gitla ...
- linux中解决SSH连接慢问题 关键点GSSAPIAuthentication
[root@ok 6FE5-D831]# ssh -v xxx.xxx.xxx.64 OpenSSH_5.3p1, OpenSSL Feb debug1: Reading configuration ...
- 数据结构和算法 – 6.构建字典: DictionaryBase 类和 SortedList 类
6.1.DictionaryBase 类的基础方法和属性 大家可以把字典数据结构看成是一种计算机化的词典.要查找的词就是关键字,而词的定义就是值. DictionaryBase 类是一种用作专有字 ...
- VS2013 当前不会命中断点,还没有为该文档加载任何符号
方法一: 把ie的 调试 打开,然后调试的时候 会问你 是在新示例中打开 还是 当前示例,你选择当前的就行了.还有 建议你用 ie8.0的 开发者工具 调试 非常舒服 我已经 早就不用debuger ...
- IBM Rational AppScan 无法记录登录序列 分类: 数据安全 2015-03-18 16:46 158人阅读 评论(0) 收藏
为了测试漏洞,我在本地部署了一个站点,为http://localhost/app,并且有登录页面. 但是尝试多次,都无法记录登录页面.此时尝试了在hosts文件中,自定义了一个域名 127.0.0.1 ...
- CentOS 6.5下samba服务器搭建与配置
转自:http://www.centoscn.com/CentosServer/ftp/2014/1023/3989.html 写在前面: 首先关闭防火墙:service iptables stop, ...
- mathematica练习程序(图像取反)
代码很简单,就四行,我想到可以用mathematica干点什么了. 有人通过mathematica编程研究过视频编解码算法么,挺有意思,可以尝试一下. img=Import["f:/lena ...
- ML 06、感知机
机器学习算法 原理.实现与实践 —— 感知机 感知机(perceptron)是二分类的线性分类模型,输入为特征向量,输出为实例的类别,取值+1和-1.感知机学习旨在求出将训练数据进行线性划分的分离超 ...
- Android开发的教程和资源
Android 设计指南非官方简体中文版 http://www.apkbus.com/design/index.html NDK下载 http://developer.android.com/tool ...
- MySQL的中文编码问题
创建表格时,怎么让表格显示中文?注意:不区分大小写 mysql> ALTER TABLE 表格的名字 CONVERT TO CHARACTER SET UTF8; 怎么让默认的数据库支持中文字符 ...