POJ3259(ford判环)
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..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 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
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判环)的更多相关文章
- POJ1860(ford判环)
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24243 Accepted: 881 ...
- hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)
这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...
- hdu4888 Redraw Beautiful Drawings 最大流+判环
hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/6553 ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- Dwarves (有向图判环)
Dwarves 时间限制: 1 Sec 内存限制: 64 MB提交: 14 解决: 4[提交][状态][讨论版] 题目描述 Once upon a time, there arose a huge ...
- COJ 3012 LZJ的问题 (有向图判环)
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1042 试题描述: LZJ有一个问题想问问大家.他在写函数时有时候很头疼,如 ...
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
- E - Andrew and Taxi-二分答案-topo判环
E - Andrew and Taxi 思路 :min max 明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存 ...
随机推荐
- 【puppeteer+Node.js】学习
总结了一下有关puppeteer的学习的网站,以后还会继续更新 puppeteer 介绍 Puppeteer是一个通过DevTools Protocol控制headless chromium的高级no ...
- 不同特权级间代码段的跳转{ 门 + 跳转(jmp + call) + 返回(ret) }
[0]写在前面 0.1)我们讲 CPU的保护机制,它是可靠的多任务运行环境所必须的: 0.2) CPU保护机制:分为段级保护 + 页级保护: 0.2.1)段级保护分为:段限长 limit 检查.段类型 ...
- 华为基于策略划分VLAN的配置方法及示例
学过思科交换机的朋友,可能对基于策略划分VLAN的配置方法印象非常深,感觉确实比较复杂,先要配置VMPS以及VMPS数据库,但在华为交换机中,这种现象得到了彻底改变,因为它有了一种特殊的端口类型—— ...
- linux 改动rootpassword以及忘记rootpassword
改动rootpassword: $ passwd root 或者sudo passwd root $password: (要求输入旧的密码) $new password:(输入两遍新密码) 忘记r ...
- 3720: Gty的妹子树
3720: Gty的妹子树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1440 Solved: 482[Submit][Status][Disc ...
- React-Native开源项目学习
https://github.com/liuhongjun719/react-native-DaidaiHelperNew 借贷助手https://github.com/liuhongjun719/r ...
- Java for LeetCode 112 Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 编写你的第一个web应用程序1
在shell中运行以下命令来检查django是否已安装及其版本 python -m django --version 如果django已经安装,你应该看到安装的版本号,如果还没有安装,你会看到一个‘n ...
- PAT 甲级 1116. Come on! Let's C (20) 【循环判断】
题目链接 https://www.patest.cn/contests/pat-a-practise/1116 思路 注意一个细节 如果没有本来 ID 的 后来又查了这个ID 不是输出 checked ...
- HTML5响应式导航
HTML5响应式导航HTML5,响应式,jQuery特效,HTML5导航,HTML5响应式导航是一款基于HTML5实现的深灰色响应式导航菜单. 地址:http://www.huiyi8.com/sc/ ...