poj 3259 Wormholes 判断负权值回路
Wormholes
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
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
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
//Memory Time
//308K 204MS #include<iostream>
#include <string.h>
using namespace std; int dis[]; //源点到各点权值
const int max_w=; //无穷远 struct weight
{
int s;
int e;
int t;
}edge[]; int N,M,W_h; //N (1≤N≤500)fields 顶点数
//M (1≤M≤2500)paths 正权双向边
//W_h (1≤W≤200) wormholes 虫洞(回溯),负权单向边
int all_e; //边集(边总数) bool bellman()
{
bool flag; /*relax*/ for(int i=;i<N-;i++) ///dis松弛的次数
{
flag=false;
for(int j=;j<all_e;j++) ///所有边集
if(dis[edge[j].e] > dis[edge[j].s] + edge[j].t) ///可以松弛 更新
{
dis[edge[j].e] = dis[edge[j].s] + edge[j].t;
flag=true; //relax对路径有更新
}
if(!flag)
break; //只要某一次relax没有更新,说明最短路径已经查找完毕,或者部分点不可达,可以跳出relax
}///已经更新完毕了 所有边集都是两点之间的最短路 /*Search Negative Circle*/ for(int k=;k<all_e;k++) ///遍历所有边集 如果还出现能够再次更新成最短的边 就表明出现负权值回路了
if( dis[edge[k].e] > dis[edge[k].s] + edge[k].t)
return true;
return false;
}
int main(void)
{
int u,v,w; int F;
cin>>F;
while(F--)
{
memset(dis,max_w,sizeof(dis)); //源点到各点的初始值为无穷,即默认不连通 cin>>N>>M>>W_h; all_e=; //初始化指针 /*read in Positive Paths*/ for(int i=;i<=M;i++)
{
cin>>u>>v>>w;
edge[all_e].s=edge[all_e+].e=u;
edge[all_e].e=edge[all_e+].s=v;
edge[all_e++].t=w;
edge[all_e++].t=w; //由于paths的双向性,两个方向权值相等,注意指针的移动
} /*read in Negative Wormholds*/ for(int j=;j<=W_h;j++)
{
cin>>u>>v>>w;
edge[all_e].s=u;
edge[all_e].e=v;
edge[all_e++].t=-w; //注意权值为负
} /*Bellman-Ford Algorithm*/ if(bellman())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}
//Memory Time
//308K 204MS #include<iostream>
#include <string.h>
using namespace std; int dis[]; //源点到各点权值
const int max_w=; //无穷远 struct weight
{
int s;
int e;
int t;
}edge[]; int N,M,W_h; //N (1≤N≤500)fields 顶点数
//M (1≤M≤2500)paths 正权双向边
//W_h (1≤W≤200) wormholes 虫洞(回溯),负权单向边
int all_e; //边集(边总数) bool bellman()
{
bool flag; /*relax*/ for(int i=;i<N-;i++) ///dis松弛的次数
{
flag=false;
for(int j=;j<all_e;j++) ///所有边集
if(dis[edge[j].e] > dis[edge[j].s] + edge[j].t) ///可以松弛 更新
{
dis[edge[j].e] = dis[edge[j].s] + edge[j].t;
flag=true; //relax对路径有更新
}
if(!flag)
break; //只要某一次relax没有更新,说明最短路径已经查找完毕,或者部分点不可达,可以跳出relax
}///已经更新完毕了 所有边集都是两点之间的最短路 /*Search Negative Circle*/ for(int k=;k<all_e;k++) ///遍历所有边集 如果还出现能够再次更新成最短的边 就表明出现负权值回路了
if( dis[edge[k].e] > dis[edge[k].s] + edge[k].t)
return true;
return false;
}
int main(void)
{
int u,v,w; int F;
cin>>F;
while(F--)
{
memset(dis,max_w,sizeof(dis)); //源点到各点的初始值为无穷,即默认不连通 cin>>N>>M>>W_h; all_e=; //初始化指针 /*read in Positive Paths*/ for(int i=;i<=M;i++)
{
cin>>u>>v>>w;
edge[all_e].s=edge[all_e+].e=u;
edge[all_e].e=edge[all_e+].s=v;
edge[all_e++].t=w;
edge[all_e++].t=w; //由于paths的双向性,两个方向权值相等,注意指针的移动
} /*read in Negative Wormholds*/ for(int j=;j<=W_h;j++)
{
cin>>u>>v>>w;
edge[all_e].s=u;
edge[all_e].e=v;
edge[all_e++].t=-w; //注意权值为负
} /*Bellman-Ford Algorithm*/ if(bellman())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}
poj 3259 Wormholes 判断负权值回路的更多相关文章
- POJ 3259 Wormholes Bellman_ford负权回路
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes(负权环路)
题意: 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单 ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- poj 3259 (Bellman_Ford判断负环)
题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...
- Wormholes POJ - 3259 spfa判断负环
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3259 Wormholes(最短路&spfa正权回路)题解
题意:给你m条路花费时间(双向正权路径),w个虫洞返回时间(单向负权路径),问你他能不能走一圈回到原点之后,时间倒流. 思路:题意有点难看懂,我们建完边之后找一下是否存在负权回路,存在则能,反之不能. ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
随机推荐
- Android--Toast(吐司)的基本使用
1.学习Android开发,不能从单方面的知识去考虑问题,要从全面出发. 2.Toast(吐司) 1.打开eclipse 选择文件存放的位置 点击Dbms,测试是否连接成功(海马模拟器或者夜神模拟器) ...
- SQL笔记-第六章,索引与约束
一.索引 CREATE INDEX 索引名 ON 表名(字段1, 字段2,……字段n) CREATE INDEX idx_person_nameage ON T_Person(FName,FAge) ...
- linux shell学习笔记
一 变量 声明变量: my_var='ddd'使用变量: ${my_var}设置为只读变量: readonly my_var删除变量: unset my_var 注意只读变量不能被删除 变量类型:( ...
- js 更改head的title
使用document.title = "hello"; 不能使用 $("title").text("dd");或者 $ ...
- 4.Mybatis的输入映射(parameterType类型解析)
前面提到过Mybatis可以对输入的参数进行映射,那么现在我们来看一下输入映射,关于输入映射大概可以分为几种情况来学习: 1.基本的类型 2.实体类 3.包装类 1.参数是基本的类型(int,Stri ...
- 邮件江湖群狼环伺 U-Mail邮件系统防狼有术
小时候听过一首儿歌<小兔子乖乖>,里面说到有条恶狼,常常冒充小兔子的“妈妈”,要求小兔 子开门,但小兔子谨守妈妈的训诫,就是不开门,直到辨别出妈妈在窗外的声音,才打开房门.如果我们将一些似 ...
- Action类中获取request等对象的方法
struts2中的action类中,SevletActionContext可以获取
- scala 学习: case class
case class: 1.定义为case class 的类在实例化时,可以不使用new 关键字. case class People(name:String, age:Int) val zhangs ...
- Android消息推送——JPush极光推送
刚看了一篇关于Android消息推送评测总结的博客http://www.cnblogs.com/logan/p/4514635.html: 自己也对原学过的JPush极光进行一下小结,方便后续工作使用 ...
- Python全栈--7.1--字符串的格式化
Python字符串格式化:(百分号/format) 1.百分号的方式: %[(name)][flags][width].[precision]typecode (name) 可选,用于选择指 ...