(最短路 spfa)Wormholes -- poj -- 3259
http://poj.org/problem?id=3259
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 37356 | Accepted: 13734 |
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
代码:
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int INF = (<<)-;
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define N 5500 int n, m, w, dist[N], G[N][N], vis[N];
int u; struct node
{
int v, t, next;
} a[N]; int Head[N], cnt; void Init()
{
cnt = ;
memset(Head, -, sizeof(Head));
memset(vis, , sizeof(vis));
for(int i=; i<=n; i++)
{
dist[i] = INF;
for(int j=; j<=i; j++)
G[i][j] = G[j][i] = INF;
}
} void Add(int u, int v, int t)
{
a[cnt].v = v;
a[cnt].t = t;
a[cnt].next = Head[u];
Head[u] = cnt++;
} int spfa()
{
queue<int>Q;
Q.push();
dist[] = ;
vis[] = ; while(Q.size())
{
int u = Q.front(); Q.pop(); vis[u] = ; for(int i=Head[u]; i!=-; i=a[i].next)
{
int v = a[i].v;
int t = a[i].t;
if(dist[u] + t < dist[v])
{
dist[v] = dist[u] + t;
if(vis[v] == )
{
Q.push(v);
vis[v] = ;
}
}
} if(dist[] < ) ///当 dist[1] 为负数的时候说明它又回到了原点
return ;
}
return ;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, u, v, x; scanf("%d%d%d", &n, &m, &w); Init();
for(i=; i<=m; i++)
{
scanf("%d%d%d", &u, &v, &x);
Add(u, v, x);
Add(v, u, x);
} for(i=; i<=w; i++)
{
scanf("%d%d%d", &u, &v, &x);
Add(u, v, -x);
} int ans = spfa(); if(ans)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
(最短路 spfa)Wormholes -- poj -- 3259的更多相关文章
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- Wormholes POJ - 3259 spfa判断负环
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...
- ShortestPath:Wormholes(POJ 3259)
田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...
- Wormholes - poj 3259 (Bellman-Ford算法)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34934 Accepted: 12752 Description W ...
- kuangbin专题专题四 Wormholes POJ - 3259
题目链接:https://vjudge.net/problem/POJ-3259 思路:求有无负环,起点随意选就可以,因为目的只是找出有没有负环,有了负环就可以让时间一直回退,那么一定能回到当初,这里 ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
随机推荐
- Spring 中的国际化Message的简单例子(ApplicationContext) 不跟框架集成的版本
首先,建立一个描述message的XML文件,名为messages.xml <?xml version="1.0" encoding="UTF-8" ...
- Haskell语言学习笔记(34)System.Environment
System.Environment getArgs :: IO [String] getProgName :: IO String getExecutablePath :: IO FilePath ...
- nstall neovim on Ubuntu 16.04
https://neovim.io/ To install NeoVim on Ubuntu, run 1 2 3 sudo add-apt-repository ppa:neovim-ppa/sta ...
- js实现jquery函数animate动画效果
<script> function animate(obj, json, interval, sp, fn) { clearInterval(obj.timer); function ge ...
- 分类模型评估之ROC-AUC曲线和PRC曲线
http://blog.csdn.net/pipisorry/article/details/51788927 在样本分布及其不均匀的情况下,建议用PRC...可以看下这个精确率.召回率.F1 值.R ...
- go语言中make和new的区别
make用于内建类型(map.slice 和channel)的内存分配.new用于各种类型的内存分配. 内建函数new本质上说跟其他语言中的同名函数功能一样:new(T)分配了零值填充的T类型的内存空 ...
- ASP.NET中的URL编码解码(转)
在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗?测试: string file="文件上(传)篇.doc";string Server_UrlEncode=Server ...
- hdoj1260 Tickets (简单DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 思路: 很简单的DP题,状态方程也比较容易想到,用f[i]表示到第i个人所耗的最短时间,详见代码 ...
- Hibernate查询方式(补)
-----------------siwuxie095 Hibernate 查询方式 1.对象导航查询 根据已经加载的对 ...
- Linux SSH基于密钥交换的自动登陆原理简介及配置说明
一.原理简介 SSH证书认证登录的基础是一对唯一匹配密钥: 私钥(private key)和公钥(public key).公钥用于对数据进行加密,而且只能用于加密.而私钥只能对使用所匹配的公钥,所加密 ...