Poj(3259),SPFA,判负环
题目链接:http://poj.org/problem?id=3259
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 44090 | Accepted: 16203 |
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
Hint
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.
#include <stdio.h>
#include<iostream>
#include <queue>
#include <string.h> using namespace std; #define INF 0x3f3f3f3f int n,m,t; struct Edge
{
int v;
int w;
int next;
} edge[]; int NE = ; int dis[];
int head[];
bool vis[];
int cnt[]; void add(int u,int v,int d)
{
edge[NE].v = v;
edge[NE].w = d;
edge[NE].next = head[u];
head[u]= NE++;
} bool SPFA()
{
for(int i=; i<=n; i++)
{
dis[i] = INF;
vis[i] = false;
cnt[i] = ;
} dis[] = ;
vis[] = true;
cnt[] = ;
queue<int> Q;
Q.push(); while(!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=false;
for(int i=head[u]; i!=-; i=edge[i].next)
{ if(dis[edge[i].v]>dis[u]+edge[i].w)
{
dis[edge[i].v] = dis[u] + edge[i].w;
if(!vis[edge[i].v])
{
vis[edge[i].v] = true;
Q.push(edge[i].v);
if(++cnt[edge[i].v]>n)
return true;
}
}
}
}
return false;
} int main()
{
//freopen("input.txt","r",stdin);
int cases;
scanf("%d",&cases);
while(cases--)
{ NE = ;
memset(head,-,sizeof(head));
scanf("%d%d%d",&n,&m,&t); for(int i=; i<m; i++)
{
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
add(a,b,d);
add(b,a,d);
} for(int i=; i<t; i++)
{
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
add(a,b,-d);
} if(SPFA())
printf("YES\n");
else printf("NO\n");
}
return ;
}
Poj(3259),SPFA,判负环的更多相关文章
- Wormholes POJ - 3259 spfa判断负环
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...
- POJ - 3851-Wormholes(SPFA判负环)
A friend of yours, an inventor, has built a spaceship recently and wants to explore space with it. D ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
- spfa判负环
bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...
- 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)
传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...
- BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划
BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...
- [P1768]天路(分数规划+SPFA判负环)
题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...
随机推荐
- Python学习笔记总结1:字符串表示str与repr的用法比较
参考博文地址:http://blog.csdn.net/yyt8yyt8/article/details/7030416 值转化为字符串常用以下三种方法: 1. str函数 str函数转化为合理形式的 ...
- linux第7天 I/O的五种模型, select
服务器端避免僵尸进程的方法: 1)通过忽略SIGCHLD信号,解决僵尸进程 signal(SIGCHLD, SIG_IGN) 2)通过wait方法,解决僵尸进程 signal(SIGCHLD, han ...
- bzoj4547 小奇的集合
当序列中最大和次大都是负数的时候,其相加会是一个更小的负数,因此答案为(Σai)+(m1+m2)*k,如果最大是正数次大是负数,那么一直相加直到两个数都为正数,当最大和次大都是正数时,做一下矩阵乘法即 ...
- . ToString(),Convert.ToString(),(string),as比较:
http://www.cnblogs.com/chehaoj/archive/2010/02/23/1671955.html 通常 object 到 string 有四种方式(假设有object ob ...
- springmvc+spring+mybatis分页查询实例版本3,添加条件检索
在第二个版本上添加了姓名模糊查询,年龄区间查询;自以为easy,结果发现mybatis的各种参数写法基本搞混或是忘了,zuo啊,直接上代码,然后赶紧把mybatis整理一遍再研究自己的项目,应该还会有 ...
- clock sense和analysis mode
PrimeTime会自动track clock tree中的inverter和buffer,从而得到每个register的clock sense. 如果clock tree中,只有buffer和inv ...
- SSAS更改默认端口号,使用非默认端口号的时候Olap连接字符串的格式
Sql server的Analysis Service服务默认使用的是2382或2383端口,但是实际上我们可以通过配置文件手动更改SSAS使用其它端口号. 修改SSAS使用端口号的方法如下,找到你的 ...
- Android自动化压力测试快速入门教程(图解)——MonkeyRunner
一.MonkeyRunner测试环境配置(转自) 1. android-sdk 下载地址:http://www.android-doc.com/sdk/index.html 下载完成后,只需要解压就 ...
- 不小心rm删除文件怎么办
不小心rm删除文件怎么办 rm 命令的副作用越来越显现.而且rm掉之后的东西想找回来很困难.有2个原则: 1 永远不要在root下操作,尤其是rm命令 2 写一个别名,代替rm 我就是在~/.bash ...
- ORA-000845 与 /dev/shm(tempfs)
MEMORY_TARGET参数在Oracle 11g被引进,主要是用于控制Oracle对于系统内存的使用,首次将SGA与PGA整合到一起实现自动管理.一旦设置了MEMORY_TARGET参数值,Ora ...