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 ...
随机推荐
- windows下根据端口号杀死进程
Windows不像Linux,Unix那样,ps -ef 查出端口和进程号,然后根据进程号直接kill进程. Windows根据端口号杀死进程要分三步: 第一步 根据端口号寻找进程号 C:\>n ...
- respondsToSelector: selector
-(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
- csu oj 1339: 最后一滴血
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1339 1339: 最后一滴血 Time Limit: 1 Sec Memory Limit: 1 ...
- <构建之法>之一至二章
身在大学,却想起了在高中的生活和初中的生活,特别是初中的生活,为什么这么说呢!因为<构建之法>,看了其中的两章的内容,为什么想到了初中和高中的生活呢,因为在高中和初三的时候看的最多的就是课 ...
- 在linux中的virtualbox无法挂载usb设备的解决方法
方法来源于网络. 在安装完virtualbox之后,virtualbox会建立一个名为 vboxusers 的组,将你的用户名加入到该组即可. 命令参考: #usermod -a -G vboxuse ...
- Yii框架,在页面输出执行sql语句,方便调试
1.下载yiidebugtb,并且放入到 application.extensions.yiidebugtb 目录 2.修改main.php,加入如下代码: 'log'=>array( 'cla ...
- ubuntu下配置SVN服务器
自己买的阿里云服务器.可是我老感觉没有SVN上传代码下载代码太不方便!决定配置个SVN服务器! 1.安装Subversion $ sudo apt-get install subversion $ s ...
- 有关OpenGL着色语言(一)
刚接触OpenGL着色语言...,不定期增加内容 1.OpenGL着色语言(GLSL)是什么? 用于OpenGL的面向过程的高级着色语言,是近年来图形编程领域中出现的最重要的新型开发技术,使用Open ...
- YeoMan 与Angularjs
链接地址: Yeoman:强大的web构建工具 http://hao.jobbole.com/yeoman/ Yeoman官方教程:用Yeoman和AngularJS做Web应用 http://blo ...
- 随讲MyIsam和InnoDB的区别
mysiam表不支持事务处理,同时mysiam表不支持外键.外键不用说了吧?不知道的话,找度娘. 同时,在执行数据库写入的操作(insert,update,delete)的时候,mysiam表会锁表, ...