题目链接:http://poj.org/problem?id=3259

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 44090   Accepted: 16203

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..NM (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: A single integer, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

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

Hint

For farm 1, FJ cannot travel back in time. 
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
 
题意:2个案例,3个点,3条路,1个虫洞,路是双向的,虫洞是单向的,单向的路为负,起点为1,看能不能穿越。
分析:SPFA判负环。
没有看模板,直接上代码,结果发现不知道WA了多少次,实在搞不下去了,最后,佳鑫大神帮我Debug了,我在建图的时候head初始化了,结果在SPFA里面有初始化为-1了,真是害死我了,最后还是RE了一次,数组开小了。不管怎么样,Dijkstra,SPFA,Floyd,Kruskal都可以不用看模板了,很开心。
#include <stdio.h>
#include<iostream>
#include <queue>
#include <string.h> using namespace std; #define INF 0x3f3f3f3f int n,m,t; struct Edge
{
int v;
int w;
int next;
} edge[]; int NE = ; int dis[];
int head[];
bool vis[];
int cnt[]; void add(int u,int v,int d)
{
edge[NE].v = v;
edge[NE].w = d;
edge[NE].next = head[u];
head[u]= NE++;
} bool SPFA()
{
for(int i=; i<=n; i++)
{
dis[i] = INF;
vis[i] = false;
cnt[i] = ;
} dis[] = ;
vis[] = true;
cnt[] = ;
queue<int> Q;
Q.push(); while(!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=false;
for(int i=head[u]; i!=-; i=edge[i].next)
{ if(dis[edge[i].v]>dis[u]+edge[i].w)
{
dis[edge[i].v] = dis[u] + edge[i].w;
if(!vis[edge[i].v])
{
vis[edge[i].v] = true;
Q.push(edge[i].v);
if(++cnt[edge[i].v]>n)
return true;
}
}
}
}
return false;
} int main()
{
//freopen("input.txt","r",stdin);
int cases;
scanf("%d",&cases);
while(cases--)
{ NE = ;
memset(head,-,sizeof(head));
scanf("%d%d%d",&n,&m,&t); for(int i=; i<m; i++)
{
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
add(a,b,d);
add(b,a,d);
} for(int i=; i<t; i++)
{
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
add(a,b,-d);
} if(SPFA())
printf("YES\n");
else printf("NO\n");
}
return ;
}
 

Poj(3259),SPFA,判负环的更多相关文章

  1. Wormholes POJ - 3259 spfa判断负环

    //判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...

  2. POJ - 3851-Wormholes(SPFA判负环)

    A friend of yours, an inventor, has built a spaceship recently and wants to explore space with it. D ...

  3. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  4. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  5. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  6. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

  7. spfa判负环

    bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...

  8. 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)

    传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...

  9. BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划

    BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...

  10. [P1768]天路(分数规划+SPFA判负环)

    题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...

随机推荐

  1. Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  2. (转) Java读取文本文件中文乱码问题

    http://blog.csdn.net/greenqingqingws/article/details/7395213 最近遇到一个问题,Java读取文本文件(例如csv文件.txt文件等),遇到中 ...

  3. 【Origin】 叹文

    行文如流水, 千字挥手就: 偏偏伤脑筋, 哪得轻松事. -作于二零一五年五月三十日

  4. [原创]java WEB学习笔记78:Hibernate学习之路---session概述,session缓存(hibernate 一级缓存),数据库的隔离级别,在 MySql 中设置隔离级别,在 Hibernate 中设置隔离级别

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. 动画--过渡所需时间 transition-duration

    transition-duration属性主要用来设置一个属性过渡到另一个属性所需的时间,也就是从旧属性过渡到新属性花费的时间长度,俗称持续时间. 案例演示: 在鼠标悬停(hover)状态下,让容器从 ...

  6. GitHub和SourceTree入门教程——(转载),希望能帮到有需要的人

    -->本教程适用于主流的开源网站github和bitbucket,个人认为sourceTree还是比较好用的git客户端,支持windows和mac os. -->soureceTree的 ...

  7. 夺命雷公狗---DEDECMS----10dedecms双标签

    双标签基本语法如下: {dede:标签名 参数名=“值” 参数名2=“值”...} 内容...... {/dede} 我们先来查看下手册,如下所示: 我们先来用一个channel的标签来做实例,因为c ...

  8. Delphi中SQL批量插入记录

    http://www.cnblogs.com/azhqiang/p/4050331.html 在进行数据库操作时, 我们经常会遇到批量向数据库中写入记录的情况. 在这里我提供3种操作方式:   1.  ...

  9. zw版【转发·台湾nvp系列Delphi例程】HALCON InpaintingCt1

    zw版[转发·台湾nvp系列Delphi例程]HALCON InpaintingCt1 unit Unit1;interfaceuses Windows, Messages, SysUtils, Va ...

  10. 转:Eclipse常用开发插件

    以下是我整理的自己开发过程中的常用Eclipse插件,按字母排序: (1)    AmaterasUML         介绍:Eclipse的UML插件,支持UML活动图,class图,sequen ...