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 ...
随机推荐
- 初始加载时edittext不自动获取焦点的方法
在做一个搜索功能时,刚进入界面,edittext直接自动获取焦点,这显然不是我想要的. 那么问题来了:如何关闭EditText自动获取焦点的? 尝试了好几个方法,在Java代码添加focus控制,在x ...
- [Android Memory] 手动回收ImageVIew的图片资源
ImageView默认是不进行图片资源的回收的,需要我们自己在activity或者fragment中进行回收: public static void releaseImageViewResouce(I ...
- leetCode(45):Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- ylbtech-LanguageSamples-Porperties(属性)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Porperties(属性) 1.A,示例(Sample) 返回顶部 “属性”示例 本示 ...
- java 利用JAX-RS快速开发RESTful 服务实例
首先看web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...
- 简单的混淆ID
public class TestfuzzId { public static void main(String[] args) { int id = 123456; int p = id^Integ ...
- C#秘密武器之特性
一.概述 Attribute说白了就是一个类而已,里边一般含有一些附加信息,或者一些特殊的处理逻辑,以便告诉编译器应用该特性的东东是个奇葩,需要特殊对待! 二.使用时的注意事项 2.1. Attrib ...
- freemarker的list指令小技术归纳
1.问题:当数据超过3位的时候,freemarker会自动用逗号截取,例如2,311 解决方法(一种即可): (1)加.toString(),如:${(data).toString()} (2)加?c ...
- vue - dist
描述:打包后准备上线的文件(需要服务器环境才能运行!!!)
- python将字典内容存入mysql
1.背景 项目须要,用python实现了将字典内容存入本地的mysql数据库. 比方说有个字典dic={"a":"b","c":& ...