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 & ...
随机推荐
- sencha touch 百度地图扩展(2014-12-17)
上个版本http://www.cnblogs.com/mlzs/p/3666466.html,新增了一些功能,修复了一些bug 扩展代码如下: Ext.define('ux.BMap', { alte ...
- cordova(安卓)(腾讯信鸽注册绑定与反绑定) 插件开发
腾讯信鸽快速开发指南 http://developer.xg.qq.com/index.php/Android_SDK%E5%BF%AB%E9%80%9F%E6%8C%87%E5%8D%97 本文参考 ...
- mysql8.0驱动问题
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</a ...
- solus 系统 - 怎么使用中文输入法?
系统默认使用 ibus 输入法框架. 可以安装 ibus-libpinyin $ sudo eopkg install ibus-libpinyin 安装好之后需要初始化 ibus-setup $ i ...
- Office Web Apps安装部署(二)
SharePoint 2013调用Office Web Apps 注意:调用OfficeWebApps的sharepoint应用的身份认证必须是基于声明的身份认证(claims-based authe ...
- 7个简单的Excel技巧,需要的赶紧get起来吧
1.直观数据图形化 2. Ctrl不连续选择 3. Ctrl+A相连文本框全选 4. 格式刷点击 5. SUM函数求和 6. 自动求和.自动求平均值.自动计数 7. 行.列距调节
- ASP.NET MVC 计划任务(不使用外接程序,.net内部机制实现)
在asp.net中要不使用其他插件的情况下只能使用定时器来检查, 并执行任务. 以下讲解步骤: 1. 在Global.asax 文件中作如下修改 1 2 3 4 5 6 7 8 9 10 11 voi ...
- JS图片水印
attendanceClick(userID,headImg,userName,company,scoreNmu) { let base64Image = 'assets/imagesaring.pn ...
- Django 的操作
安装: pip install Django 创建django工程 django-admin startproject mysite python manage.py startapp blog / ...
- 内部排序->交换排序->起泡排序
文字描述 首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序(L.r[1].key>L.r[2].key),则将两个记录交换位置,然后比较第二个记录和第三个记录的关键字.依次类推,直 ...