http://poj.org/problem?id=3259

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..NM (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, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) 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.
 

题目大意:虫洞问题,现在有n个点,m条边,代表现在可以走的通路,比如从a到b和从b到a需要花费c时间,现在在地上出现了w个虫洞,虫洞的意义就是你从a到b话费的时间是-c(时间倒流,并且虫洞是单向的),现在问你从某个点开始走,能回到从前

解题思路:其实给出了坐标,这个时候就可以构成一张图,然后将回到从前理解为是否会出现负权环,用bellman-ford就可以解出了

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 900001
struct node
{
int u,v,w;
}q[];
int dis[];
int n,m,w1,count=;
int B()
{
int flag=;
for(int i=;i<=n;i++)
dis[i]=N;
dis[]=;
for(int i=;i<=n-;i++)
{
flag=;
for(int j=;j<count;j++)
{
if(dis[q[j].v]>dis[q[j].u]+q[j].w)//这里u,v是不能颠倒的,因为
{
dis[q[j].v]=dis[q[j].u]+q[j].w;
flag=;
}
}
/* for(int j=0;j<n;j++)
printf(".%d",dis[j]);*/
if(!flag) break;
}
for(int i=;i<count;i++)
{
if(dis[q[i].v]>dis[q[i].u]+q[i].w)
return ;
}
return ;
}
int main()
{
int x,y,x1,T;
scanf("%d",&T);
while(T--)
{
count=;
scanf("%d%d%d",&n,&m,&w1);
while(m--)
{
scanf("%d%d%d",&x,&y,&x1);
q[count].u=x;
q[count].v=y;
q[count++].w=x1;
q[count].u=y;
q[count].v=x;
q[count++].w=x1;
}
while(w1--)
{
scanf("%d%d%d",&x,&y,&x1);
q[count].u=x;
q[count].v=y;
q[count++].w=-x1;//他是有方向的
}
int t=B();
if(t==) printf("YES\n");
else printf("NO\n");
}
return ;
}

第二次写的:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 0x7fffffff
using namespace std;
int n,m,k,tt;
struct node
{
int x,y,z;
} q[];
int dis[];
void add(int xx,int yy,int zz)
{
q[tt].x=xx;
q[tt].y=yy;
q[tt++].z=zz;
}
void BF()
{
int flag;
dis[]=;
for(int i=; i<=n; i++)
{
flag=;
for(int i=; i<tt; i++)
{
if(dis[q[i].y]>dis[q[i].x]+q[i].z)
{
dis[q[i].y]=dis[q[i].x]+q[i].z;
flag=;
}
}
if(flag==) break;
}
if(flag==) printf("YES\n");
else printf("NO\n");
}
int main()
{
int T,zz,xx,yy;
cin>>T;
while(T--)
{
cin>>n>>m>>k;
tt=;
for(int i=; i<m; i++)
{
cin>>xx>>yy>>zz;
add(xx,yy,zz);
add(yy,xx,zz);
}
for(int i=; i<k; i++)
{
cin>>xx>>yy>>zz;
add(xx,yy,-zz);
}
BF();
}
return ;
}

poj3259: Wormholes(BF模板题)的更多相关文章

  1. poj3259 Wormholes (判负环)【spfa】(模板)

    <题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...

  2. POJ3259 Wormholes

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

  3. HDU 4347 - The Closest M Points - [KDTree模板题]

    本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...

  4. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  5. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  6. POJ2774 & 后缀数组模板题

    题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...

  7. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  8. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  9. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

随机推荐

  1. kafka+zookeeper环境配置(linux环境单机版)

    版本: CentOS-6.5-x86_64 zookeeper-3.4.6 kafka_2.10-0.10.1.0 一.zookeeper下载与安装 1)下载 $ wget http://mirror ...

  2. smartcrop.js 内容感知图像裁剪

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Devart.Data.Oracle.OracleException: ORA-01480: STR 绑定值的结尾 Null 字符缺失,entity framework

    1. 问题描述 这个问题主要的原因是 使用Devart oracle更新的时候 有中文的话 那就会出这个,其实就是 我们sqlserver 你没有加 N'' 这种的去更新 2. 解决方案 在连接字符串 ...

  4. .NET Core开发日志——结构化日志

    在.NET生态圈中,最早被广泛使用的日志库可能是派生自Java世界里的Apache log4net.而其后来者,莫过于NLog.Nlog与log4net相比,有一项较显著的优势,它支持结构化日志. 结 ...

  5. 通过Docker构建TensorFlow Serving

    最近在用Docker搭建TensorFlow Serving, 在查阅了官方资料后,发现其文档内有不少冗余的步骤,便一步步排查,终于找到了更简单的Docker镜像构建方法.这里有两种方式: 版本一: ...

  6. 命令配置linux分辨率

    1. xrandr 使用该命令列举系统支持的分辨率 2. xrandr -s 回复原来的分辨率 3. xrandr -s 1360x768 设置分辨率   如果分辨率没能锁定,请在根目录使用gedit ...

  7. EF Code First模型约束

    总之,EF比较复杂.如果不想深究,建议简单用用.基本对应就行,大项目标准开发还是ModelFirst(先建立DB各种约束),然后再c#类约束.定义. 当然写原型时用ef很快.

  8. scala-高阶函数

    //1类似于lambda表达式的函数直接量====================== var get = (name: String) => { println(123 + name) } g ...

  9. activeMQ配置文件

    <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agree ...

  10. Java List的分段操作

    实现思路: list按sublist的大小截成一定的份数,然后放到一个数组里面, 一下是一个demo例子: public List<List<String>> getSubLi ...