POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259
题目大意是给你n个点,m条双向边,w条负权单向边。问你是否有负环(虫洞)。
这个就是spfa判负环的模版题,中间的cnt数组就是记录这个点松弛进队的次数,次数超过点的个数的话,就说明存在负环使其不断松弛。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 1e3 + ;
const int INF = 1e9;
struct data {
int next , to , cost;
}edge[MAXN * ];
int head[MAXN] , d[MAXN] , cnt[MAXN] , cont;
bool vis[MAXN]; void init(int n) {
for(int i = ; i <= n ; i++) {
d[i] = INF;
cnt[i] = ;
head[i] = -;
vis[i] = false;
}
cont = ;
} inline void add(int u , int v , int cost) {
edge[cont].next = head[u];
edge[cont].to = v;
edge[cont].cost = cost;
head[u] = cont++;
} bool spfa(int s , int n) {
d[s] = ;
queue <int> que;
while(!que.empty()) {
que.pop();
}
que.push(s);
while(!que.empty()) {
int temp = que.front();
que.pop();
vis[temp] = false;
for(int i = head[temp] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(d[v] > d[temp] + edge[i].cost) {
d[v] = d[temp] + edge[i].cost;
if(!vis[v]) {
que.push(v);
vis[v] = true;
cnt[v]++;
if(cnt[v] >= n)
return true;
}
}
}
}
return false;
} int main()
{
int n , m , c , u , v , cost , t;
scanf("%d" , &t);
while(t--) {
scanf("%d %d %d" , &n , &m , &c);
init(n);
while(m--) {
scanf("%d %d %d" , &u , &v , &cost);
add(u , v , cost);
add(v , u , cost);
}
while(c--) {
scanf("%d %d %d" , &u , &v , &cost);
add(u , v , -cost);
}
if(spfa( , n)) {
printf("YES\n");
}
else {
printf("NO\n");
}
}
}
POJ 3259 Wormholes(SPFA判负环)的更多相关文章
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes (判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- POJ 3259 Wormholes( bellmanFord判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36425 Accepted: 13320 Descr ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
- POJ 3259 Wormholes 最短路+负环
原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
随机推荐
- 无法连接到SQL Server 2008 R2
服务器环境: 操作系统 名称: Microsoft Windows Server 2008 R2 Enterprise 版本: 6.1.7601 服务包: Ser ...
- Asp.net 后台添加Meta标签方法
Asp.net 后台添加Meta标签方法包括keywords,CSS.JS 下面是从Asp.net 后台添加CSS.JS.Meta标签的写法,我们这里写成函数方便以后使用.如果函数放在页面类中, Pa ...
- hdu 4635 Strongly connected(强连通)
考强连通缩点,算模板题吧,比赛的时候又想多了,大概是不自信吧,才开始认真搞图论,把题目想复杂了. 题意就是给你任意图,保证是simple directed graph,问最多加多少条边能使图仍然是si ...
- VB程序逆向反汇编常见的函数
VB程序逆向常用的函数 1) 数据类型转换: a) __vbaI2Str 将一个字符串转为8 位(1个字节)的数值形式(范围在 0 至 255 之间) 或2 个字节的数值形式(范围在 -32,7 ...
- OK335xS psplash 进度条工作原理 hacking
#!/bin/sh # # rc This file is responsible for starting/stopping # services when the runlevel changes ...
- None
0 值的整型 / 浮点型.空字符串('').空列表([]). 空元组((,)).空字典({}).空集合(set())都等价于 False,但是不等于 None thing = None if thin ...
- linux 定时任务调度Cron的用法详解
在linux中,推荐使用crontab -e命令添加自定义的任务,退出后重启crond进程. 重新启动cron服务或重新加载cron配置,命令: 复制代码代码示例: /etc/rc.d/init.d/ ...
- liunx安装qq
http://www.07net01.com/电脑玩物 http://www.07net01.com/2014/09/68186.html 安装qq 一开始,我在ubuntu14.04下安装的QQ版本 ...
- Oracle 不同故障的恢复方案
之前在Blog中对RMAN 的备份和恢复做了说明,刚看了下,在恢复这块还有知识点遗漏了. 而且恢复这块很重要,如果DB 真要出了什么问题,就要掌握对应的恢复方法. 所以把DB的恢复这块单独拿出来说明一 ...
- [Everyday Mathematics]20150113
设 $f\in C^2(0,+\infty)$ 适合 $$\bex \lim_{x\to 0^+}f'(x)=-\infty,\quad \lim_{x\to 0^+}f''(x)=+\infty. ...