(最短路 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次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
随机推荐
- Ansible develop module
def cnf(action,configs,path): message = "Modify %s\n" %path changed = False if action == & ...
- conductor 系统任务
动态任务: 参数: dynamicTaskNameParam:来自任务输入的参数的名称,其值用于调度任务. 例如 如果参数的值为ABC,则调度的下一个任务类型为“ABC”. Example { &qu ...
- mysql优化连接数
很多开发人员都会遇见”MySQL: ERROR 1040: Too many connections”的异常情况,造成这种情况的一种原因是访问量过高,MySQL服务器抗不住,这个时候就要考虑增加从服务 ...
- sql重复数据只取一条记录
1.SQL SELECT DISTINCT 语句 在表中,可能会包含重复值.这并不成问题,不过,仅仅列出不同(distinct)的值. 关键词 DISTINCT 用于返回唯一不同的值. 语法: SEL ...
- Spring @Qualifier
先说明下场景,代码如下: 有如下接口: public interface EmployeeService { public EmployeeDto getEmployeeById(Long id); ...
- java 注解 基本原理 编程实现
摘要: java 1.5开始引入了注解和反射,正确的来说注解是反射的一部分,没有反射,注解无法正常使用,但离开注解,反射依旧可以使用,因此来说,反射的定义应该包含注解才合理一些. java 1.5开始 ...
- MYSQL 备份及还原数据库
二.还原 1.NEW DB
- 消息 14607,级别 16,状态 1,过程 sp_send_dbmail,第 141 行 profile 名称无效
错误:消息 14607,级别 16,状态 1,过程 sp_send_dbmail,第 141 行profile 名称无效 原因: 用SA帐户是可以发送邮件的,但换了另外一个帐户后却提示以上错误. 解决 ...
- CloseableHttpClient(二)
package com.cmy.httpClient; import java.io.IOException; import org.apache.http.HttpEntity; import or ...
- 15-matlab矩阵运用
from scipy.spatial import Delaunay from mpl_toolkits.mplot3d import Axes3D import numpy as np import ...