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 ...
随机推荐
- oracle和sql server的区别(1)
A.instance和database 1.从oracle的角度来说,每个instance对应一个database.有时候多个instance对应一个database(比如rac环境).有自己的Sys ...
- jvm内存设置
JVM有很多个内存管理命令,总体而言,分为两类: 1.-X开头的管理命令:这些选项在JDK升级时不会通知修改: 2.-XX开头的管理命令:这些选项不够稳定,所以建议少用. JVM参数的含义: 参数名称 ...
- .NET C#: NameValueCollection
NameValueCollection class is in System.Collection.Specialized assembly. Unlike with HashTable, NameV ...
- UVA 10498 Happiness(线性规划-单纯形)
Description Prof. Kaykobad has given Nasa the duty of buying some food for the ACM contestents. Nasa ...
- bzoj 4237稻草人
按x轴进行分治,将[l,r]分成[l,mid]和[mid+1,r],左下角点x值在[l,mid]中,右上角点x值在[mid+1,r],然后将[l,r]中的所有点按y轴排序,按顺序扫描,若扫描到左下角点 ...
- CCF真题之相邻数对
201409-1 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一 ...
- asp上传图片提示 ADODB.Stream 错误 '800a0bbc'的解决方法
asp上传图片提示 ADODB.Stream 错误 '800a0bbc' 有这个提示有很多问题导致.权限是常见一种.这个不多说,还有一个有点怪的就是 windows2008显示系统时间的格式竟然是:2 ...
- 开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误
已经解决,问题描述:在开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误,程序调试运行,发现程序在打开数据库时候报错,也就是Connection.Open() ...
- Workflow Mailer Notifications设置
参考:http://www.docin.com/p-651716490.html http://www.360doc.com/content/12/0218/15/3200886_187602886. ...
- 查看在线EBS用户的相关信息
--查看在线EBS用户的相关信息 SELECT PAP.FULL_NAME, FU.USER_NAME, FAT.APPLICATION_NAME, FRT.RESPONSIBILITY_NAME, ...