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.

题目大意就是:农夫约翰有F个农场,每个农场有N块地,其间有M条路(无向),W条时光隧道(有向且时间倒流即:权值为负)。问是否可能回到过去?

经典的bellman_Ford理解题,不知道的可以去百度!

//Asimple
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#include <time.h>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
typedef long long ll;
int n, m, num, T, k, x, y, len;
int Map[maxn][maxn];
int dis[maxn];
typedef struct node {
int begin;
int end;
int weight;
node(){}
node(int begin, int end, int weight) {
this->begin = begin;
this->end = end;
this->weight = weight;
}
}eee;
eee edg[maxn];
//Bellman-Ford算法:求含负权图的单源最短路径算法
//单源最短路径(从源点s到其它所有顶点v)
bool bellmanFord() {
memset(dis, , sizeof(dis));
for(int i=; i<n; i++) {
for(int j=; j<len; j++) {
eee e = node(edg[j].begin, edg[j].end, edg[j].weight);
if( dis[e.end] > dis[e.begin] + e.weight) {
dis[e.end] = dis[e.begin] + e.weight;
if( i == n- ) return true;
}
}
}
return false;
} void input() {
cin >> T ;
while( T -- ) {
cin >> n >> m >> k;
len = ;
for(int i=; i<m; i++) {
cin >> x >> y >> num;
edg[len].begin = x;
edg[len].end = y;
edg[len].weight = num;
len ++;
edg[len].begin = y;
edg[len].end = x;
edg[len].weight = num;
len ++;
}
for(int i=; i<k; i++) {
cin >> x >> y >> num ;
edg[len].begin = x;
edg[len].end = y;
edg[len].weight = -num;
len ++;
}
if( bellmanFord() ) cout << "YES" << endl;
else cout << "NO" << endl;
}
} int main(){
input();
return ;
}

2017-5-26  修改:

自己写了一个邻接矩阵的SPFA解法

坑点:可能会出现重复的路径,这个时候需要取小值。

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = +;
const int INF = ( << );
int n, m, x, y, num, T, k;
int Map[maxn][maxn], dis[maxn], c[maxn]; void init(){
for(int i=; i<=n; i++) {
dis[i] = INF;
c[i] = ;
for(int j=; j<=n; j++) {
Map[i][j] = INF;
}
}
} bool spfa(){
bool vis[maxn];
queue<int> q;
memset(vis, false, sizeof(vis));
q.push();
vis[] = true;
c[] = ;
dis[] = ;
while( !q.empty() ) {
x = q.front();q.pop();
vis[x] = false;
for(int i=; i<=n; i++) {
if( dis[i]>dis[x]+Map[x][i] ) {
dis[i] = dis[x]+Map[x][i];
if( !vis[i] ) {
vis[i] = true;
c[i] ++;
if( c[i]>=n ) return true;
q.push(i);
}
}
}
}
return false;
} int main(){
cin >> T;
while( T -- ) {
cin >> n >> m >> k;
init();
while( m -- ) {
cin >> x >> y >> num;
Map[x][y] = min(Map[x][y], num);
Map[y][x] = Map[x][y];
}
while( k -- ) {
cin >> x >> y >> num;
Map[x][y] = min(Map[x][y], -num);
}
if( spfa() ) cout << "YES" << endl;
else cout << "NO" << endl;
}
return ;
}

Wormholes的更多相关文章

  1. [题解]USACO 1.3 Wormholes

    Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...

  2. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

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

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

  4. poj 3259 Wormholes 判断负权值回路

    Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java ...

  5. Wormholes(Bellman-ford)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33008   Accepted: 12011 Descr ...

  6. poj3259 bellman——ford Wormholes解绝负权问题

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35103   Accepted: 12805 Descr ...

  7. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  8. Wormholes 分类: POJ 2015-07-14 20:21 21人阅读 评论(0) 收藏

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35235   Accepted: 12861 Descr ...

  9. POJ 3259 Wormholes (Bellman_ford算法)

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

随机推荐

  1. 快速安装zabbix agent并部署监控

    1.准备yum源: epel源:yum install -y zabbix22-agent 2.上传脚本: 上传脚本事先写好的监控脚本到/script/下面 3.修改配置文件:Server=10.10 ...

  2. 几何不能具有Z值

    ArcEngine 复制要素Geometry时,产生 几何不能具有Z值 的异常 解决方法:http://forums.esri.com/Thread.asp?c=159&f=1707& ...

  3. 解析私有IP地址和公网IP地址

    局域网私有IP地址上外网的原理 IP地址分为两部分,网络号和主机号,这种分法应用在私有和公有IP地址上.一个局域网中,为了该局域网的安全,我们应用了私有IP地址,为了和Internet中的其他主机进行 ...

  4. 字典树(Trie Tree)

    在图示中,键标注在节点中,值标注在节点之下.每一个完整的英文单词对应一个特定的整数.Trie 可以看作是一个确定有限状态自动机,尽管边上的符号一般是隐含在分支的顺序中的.键不需要被显式地保存在节点中. ...

  5. 微信小程序学习

    官方网站 https://mp.weixin.qq.com/debug/wxadoc/dev/index.html 项目结构介绍 -- MINA框架 https://mp.weixin.qq.com/ ...

  6. 怎么配置Java环境变量?

    右键计算机 -> 属性 -> 高级系统设置 -> 环境变量,   在系统环境变量添加以下三条变量. 1. PATH, 配置JDK命令文件的位置. 输入“%JAVA_HOME%\bin ...

  7. ThinkPHP 3.2.3 关联模型的使用

    关于关联模型 ThinkPHP 3.2.3 的关联模型(手册地址)一般处理关联数据表的 CURD 操作,例如关联读取.关联写入.关联删除等. 实例 博客管理模块关于博客有 4 张数据表:博客表 crm ...

  8. Redis和Memcache对比及选择(转载)

  9. Eclipse Android开发环境搭建

    要点: 1)已经安装配置好eclipse和jdk环境 2)给eclipse安装ADT插件(支持android开发) 3)安装配置android sdk环境(类似jdk) 4)eclipse中配置and ...

  10. mysql5.5 修改字符集

    对于使用者来说,一般推荐使用utf8编码来存储数据.而要解决乱码问题,不单单是MySQL数据的存储问题,还和用户的程序文件的编码方式.用户程序和MySQL数据库的连接方式都有关系. 首先,MySQL有 ...