Wormholes

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

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: A single integer, F. F farm descriptions follow. 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

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

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 1, FJ cannot travel back in time. 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.
//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 判断负权值回路的更多相关文章

  1. POJ 3259 Wormholes Bellman_ford负权回路

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  2. POJ 3259 Wormholes(负权环路)

    题意: 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单 ...

  3. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  4. poj 3259 (Bellman_Ford判断负环)

    题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...

  5. Wormholes POJ - 3259 spfa判断负环

    //判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...

  6. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  7. POJ 3259 Wormholes(最短路&spfa正权回路)题解

    题意:给你m条路花费时间(双向正权路径),w个虫洞返回时间(单向负权路径),问你他能不能走一圈回到原点之后,时间倒流. 思路:题意有点难看懂,我们建完边之后找一下是否存在负权回路,存在则能,反之不能. ...

  8. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  9. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

随机推荐

  1. App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.-解决办法

    运行环境:Xcode Version 7.3.1 (7D1014) 使用NSURL进行数据请求数据代码: -(NSData *)requestData{ NSURL *url = [NSURL URL ...

  2. 小杨同学git使用记(适合使用过git但是不熟练的童鞋)

    首先声明:这不是一篇git使用手册或者指南,如果要详细的git使用指南,下面是廖雪峰的git教程,可以系统学习廖雪峰的git教程,当然,如果你想马上以一种正确的方式使用git,那么接下来你很快就会学会 ...

  3. JS驗證兩位小數

    function SizeCheck(Textdiv) {                var fg = true;                str = $("#" + T ...

  4. 无法使用内置管理员账户打开Microsoft Edge

    一.以管理员批准模式运行所有管理员 运行"gpedit.msc",打开本地组策略编辑器,然后依次打开"计算机配置→Windows 设置→安全设置→本地策略→安全选项&qu ...

  5. TListView Header重绘和高度设置

    TListView 的 Header 部分默认 BtnFace 颜色,高度也不能改变.我们可以通过编写一些代码来实现这些功能: 获得TListView 的Header 的句柄: TListView的H ...

  6. HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)

    根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...

  7. 深入理解js——函数和对象的关系

    函数也是对象,但是函数却不像数组--数组是对象的一种,它是对象的一个子集.函数和数组之间不是单纯的包含与被包含的关系,它们之间有点像鸡生蛋蛋生鸡的逻辑. 来例子:function Fn(){ this ...

  8. SqlServer2008R2附件数据库失败

    MSSQL附加数据库时提示以下错误: 无法打开物理文件“***.mdf”.操作系统错误 5:“5(拒绝访问.)”. (Microsoft SQL Server,错误: 5120) 该经验介绍如何处理该 ...

  9. Erlang 从入门到精通(一) 下载安装

    我的电脑配置: 系统:win8.1  x64 内存:16G 在官网下载http://www.erlang.org/

  10. CCKJ 笔试

    面向对象三个特性: 面向对象技术是目前流行的系统设计开发技术,它包括面向对象分析和面向对象程序设计.面向对象程序设计技术的提出,主要是为了解决传统程序设计方法--结构化程序设计所不能解决的代码重用问题 ...