poj3259: Wormholes(BF模板题)
http://poj.org/problem?id=3259
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.
题目大意:虫洞问题,现在有n个点,m条边,代表现在可以走的通路,比如从a到b和从b到a需要花费c时间,现在在地上出现了w个虫洞,虫洞的意义就是你从a到b话费的时间是-c(时间倒流,并且虫洞是单向的),现在问你从某个点开始走,能回到从前
解题思路:其实给出了坐标,这个时候就可以构成一张图,然后将回到从前理解为是否会出现负权环,用bellman-ford就可以解出了
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 900001
struct node
{
int u,v,w;
}q[];
int dis[];
int n,m,w1,count=;
int B()
{
int flag=;
for(int i=;i<=n;i++)
dis[i]=N;
dis[]=;
for(int i=;i<=n-;i++)
{
flag=;
for(int j=;j<count;j++)
{
if(dis[q[j].v]>dis[q[j].u]+q[j].w)//这里u,v是不能颠倒的,因为
{
dis[q[j].v]=dis[q[j].u]+q[j].w;
flag=;
}
}
/* for(int j=0;j<n;j++)
printf(".%d",dis[j]);*/
if(!flag) break;
}
for(int i=;i<count;i++)
{
if(dis[q[i].v]>dis[q[i].u]+q[i].w)
return ;
}
return ;
}
int main()
{
int x,y,x1,T;
scanf("%d",&T);
while(T--)
{
count=;
scanf("%d%d%d",&n,&m,&w1);
while(m--)
{
scanf("%d%d%d",&x,&y,&x1);
q[count].u=x;
q[count].v=y;
q[count++].w=x1;
q[count].u=y;
q[count].v=x;
q[count++].w=x1;
}
while(w1--)
{
scanf("%d%d%d",&x,&y,&x1);
q[count].u=x;
q[count].v=y;
q[count++].w=-x1;//他是有方向的
}
int t=B();
if(t==) printf("YES\n");
else printf("NO\n");
}
return ;
}
第二次写的:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 0x7fffffff
using namespace std;
int n,m,k,tt;
struct node
{
int x,y,z;
} q[];
int dis[];
void add(int xx,int yy,int zz)
{
q[tt].x=xx;
q[tt].y=yy;
q[tt++].z=zz;
}
void BF()
{
int flag;
dis[]=;
for(int i=; i<=n; i++)
{
flag=;
for(int i=; i<tt; i++)
{
if(dis[q[i].y]>dis[q[i].x]+q[i].z)
{
dis[q[i].y]=dis[q[i].x]+q[i].z;
flag=;
}
}
if(flag==) break;
}
if(flag==) printf("YES\n");
else printf("NO\n");
}
int main()
{
int T,zz,xx,yy;
cin>>T;
while(T--)
{
cin>>n>>m>>k;
tt=;
for(int i=; i<m; i++)
{
cin>>xx>>yy>>zz;
add(xx,yy,zz);
add(yy,xx,zz);
}
for(int i=; i<k; i++)
{
cin>>xx>>yy>>zz;
add(xx,yy,-zz);
}
BF();
}
return ;
}
poj3259: Wormholes(BF模板题)的更多相关文章
- poj3259 Wormholes (判负环)【spfa】(模板)
<题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...
- POJ3259 Wormholes
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- HDU 4347 - The Closest M Points - [KDTree模板题]
本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...
- [AHOI 2009] 维护序列(线段树模板题)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- POJ2774 & 后缀数组模板题
题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- HDU-3549 最大流模板题
1.HDU-3549 Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
随机推荐
- Python零基础入门学习 作者:小甲鱼
temp = input('不妨想一想小甲鱼现在心里想的哪一个数字:') guess = int(temp) if guess == 8: print('你是小甲鱼心里的蛔虫吗?') print('哼 ...
- G - SDOI
The Annual National Olympic of Information(NOI) will be held.The province of Shandong hold a Select( ...
- Slapper帮助Dapper实现一对多
Dapper的Query的方法提供了多个泛型重载可以帮助我们实现导航属性的查询 1对1 public class Employees4List { public int Id { get; set; ...
- Saltstack设置安装源为阿里源
Saltstack设置安装源为官方源有时候在国内网络不好安装较慢或者安装不上,可设置为阿里源 比如对于 Centos 7 系统,在 saltstack 的官网提供的配置初始化手册是: sudo yum ...
- Ajax框架---dwr的用法
通常使用Ajax时用的都是jQuery框架,现在公司的框架里用的都是dwr.我觉得dwr和jQuery中的ajax用法差不多,看起来也很像. 一.简介 百度百科上对dwr的描述: DWR采取了一个类似 ...
- 洛谷P1141 01迷宫【bfs】
题目链接:https://www.luogu.org/problemnew/show/P1141 题意: 有一个填了0和1的n*n的格子,只能0走到1,1走到0 有m组询问(数据量是1e5),问某一个 ...
- vim中的ctrl+s导致的“假死”、无响应、不接受输入
有时候vim看到vim的光标在闪烁,但无法输入任何东西,最后只好结束终端了事. 这种现象,是windows用户在使用vim时经常犯的“错误”.在windows下,为了保护自己的劳动成果,ctrl+s已 ...
- springboot程序无法访问静态资源
今天开发遇到了一个很奇葩的错误,再spngboot程序成功运行后发现无法访问再resouces/static下的静态资源,通过rul访问总是404,原因最终锁定在某配置类的一个标签上: @Enable ...
- Vue SSR 配合Java的Javascript引擎j2v8实现服务端渲染3配置webpack支持ssr
安装 cross-env yarn add -D cross-env 安装 html-webpack-plugin yarn add -D html-webpack-plugin 安装 webpack ...
- piano class 12
1,不要记谱子,眼睛要一直看着谱子,手指凭感觉找琴键 2,弹的时候一定要按照谱子上标出来的指法弹奏,很重要 3,两只手要会跷跷板弹奏 4,八分音符,一般第二个会比第一个弱一点,但是要看自己感觉 5,慢 ...