POJ Wormholes 3259
题目描述:
Farmer John 在探索农场的时候 惊奇的发现一些虫洞,虫洞是一个特殊的东西,他是一个单向通道,他能到达虫洞的另一端, 可以穿越到达之前的时间。Farmer John 的由N个农场组成, 编号是 1-N, M条单向通道, W个虫洞。
Farmer John 是个时空穿越迷, 他想要做到一下几点: 开始在一些农场,旅行通过虫洞或者是路,通过虫洞能返回到他之前的时间。
帮助Farmer John 他是否有可能穿越到之前的时间, 他将会给你完整的地图, 他有F个农场, 走过这段路径的长度是小于10000秒的, 并且虫洞的穿越也是不会超过10000秒的
输入:
F代表测试实例的个数
第二行三个整数 N, M ,W 分别用空格隔开
接下来M行 是M个道路 ,道路是双向的。 S, E ,T 代表从S到E话费时间为T。
接下来W行 代表W个虫洞, 虫洞是单向, S, E, T,代表 从S 到 E 穿越的时间是 T。
题目分析:
他问你是否能穿越,其实就是问有无负权环。 直接SPFA 判断
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
#define INF 0xfffffff
#define maxn 520
struct Edge
{
int e, w;
Edge(int e=,int w=) : e(e), w(w) {}
};
bool vis[maxn];
int use[maxn], dist[maxn];
int n, m, w;
vector<Edge> G[maxn]; void Init()
{
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++)
dist[i] = INF, use[i] = ;
}
bool Spfa(int Star)
{
Edge P, Pn;
queue<Edge> Q;
P.e = Star, P.w = ;
Q.push(P);
use[Star] = ;
dist[Star] = ; while( !Q.empty() )
{
P = Q.front();
Q.pop(); vis[P.e] = false; int len = G[P.e].size(); for(int i=; i<len; i++)
{
Pn = G[P.e][i]; if( dist[Pn.e] > dist[P.e] + Pn.w )
{
dist[Pn.e] = dist[P.e] + Pn.w; if( !vis[Pn.e] )
{
vis[Pn.e] = true;
Q.push(Pn);
use[Pn.e] ++; if(use[Pn.e] >= n && dist[Pn.e] < )
return true;
}
}
}
}
return false;
}
bool Slove()
{
for(int i=1; i<=n; i++)
{
Init(); if( Spfa(i) )
return true;
}
return false;
} int main()
{
int T;
cin >> T; while(T--)
{
cin >> n >> m >> w;
int a, b, c; for(int i=; i<=n; i++)
G[i].clear(); for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c); G[a].push_back( Edge(b,c) );
G[b].push_back( Edge(a,c) );
} for(int i=; i<w; i++)
{
scanf("%d%d%d",&a,&b,&c); G[a].push_back( Edge(b,-c) ); } if( Spfa(i) )
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return ;
}
POJ Wormholes 3259的更多相关文章
- POJ No 3259 Wormholes Bellman-Ford 判断是否存在负图
题目:http://poj.org/problem?id=3259 题意:主要就是构造图, 然后判断,是否存在负图,可以回到原点 /* 2 3 3 1 //N, M, W 1 2 2 1 3 4 2 ...
- 【POJ】3259 Wormholes
题目链接:http://poj.org/problem?id=3259 题意:n个农场,m条双向路径,w条单向路径(虫洞).单向虫洞路径是负值.农夫想知道自己能不能看到自己(X). 题解:其实刚开始没 ...
- POJ Wormholes (SPFA)
http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...
- POJ 1860&&3259&&1062&&2253&&1125&&2240
六道烦人的最短路(然而都是水题) 我也不准备翻译题目了(笑) 一些是最短路的变形(比如最长路,最短路中的最长边,判环),还有一些就是裸题了. 1860:找环,只需要把SPFA的松弛条件改一下即可,这里 ...
- 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 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 (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
随机推荐
- 使用jqMobi开发app基础:Badge的使用
显示效果: 红色的部分就是Badge,能够用来显示数量或者是其它的信息. 使用事实上非常easy, $.ui.updateBadge("#" + id, res.Msg, &qu ...
- css之选择器
我们都用过jquery,使用jquery选择器,非常的简单,最近刚好有项目上手,拿起书本看了一下,发现好多的东西都忘掉了,好记性不如烂笔头,就将这章内容记录下来,现在我们看下css原生的选择器. 选择 ...
- java多态---ABC案列
class A{ public void show(){ show2(); } public void show2(){ System.out.println("我"); } } ...
- POJ 3449 Geometric Shapes (求正方形的另外两点)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1470 Accepted: 622 D ...
- Servlet学习--练习示例总结
醉了醉了..本来想测试下Servlet生命周期的,然后调了好久的错误,还是没成功,不知道为什么不能这样做 贴上代码: import java.io.IOException; import java.i ...
- Python 时间函数
时间的运用 #coding=utf-8 #!user/bin/python import time import calendar ticks = time.asctime(time.localtim ...
- sql - Invalid object name 'dbo.in$'
这是我从excel导入的表,用查询的时候,不加前面部分的'dbo',能查出来,好像是owner的原因吧.
- oracle中创建一个用户,只能查看指定的视图,如何授权,创建别名
1.create user A identified by Apassword,创建用户,用户名是A,密码是Apassword2.grant connect to A --授予connect权限3.g ...
- 重新开始学习javase_集合_Map
一,Map之HashMap(转:http://blog.csdn.net/zheng0518/article/details/42197049) 1. HashMap概述: HashMap是基于 ...
- Android Studio删除Project
最直截了当的方法是在Project所在目录直接删除整个Project,然后再次打开Android Studio的欢迎界面中把光标移到你的Project上,然后按键盘上的Delete键即可! 参考自st ...