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. 深入Asyncio(九)异步生成器

    Async Generators:yield inside async def functions 如果在async def中使用yield会发生什么,答案就是生成一个异步生成器函数,如果有生成器.协 ...

  2. 【BZOJ4281】[ONTAK2015]Związek Harcerstwa Bajtockiego LCA

    [BZOJ4281][ONTAK2015]Związek Harcerstwa Bajtockiego Description 给定一棵有n个点的无根树,相邻的点之间的距离为1,一开始你位于m点.之后 ...

  3. 远程访问(post 传参数) 以及IOUtils复制文件

    package com.action; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream ...

  4. Spring和ActiveMQ整合的完整实例

     Spring和ActiveMQ整合的完整实例 前言 这篇博文,我们基于Spring+JMS+ActiveMQ+Tomcat,做一个Spring4.1.0和ActiveMQ5.11.1整合实例,实现了 ...

  5. 该 Bucket 已存在,或被其他用户占用

  6. T_CODE I18N

    关于T-CODE I18N 最近由于看到很多人遇到SMARTFORMS不能拖拽字段的问题,这个的解决方案 I18N:解决SMARTFORMS的不能从Field name 那边直接把变量拖入右边编辑框 ...

  7. ubuntu mysql 配置(远程访问&&字符集设置&&忽略大小写)

    1.安装 参考http://www.cnblogs.com/wuhou/archive/2008/09/28/1301071.html sudo apt-get install mysql-serve ...

  8. Python爬虫 —— 抓取美女图片(Scrapy篇)

    杂谈: 之前用requests模块爬取了美女图片,今天用scrapy框架实现了一遍. (图片尺度确实大了点,但老衲早已无恋红尘,权当观赏哈哈哈) Item: # -*- coding: utf-8 - ...

  9. 【html学习整理】meta,img,表格,表单

    meta标签: 作用: 给搜索引擎用 . 告诉浏览器是什么编码 <meta charset="UTF-8"> <meta name="keywords& ...

  10. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...