题目描述:

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的更多相关文章

  1. 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 ...

  2. 【POJ】3259 Wormholes

    题目链接:http://poj.org/problem?id=3259 题意:n个农场,m条双向路径,w条单向路径(虫洞).单向虫洞路径是负值.农夫想知道自己能不能看到自己(X). 题解:其实刚开始没 ...

  3. POJ Wormholes (SPFA)

    http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovere ...

  4. POJ 1860&&3259&&1062&&2253&&1125&&2240

    六道烦人的最短路(然而都是水题) 我也不准备翻译题目了(笑) 一些是最短路的变形(比如最长路,最短路中的最长边,判环),还有一些就是裸题了. 1860:找环,只需要把SPFA的松弛条件改一下即可,这里 ...

  5. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  6. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  7. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  8. poj 3259 Wormholes

    题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...

  9. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

随机推荐

  1. [AngularJS] Enable Animations Explicitly For A Performance Boost In AngularJS

    http://www.bennadel.com/blog/2935-enable-animations-explicitly-for-a-performance-boost-in-angularjs. ...

  2. [WebStrom] Change default cmd to Cygwin

    GO to setting, search Terminal: Change shell path : C:\cygwin\bin\bash.exe --login -i    (to the loc ...

  3. Cocostudio学习笔记(1) 扯扯蛋 + 环境搭建

    转眼七月份就到了,2014已经过了一半,而我也最终算是有"一年工作经验"了,开心ing. 回想这一年Cocos2dx的游戏开发经历,去年下半年重心主要在游戏的逻辑上,而今年上半年重 ...

  4. 使用 Java 配置进行 Spring bean 管理--转

    概述 众所周知,Spring 框架是控制反转 (IOC) 或依赖性注入 (DI) 模式的推动因素,而这种推动是通过基于容器的配置实现的.过去,Spring 允许开发人员使用基于 XML 的配置,通过利 ...

  5. Hibernate分页

    1. HQL分页: Session session = HibernateUtil.getInstance().getSession(); Query query = session.createQu ...

  6. Android(java)学习笔记214:开源框架的文件上传(只能使用Post)

    1.文件上传给服务器,服务器端必然要写代码进行支持,如下: 我们新建一个FileUpload.jsp的动态网页,同时我们上传文件只能使用post方式(不可能将上传数据拼凑在url路径下),上传数据Ap ...

  7. iOS多线程编程之GCD的使用

    什么是线程呢? 1个CPU执行的CPU命令列为一条无分叉的路径即为线程. 这种无分叉路径不止1条,存在多条时即为多线程. 什么是GCD? Grand Central Dispatch (GCD)是异步 ...

  8. 转载:C#中委托、事件与Observer设计模式

    原文地址 http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx 感谢博主分享! 范例说明 假设 ...

  9. CXF处理Date类型的俩种方式

    1. CXF利用wsdl2java生成客户端时Date日期类型转换 http://cwfmaker.iteye.com/blog/1142835 2. GregorianCalendar cal = ...

  10. pod install后出现的错误

    [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use T ...