poj 3259 Wormholes 【SPFA&&推断负环】
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 36852 | Accepted: 13502 |
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
分析:
英语不好。就不在翻译了。意思就是求最短路。假设最短路中有负环,输出yes,没有输出no。
代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define maxn 10010
using namespace std; int N,M,W;
int pnum;
int dis[maxn];
int vis[maxn];
int head[maxn];
int used[maxn];
bool cnt;
struct node{
int from;
int to;
int val;
int next;
};
node pp[2*maxn]; void addpp(int u,int v,int w)
{
node E ={u,v,w,head[u]};
pp[pnum]=E;
head[u]=pnum++;
} void init()
{
int a,b,c;
while(M--)
{
scanf("%d%d%d",&a,&b,&c);
addpp(a,b,c);
addpp(b,a,c);
}
} void gmap()
{
int a,b,c;
while(W--)
{
scanf("%d%d%d",&a,&b,&c);
addpp(a,b,-c);
}
} void SPFA(int s)
{
queue<int>q;
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
dis[s]=0;
vis[s]=1;
used[s]++;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=pp[i].next)
{
int v=pp[i].to;
if(dis[v]>dis[u]+pp[i].val)
{
dis[v]=dis[u]+pp[i].val;
if(!vis[v])
{ vis[v]=1;
q.push(v);
used[v]++;
if(used[v]>N)
{
cnt=true;
return;
}
}
}
}
} } int main(){
int T;
scanf("%d",&T);
while(T--)
{
pnum=0;
cnt=false;
memset(head,-1,sizeof(head));
scanf("%d%d%d",&N,&M,&W);
init();
gmap();
SPFA(1);
if(cnt)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
poj 3259 Wormholes 【SPFA&&推断负环】的更多相关文章
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 49962 Accepted: 18421 Descr ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes SPFA算法题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- uva558 Wormholes SPFA 求是否存在负环
J - Wormholes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ...
- poj 3259 Wormholes spfa算法
点击打开链接 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25582 Accepted: 9186 ...
随机推荐
- 寒武纪芯片——有自己的SDK,支持tf、caffe、MXNet
寒武纪芯片 产品中心>智能处理器IP 智能处理器IP MLU智能芯片 软件开发环境 Cambricon-1A 高性能硬件架构及软件支持兼容Caffe.Tensorflow.MXnet等主流AI开 ...
- Scrapy中的核心工作流程以及POST请求
五大核心组件工作流程 post请求发送 递归爬取 五大核心组件工作流程 引擎(Scrapy)用来处理整个系统的数据流处理, 触发事务(框架核心) 调度器(Scheduler)用来接受引擎发过来的请求, ...
- 如何正确产看API
看API时,先看的它的父接口自接口,及其相关的抽象类和子类 看完后,看概述的第一段话就行,后面的不用看. 再看构造方法,并到底层去看构造方法里参数的具体含义. 最后,再将包含的方法一个个进行测试. 解 ...
- QlikSense移动端使用攻略
公司内部署QlikSense服务器,除了在电脑上用浏览器访问,也可以在移动端进行访问. 移动端访问在如下网址有英文详细介绍:https://community.qlik.com/docs/DOC-19 ...
- 利用JavaScript实现文本框改文字功能
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- Android图片剪裁库
最近利用一周左右的业余时间,终于完成了一个Android图片剪裁库,核心功能是根据自己的理解实现的,部分代码参考了Android源码的图片剪裁应用.现在将该代码开源在Github上以供大家学习和使用, ...
- 使用光盘作为yum源安装ifconfig等网络命令
# mkdir -p /mnt/cdrom# 如果是光驱:mount -t iso9660 /dev/cdrom /mnt/cdrom/# 如果是ISO:mount -o loop /usr/loca ...
- 修复lsp,360浏览器可以上网其它软件不行
netsh winsock reset netsh int ip reset 重启电脑
- 数据的图表统计highcharts
数据统计常用的图表一般是饼状图.柱状图.线状图,HighCharts可以很好的实现. HighCharts highcharts是基于jquery的一个功能强大的插件,使用时先导入jquery.js ...
- HttpServletRequest二三事
缘由 在项目中,闲来无聊写了个bug LOGGER.info("前端请求,request:{}",JSON.toJSONString(request)); 好像还好是吧,来我告诉你 ...