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

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions:75598   Accepted: 28136

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.
题目大意:n 个点, m 条双向边,代表经过所需的时间,w 条单向边,代表经过所减少的时间。问该图中是否存在负环。
思路:
1.简单的判环,用spfa即可,在不存在环的情况下,任意点在进行路径松弛时,最多被其他的点更新一次。那么任意点的最多入队次数只能是 n 次。当存在任何一点的入队次数大于顶点数n,即说明存在环。
2.判负环,即路径往小松弛。判正环时,即路径往大更新。注意dis数组初始化即可。
代码如下:
 #include<stdio.h>
#include<queue>
#include<string.h>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = ;
const int MAXM = ;
const int inf = 0x3f3f3f3f;
using namespace std; int n, m, k; //n个点 m条双向边 k个虫洞(单向边)
int head[MAXN], cnt;
int vis[MAXN], num[MAXN];//num表示第i个点的入队次数 用来判断负环是否存在
int dis[MAXN], flag;
queue<int> Q; struct Edge
{
int to, next, w;
}edge[ * MAXM]; void add(int a, int b, int c)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].w = c;
edge[cnt].next = head[a];
head[a] = cnt;
} void spfa(int st)
{
while(!Q.empty()) Q.pop();
mem(vis, ), mem(dis, inf), mem(num, );
Q.push(st);
vis[st] = ;
num[st] = ;
dis[st] = ;
while(!Q.empty())
{
int a = Q.front();
Q.pop();
vis[a] = ;
for(int i = head[a]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(dis[to] > dis[a] + edge[i].w)
{
dis[to] = dis[a] + edge[i].w;
if(!vis[to])
{
vis[to] = ;
Q.push(to);
num[to] ++;
if(num[to] > n)
{
flag = ;
break;
}
}
}
}
if(flag)
break;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
cnt = , flag = , mem(head, -);
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i <= m; i ++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
for(int i = ; i <= k; i ++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, -c);
}
spfa();
}
return ;
}

POJ3259

 

POJ3259 Wormholes 【spfa判负环】的更多相关文章

  1. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  2. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

  3. POJ3259 Wormholes(SPFA判断负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  4. POJ 3259 Wormholes(SPFA判负环)

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

  5. poj3259(spfa判负环)

    题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时 ...

  6. POJ3259 Wormholes —— spfa求负环

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

  7. Poj 3259 Wormholes(spfa判负环)

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

  8. BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环

    Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...

  9. spfa判负环

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

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

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

随机推荐

  1. 3、python--第三天练习题

    #1.简述普通参数.指定参数.默认参数.动态参数的区别 #1.普通参数就是传入的函数,没有默认值 def f(a): a = a + 1 return a print(f(3)) #2.指定参数 de ...

  2. 007_STM32程序移植之_多通道ADC转换

    1. 测试环境:STM32C8T6 2. 测试:使用DMA进行多通道ADC转换 3. 描述:用 ADC 连续采集 12 路模拟信号,并由 DMA 传输到内存.ADC 配置为扫描 并且连续转换模式,AD ...

  3. 快速了解AMD、CMD、CommonJS、ESM

    1.ES6 Module javascript在ES2015(ES6)中出现了语言层面的模块(module). ES6的模块既可以用于浏览器端,也可以用于服务器端(nodeJS). ES6模块是静态化 ...

  4. @Async 异步注释 @EnableAsync

    @SpringBootApplication @ComponentScan(basePackages = "com.fddsfsg") //@EnableSwagger2 @Ena ...

  5. 数据库学习之二--SQL语句以及数据类型

    一.SQL语句种类: 1. DDL(Data Definition Language,数据定义语言)用来创建或者删除存储数据用的数据库以及数据库中的表;包含以下几种指令: a. CREATE:CREA ...

  6. 【luogu1468】[Violet]蒲公英--求区间众数

    题目背景 亲爱的哥哥: 你在那个城市里面过得好吗? 我在家里面最近很开心呢.昨天晚上奶奶给我讲了那个叫「绝望」的大坏蛋的故事的说!它把人们的房子和田地搞坏,还有好多小朋友也被它杀掉了.我觉得把那么可怕 ...

  7. 一、docker安装CentOS7

    一.安装步骤 前提条件 Docker运行在CentOS7上,要求系统64位.系统内核版本为3.10以上. Docker是一个进程,一启动就两个进程,一个服务,一个守护进程.占用资源就非常少,启动速度非 ...

  8. 七、find 文件查找

    在文件系统上查找符合条件的文件 locate :依赖于事先构建的索引,索引的构建是在系统较为空闲时自动进行(周期性任务) 手动更新数据库(updatedb) 非实时查找 模糊查找 索引构建过程需要遍历 ...

  9. elasticsearch _source

    默认地,Elasticsearch 在 _source 字段存储代表文档体的JSON字符串.和所有被存储的字段一样, _source 字段在被写入磁盘之前先会被压缩.这个字段的存储几乎总是我们想要的, ...

  10. IE与其他浏览器兼容性问题总结

    1.eval(idName) [问题描述]:IE.safari.Chrome浏览器下都可以使用eval(idName)或getElementById(idName)来取得id为idName的HTML对 ...