Wormholes POJ 3259(SPFA判负环)
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
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
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 0x3f3f3f3f
#define maxs 10010
using namespace std;
struct node
{
int to;
int w;
} t;
vector<node>Map[maxs];
int dis[maxs];
int vis[maxs];
int n,m,k;
void add(int s,int e,int w)
{
t.to=e;
t.w=w;
Map[s].push_back(t);
}
void init()
{
int i;
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
{
Map[i].clear();
}
}
int SPFA(int s)
{
int cnt[maxs]= {};///记录每个点入队次数
int i;
dis[s]=;
vis[s]=;
queue<int>q;
q.push(s);
cnt[s]++;
while(!q.empty())
{
int u=q.front();
/*if(cnt[u]>=n)
{
return 0;///这里用来判断入队列是否超过N次
}*/
q.pop();
vis[u]=;
for(i=; i<Map[u].size(); i++)
{
int to=Map[u][i].to;
if(dis[to]>dis[u]+Map[u][i].w)
{
dis[to]=dis[u]+Map[u][i].w;///松弛操作
if(dis[]<)///这种判断是否有负环的更快。如果起始点的dis[s]<0说明存在负环
{
return ;
}
if(!vis[to])
{
vis[to]=;
q.push(to);
cnt[to]++;
}
}
}
}
return ;
}
int main()
{
int t,i,j,u,v,w;
scanf("%d",&t);
for(i=; i<=t; i++)
{
scanf("%d%d%d",&n,&m,&k);
init();
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
while(k--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,-w);///虫洞时间取负值
}
if(SPFA())
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
return ;
}
这里再附上使用邻接矩阵和Bellman-Ford算法的代码。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
struct Edge
{
int f;///起点
int t;///终点
int c;///距离
} edge[];
int dist[];
int n,m,h,cnt;
int bellman_ford()
{
memset(dist,inf,sizeof(dist));
dist[]=;///起点
int i,j;
for(j=; j<=n; j++)
{
for(i=; i<=cnt; i++)
{
if(dist[edge[i].t]>dist[edge[i].f]+edge[i].c)///松弛操作
{
dist[edge[i].t]=dist[edge[i].f]+edge[i].c;
}
}
}
int flag=;
for(i=; i<=cnt; i++)///遍历所有的边
{
if(dist[edge[i].t]>dist[edge[i].f]+edge[i].c)
{
flag=;///存在从源点可达的负权回路
break;
}
}
return flag;
}
int main()
{
int i,t;
scanf("%d",&t);
while(t--)
{
cnt=;
scanf("%d %d %d",&n,&m,&h);
int u,v,w;
for(i=; i<=m; i++)
{
cnt++;
scanf("%d %d %d",&u,&v,&w);
edge[cnt].f=u;
edge[cnt].t=v;
edge[cnt].c=w;
cnt++;
edge[cnt].f=v;
edge[cnt].t=u;
edge[cnt].c=w;
}
for(i=; i<=h; i++)
{
cnt++;
scanf("%d %d %d",&u,&v,&w);
edge[cnt].f=u;
edge[cnt].t=v;
edge[cnt].c=-w;
}
if(bellman_ford())
printf("NO\n");
else
printf("YES\n");
}
return ;
}
Wormholes POJ 3259(SPFA判负环)的更多相关文章
- Wormholes POJ - 3259 spfa判断负环
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】
tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstrin ...
- 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 ...
- 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 ...
- 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)
传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...
随机推荐
- 实施erp的建议
纺织行业实施ERP建议 (一)企业各层面应提高对ERP的认识 ERP项目的实施范围横跨企业的每一个部门,在实施过程中需要调动各个部门的资源,这首先需要企业领导者高度重视,从实施的各个环节给予支持:其次 ...
- Scala相关笔记
一.Scala概述以及安装 1. 什么是Scala Scala 是一种多范式的编程语言,其设计的初衷是要集成面向对象编程和函数式编程的各种特性.Scala 运行于 Java 平台(Java 虚拟机 ...
- Django DTL 加减乘除求余
django模板只提供了加法的filter,没有提供专门的乘法和除法运算: django提供了widthratio的tag用来计算比率,可以变相用于乘法和除法的计算. 加法 {{value|add:1 ...
- PHP-掌握基本的分布式架构思想
虽然说写PHP目前都是接触的业务代码,发现写久了,也要熟悉相应的架构 在高并发,高可用的系统下,都是使用高性能的分布式架构,最近在学习相关知识 分享一张图片: 欢迎关注公众号[phper的进阶之路], ...
- PHP 变量分页标签页面源代码技术分享
最近在研究PHP的常规变量的分页源代码. 现在发布一个给大家看一下. defined('IN_DUOAO') or exit('No permission resources.');$smarty ...
- Windos10 mysql-8.0.13安装手顺
一.下载 1.1 官方下载地址:https://dev.mysql.com/downloads/mysql/ ,点击Download 1.2 点击 No thanks,just start my do ...
- Go语言中结构体的使用-第1部分结构体
1 概述 结构体是由成员构成的复合类型.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性.结构体成员,也可称之为成员变量,字段,属性.属性要满足唯一性.结构体的概念在软件工程上 ...
- 20155235 2016-2017-2《Java程序设计》课程总结
每周作业链接汇总 预备作业一:学期前作业 预备作业二:技能获取与C语言学习情况 预备作业三:安装虚拟机与Linux的学习 第一周作业:20155235 2006-2007-2 <Java程序设计 ...
- 20155305《信息安全系统设计基础》10月18日课堂 fork,exic,wait
20155305<信息安全系统设计基础>10月18日课堂 fork,exic,wait fork()函数 1.fork函数作用 一般来讲, 我们编写1个普通的c程序, 运行这个程序直到程序 ...
- 342. Power of Four(One-line)
342. Power of Four Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...