POJ3259 Wormholes 【spfa判负环】
题目链接:http://poj.org/problem?id=3259
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions:75598 | Accepted: 28136 |
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.
#include<stdio.h>
#include<queue>
#include<string.h>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = ;
const int MAXM = ;
const int inf = 0x3f3f3f3f;
using namespace std; int n, m, k; //n个点 m条双向边 k个虫洞(单向边)
int head[MAXN], cnt;
int vis[MAXN], num[MAXN];//num表示第i个点的入队次数 用来判断负环是否存在
int dis[MAXN], flag;
queue<int> Q; struct Edge
{
int to, next, w;
}edge[ * MAXM]; void add(int a, int b, int c)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].w = c;
edge[cnt].next = head[a];
head[a] = cnt;
} void spfa(int st)
{
while(!Q.empty()) Q.pop();
mem(vis, ), mem(dis, inf), mem(num, );
Q.push(st);
vis[st] = ;
num[st] = ;
dis[st] = ;
while(!Q.empty())
{
int a = Q.front();
Q.pop();
vis[a] = ;
for(int i = head[a]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(dis[to] > dis[a] + edge[i].w)
{
dis[to] = dis[a] + edge[i].w;
if(!vis[to])
{
vis[to] = ;
Q.push(to);
num[to] ++;
if(num[to] > n)
{
flag = ;
break;
}
}
}
}
if(flag)
break;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
cnt = , flag = , mem(head, -);
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i <= m; i ++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
for(int i = ; i <= k; i ++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, -c);
}
spfa();
}
return ;
}
POJ3259
POJ3259 Wormholes 【spfa判负环】的更多相关文章
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- POJ3259 Wormholes(SPFA判断负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- poj3259(spfa判负环)
题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时 ...
- POJ3259 Wormholes —— spfa求负环
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- spfa判负环
bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
随机推荐
- 005_STM32程序移植之_RC522读卡模块
1. 测试环境:STM32C8T6 2. 测试模块:RC522读卡模块 3. 测试接口: RC522读卡模块: VCC------------------3.3V GND--------------- ...
- php大文件上传
PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...
- linux crontab 防止周期内为执行完成重复执行
问题的背景: 我们常常需要通过crontab部署某个脚本运行某些定时任务,但在实际的过程中,一旦处理不好可能导致在同一时刻出现脚本的多个运行副本,比如crontab的调度是每5 分钟运行一次脚本,如果 ...
- mysql建表问题
PUBLIC Stack Overflow Tags Users Jobs TeamsQ&A for workLearn More MySQL error: The maximum col ...
- ERRORS: ?: (corsheaders.E013) Origin '*' in CORS_ORIGIN_WHITELIST is missing scheme or netloc HINT:
报错信息 ERRORS: ?: (corsheaders.E013) Origin '*' in CORS_ORIGIN_WHITELIST is missing scheme or netloc H ...
- 第三章、HTTP报文
1 报文流 HTTP 报文是在 HTTP 应用程序之间发送的数据块.这些数据块以一些文本形式的元信息(meta-information)开头.这些报文在客户端.服务器和代理之间流动.术语“流入”.“流 ...
- JavaEE三大框架的整合
JavaEE三大框架的整合 ...
- ROS Topic 常用指令
rostopic list rosnode list一樣,就是列出目前運行中的topic有哪些. rostopic echo <topic_name> 接下來這個指令比較重要啦,就是去監聽 ...
- 无法访问com.alibaba.fastjson.parser.deserializer.PropertyProcessable
某项目加入了某依赖A,IDEA里编译报了如下错误: 无法访问com.alibaba.fastjson.parser.deserializer.PropertyProcessable 错误代码行为某个使 ...
- Vue插件编写、用法详解(附demo)
Vue插件编写.用法详解(附demo) 1.概述 简单来说,插件就是指对Vue的功能的增强或补充. 比如说,让你在每个单页面的组件里,都可以调用某个方法,或者共享使用某个变量,或者在某个方法之前执行一 ...