POJ--3529--Wormholes
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 36889 | Accepted: 13520 |
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
题意:就是给N组測试数据,给你N个点M条无向边,再给你W个虫洞(能够使时间倒流)。问你能不能看到刚出发时的自己。(就是推断存不存在负环)。
思路:套上SPFA公式然后推断负环就能够了。
#include<stdio.h>//好坑,wa了半天没过去,竟然是由于大写和小写问题。整个人都不好了。
#include<string.h>
#include<queue>
#define INF 0x3f3f3f3f
#define M 3000*2
#define N 550
using namespace std;
int n,m,w,dis[N],vis[N],used[N],head[N],edgenum;
struct node{
int from,to,cost,next;
}edge[M];
void init(){
edgenum=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int cost){
node E={u,v,cost,head[u]};
edge[edgenum]=E;
head[u]=edgenum++;
}
void spfa(){
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
memset(used,0,sizeof(used));
dis[1]=0;
vis[1]=1;
q.push(1);
used[1]++;
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].cost){
dis[v]=dis[u]+edge[i].cost;
if(!vis[v]){
vis[v]=1;
used[v]++;
if(used[v]>n){
printf("YES\n");//存在负环就意味着时光能够倒流。
return ;
}
q.push(v);
}
}
}
}
printf("NO\n");
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&w);
init();
while(m--){
int a,b,cost;
scanf("%d%d%d",&a,&b,&cost);//加入无向边。
add(a,b,cost);
add(b,a,cost);
}
while(w--){
int a,b,cost;//加入单向虫洞。 scanf("%d%d%d",&a,&b,&cost);
cost=-cost;
add(a,b,cost);
}
spfa();
}
return 0;
}
ac代码:
POJ--3529--Wormholes的更多相关文章
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- poj 3259 Wormholes
题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ...
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
随机推荐
- node.js中express-session配置项详解
官方地址:https://www.npmjs.com/package/express-session 作用:用指定的参数创建一个session中间件,sesison数据不是保存在cookie中,仅仅s ...
- 开源轻量级即时通讯技术 MobileIMSDK 的常见问题解答
本帖最后由 茜茜asa 于 2015-12-14 17:50 编辑 申明:MobileIMSDK 目前为个人原创开源工程且已发布,现整理了一些有关MobileIMSDK的常见的问题,希望对需要的人有用 ...
- Linux Shell编程与编辑器使用详解
<Linux Shell编程与编辑器使用详解> 基本信息 作者: 刘丽霞 杨宇 出版社:电子工业出版社 ISBN:9787121207174 上架时间:2013-7-22 出版日期:201 ...
- codeforces#277.5 C. Given Length and Sum of Digits
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- jQuery选择器的灵活用法
// 摘自: http://hi.baidu.com/274084093/item/47a4ce696e89e534ad3e836b jQuery中选择器很强大,可以根据元素名称.ID.class等多 ...
- TestNG 一、 概论
一. 概论 TestNG,即Testing, NextGeneration,下一代测试技术,是一套根据JUnit 和NUnit思想而构建的利用注释来强化测试功能的一个测试框架,即可以用 ...
- FTP服务器高级配置
设置FTP服务器的访问限制 /etc/vsftpd/ftpusers ftpusers :黑名单 其内所有的用户无法登录FTP服务器 如果userlist_enable=YES(主配置文件中设置),u ...
- Python自省 type(),dir(),getattr(),hasattr(),isinstance().
Python自省 这个也是python彪悍的特性. 自省就是面向对象的语言所写的程序在运行时,所能知道对象的类型.简单一句就是运行时能够获得对象的类型.比如type(),dir(),getattr() ...
- js数组高阶方法reduce经典用法代码分享
以下是个人在工作中收藏总结的一些关于javascript数组方法reduce的相关代码片段,后续遇到其他使用这个函数的场景,将会陆续添加,这里作为备忘. javascript数组那么多方法,为什么我要 ...
- bin和sbin区别
据说这个目录结构是沿袭unix的,不大清楚. bin是binary的缩写,是可执行的二进制文件./bin里面一般是基本的,大家都要用的工具:sbin里面的s是system的意思,是供system ad ...