Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 39078   Accepted: 14369

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 题意:每个农场有N各区域,连接所有区域的是M个双向路径和W个单向时空隧道,从S->E若为路径则花费T秒,若为时空隧道则倒退T秒。问是否可以从某点出发,转一圈回来,回到出发时刻之前。
思路:因为时空隧道实现倒退,所以将其权值设为负值,利用ford判断是否存在负环。
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int from,to,cost;
}es[MAXN];
int N,M,W;
int E;
int d[MAXN];
bool ford(int s)
{
for(int i=;i<=N;i++) d[i]=INF;
d[s]=; int n=N;
while(n--)
{
bool update=false;
for(int i=;i<E;i++)
{
Edge e=es[i];
if(d[e.from]!=INF&&d[e.to]>d[e.from]+e.cost)
{
d[e.to]=d[e.from]+e.cost;
update=true;
}
}
if(!update) break; } if(n==-) return true;
else return false;
}
int main()
{
int F;
scanf("%d",&F);
while(F--)
{
E=;
scanf("%d%d%d",&N,&M,&W);
for(int i=;i<M;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
es[E].from=u,es[E].to=v,es[E++].cost=c;
es[E].from=v,es[E].to=u,es[E++].cost=c;
}
for(int i=;i<W;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
es[E].from=u,es[E].to=v,es[E++].cost=-c;//倒退c秒
} if(ford()) printf("YES\n");
else printf("NO\n");
} return ;
}

spfa+前向星可解决重边问题

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
struct Edge{
int v,w,next;
}es[];
int head[MAXN],tot;
void addedge(int u,int v,int w)
{
es[tot].v=v;
es[tot].w=w;
es[tot].next=head[u];
head[u]=tot++;
}
int d[MAXN],vis[MAXN],cnt[MAXN];
int n,m,k;
bool spfa(int s)
{
for(int i=;i<=n;i++)
{
d[i]=INF;
vis[i]=;
cnt[i]=;
}
d[s]=;
queue<int> que;
que.push(s);
vis[s]=;
cnt[s]++;
while(!que.empty())
{
int u=que.front();que.pop();
vis[u]=;
for(int i=head[u];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.v]>d[u]+e.w)
{
d[e.v]=d[u]+e.w;
if(!vis[e.v])
{
vis[e.v]=;
que.push(e.v);
cnt[e.v]++;
if(cnt[e.v]>=n) return true;
}
}
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(head,-,sizeof(head));
tot=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
for(int i=;i<k;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,-w);
}
if(spfa()) printf("YES\n");
else printf("NO\n");
}
return ;
}

POJ3259(ford判环)的更多相关文章

  1. POJ1860(ford判环)

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24243   Accepted: 881 ...

  2. hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)

    这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...

  3. hdu4888 Redraw Beautiful Drawings 最大流+判环

    hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/6553 ...

  4. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

    分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...

  5. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  6. Dwarves (有向图判环)

    Dwarves 时间限制: 1 Sec  内存限制: 64 MB提交: 14  解决: 4[提交][状态][讨论版] 题目描述 Once upon a time, there arose a huge ...

  7. COJ 3012 LZJ的问题 (有向图判环)

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1042 试题描述: LZJ有一个问题想问问大家.他在写函数时有时候很头疼,如 ...

  8. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  9. E - Andrew and Taxi-二分答案-topo判环

    E - Andrew and Taxi 思路 :min max   明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存 ...

随机推荐

  1. FreeRTOS在神舟IV号开发板的应用demo

    下面一个可以直接编译运行的例子,FreeRTOS的版本是V7.1.0,芯片是STM32F107VCT6,使用的开发环境是Keil uVision5. 这里例子创建了四个任务,每个任务控制一个LED的亮 ...

  2. Inno Setup 使用笔记

    使 用 笔 记https://blog.csdn.net/dongshibo12/article/details/79095971 1.Inno Setup 是什么?Inno Setup 是一个免费的 ...

  3. JS加水印遮罩

    <%@ page language="java" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC & ...

  4. 如何理解API,API 是如何工作的

    大神博客:https://blog.csdn.net/cumtdeyurenjie/article/details/80211896

  5. kindeditor浏览器兼容性问题

    1.kindeditor在IE下出现异常“对象不支持“attachEvent”属性或方法” 通过开发人员工具会发现: 这时问题就很明了,也就是IE11版本不支持“attachEvent”; 解决方案: ...

  6. ArcGIS api for js OverviewMap(鹰眼/概览图)

    说明.本篇博客中主要介绍 地图显示在某个div情况 1.运行效果 2.HTML <!DOCTYPE html> <html> <head> <meta htt ...

  7. 使用Retrofit发送POST请求提交JSON数据

    Retrofit官网:https://square.github.io/retrofit/ 示例如下 HttpService.java import okhttp3.RequestBody; impo ...

  8. 【题解】P3599 Koishi Loves Construction

    [题解]P3599 Koishi Loves Construction \(\mod n\) 考虑如何构造,发现\(n\)一定在第一位,不然不行.\(n\)一定是偶数或者是\(1\),不然 \(n|\ ...

  9. VM tools安装错误The path "" is not a valid path to the xx generic kernel headers.

    VMWARE TOOLS安装提示THE PATH IS NOT A VALID PATH TO THE GENERIC KERNEL HEADERSI solved this problem, I g ...

  10. Ruby JSON操作

      解析来我们就可以使用以下命令来安装Ruby JSON 模块: ? 1 $gem install json 使用 Ruby 解析 JSON 以下为JSON数据,将该数据存储在 input.json ...