Wormholes 分类: POJ 2015-07-14 20:21 21人阅读 评论(0) 收藏
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 35235 | Accepted: 12861 |
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
有一些道路和虫洞,经过道路需要一定的时间,穿过虫洞可以会回到之前的时间,问是不是可以回到进入之前的时间
就是判断有没有负环SPFA+前向星 算法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#define exp 1e-9
#define INF 0x3f3f3f3f using namespace std;
const int Max=550;
struct node
{
int v;
int w;
int next; } Map[Max],Head[10000];
int Dis[Max];
bool vis[Max];
int path[Max];
int Du[Max];
int n,m,w;
int top;
bool SPFA(int star)
{
queue<int >Q;
memset(Dis,INF,sizeof(Dis));
memset(vis,false,sizeof(vis));
memset(Du,0,sizeof(Du));
memset(path,1,sizeof(path));
Q.push(star);
vis[star]=true;
Du[star]++;
Dis[star]=0;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
if(Du[u]>n)
return true;
vis[u]=false;
int p=Map[u].next;
while(p!=-1)
{
if(Dis[Head[p].v]>Dis[u]+Head[p].w)
{
Dis[Head[p].v]=Dis[u]+Head[p].w;
if(!vis[Head[p].v])
{
Q.push(Head[p].v);
Du[Head[p].v]++;
vis[Head[p].v]=true;
}
}
p=Head[p].next;
}
}
return false;
}
void Creat(int u,int v,int ww)
{
Head[top].next=Map[u].next;
Map[u].next=top;
Head[top].v=v;
Head[top].w=ww;
top++;
} int main()
{
int T;
int u,v,ww;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d",&n,&m,&w);
top=0;
for(int i=0; i<=n; i++)
{
Map[i].next=-1;
}
for(int i=0; i<m; i++)
{
scanf("%d %d %d",&u,&v,&ww);
Creat(u,v,ww);
Creat(v,u,ww);
}
for(int i=0; i<w; i++)
{
scanf("%d %d %d",&u,&v,&ww);
Creat(u,v,-ww); }
if(SPFA(1))
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Wormholes 分类: POJ 2015-07-14 20:21 21人阅读 评论(0) 收藏的更多相关文章
- hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏
hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...
- 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...
- Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode 2014-11-13 20:48 81人阅读 评论(0) 收藏
#include<iostream> #include<stack> #include<stdio.h> using namespace std; struct n ...
- Dijkstra with priority queue 分类: ACM TYPE 2015-07-23 20:12 4人阅读 评论(0) 收藏
POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra) //================================================= ...
- NYOJ 119 士兵杀敌(三)【ST算法】 分类: Brush Mode 2014-11-13 20:56 101人阅读 评论(0) 收藏
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=119 解题思路: RMQ算法. 不会的可以去看看我总结的RMQ算法. http://blo ...
- bzoj 1041 圆上的整点 分类: Brush Mode 2014-11-11 20:15 80人阅读 评论(0) 收藏
这里先只考虑x,y都大于0的情况 如果x^2+y^2=r^2,则(r-x)(r+x)=y*y 令d=gcd(r-x,r+x),r-x=d*u^2,r+x=d*v^2,显然有gcd(u,v)=1且u&l ...
- HTTP 错误 500.19- Internal Server Error 错误解决方法 分类: Windows服务器配置 2015-01-08 20:16 131人阅读 评论(0) 收藏
1.第一种情况如下: 解决方法如下: 经过检查发现是由于先安装Framework组件,后安装iis的缘故,只需重新注册下Framework就可以了,具体步骤如下 1 打开运行,输入cmd进入到命令提示 ...
- winform Execl数据 导入到数据库(SQL) 分类: WinForm C# 2014-05-09 20:52 191人阅读 评论(0) 收藏
首先,看一下我的窗体设计: 要插入的Excel表: 编码 名称 联系人 电话 省市 备注 100 100线 张三 12345678910 北京 测试 101 101线 张三 12345678910 上 ...
- Ubuntu 命令行下快速打开各类文件 分类: ubuntu shell 2014-11-18 20:06 210人阅读 评论(0) 收藏
xdg-open 命令可以用来在Ubuntu下快速打开各类文件. 下面是从 manual 文档里截取的内容: 可以知道,该命令的功能是在图形界面下按照用户的平时习惯打开各类文件,甚至是链接. 这样,我 ...
- House Robber 分类: leetcode 算法 2015-07-09 20:53 2人阅读 评论(0) 收藏
DP 对于第i个状态(房子),有两种选择:偷(rob).不偷(not rob) 递推公式为: f(i)=max⎧⎩⎨⎪⎪{f(i−1)+vali,f(i−2)+vali,robi−1==0robi−1 ...
随机推荐
- SpinLock 实现
/* Example: SpinLock Description: SpinLock is the lock implementation using AtomicInteger as a primi ...
- 《30天自制操作系统》04_day_学习笔记
harib01a: P65 用C语言实现内存写入 实现一个往黑画面上写入东西的函数 修改了naskfunc.nas中的内容 在bootpack.c中 用write_mem8()函数将VRMA中全部写入 ...
- [原创]java WEB学习笔记51:国际化 概述,API 之 locale类,dataFormat类,numberFormat类, MessageFormat类,ResourceBundle 类
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- mac office
弄了个office2016正式版的教程,大多数朋友表示搞不懂,SO重新写了个超级详细的,在不懂我也是醉了.Mac office 2016免费安装教程微软近日正式向 Office 365 订阅用户发布了 ...
- SQL Server练习
SQL Server 基本语法: http://www.w3school.com.cn/sql/sql_intro.asp 练习1: 运行语句: USE [Test1] select FNumber, ...
- smarty简单介绍
smarty简单介绍 示意图如下 简单介绍smarty.class.php类的大体内容,如下: <?php class Smarty //此类就是libs中的Smarty.class.php类 ...
- 9. 星际争霸之php设计模式--代理模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- [php] 判断当前运行模式
//判断是否cgi模式 define('IS_CGI',substr(PHP_SAPI, 0,3)=='cgi' ? 1 : 0 ); //判断操作系统是否为windows define('IS_WI ...
- Javascript与C#编码解码
(一) Javascript与C#编码解码的对应关系 http://www.jb51.net/article/44062.htm 这篇文章主要是对JS与C#编码解码进行了详细的介绍,需要的朋友可以过来 ...
- 微软Sql server analysis service数据挖掘技术
最新在一个项目中要求用到微软SSAS中的数据挖掘功能,虽然以前做项目的时候也经常用到SSAS中的多维数据集 (就是CUBE),但是始终没有对SSAS中的数据挖掘功能进行过了解.所以借着项目需求这股东风 ...