POJ 3259 Bellman_Ford算法
额。关键是读题。反正我是看了解题报告才知道意思的。给你n个点。m条路。双向的。耗费时间。w个虫洞。单向的。时间为负值。问你是否可以从某一点返回看到之前的自己。即为判断是不是有负环。用Bellman_Ford算法。
分分钟打完。排了好久的bug。还是循环那里j和i傻傻的分不清楚。
附代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define maxn 0x1f1f1f1f
using namespace std;
int n, m, w; //定点数 m+w是边数
struct Edge
{
int u, v, t;
} edge[6000];
int low[600];
int tot;
bool Bellman_Ford()
{
memset(low, maxn, sizeof(maxn));
//for (int i=2; i<=n; ++i)
// low[i] = maxn;
low[1] = 0;
for (int i=1; i<n; i++)
{
bool flag = false;
for (int j=0; j<tot; ++j)
{
if (low[edge[j].v] > low[edge[j].u] + edge[j].t)
{
low[edge[j].v] = low[edge[j].u] + edge[j].t;
flag = true;
}
}
if (!flag) break;
}
for (int i=0; i<tot; ++i)
{
if (low[edge[i].v] > low[edge[i].u] + edge[i].t)
return true; // 存在负环
}
return false;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &n, &m, &w);
tot = 0;
int u, v, t;
for (int i=0; i<m; ++i)
{
scanf("%d%d%d", &u, &v, &t);
edge[tot].u = u;
edge[tot].v = v;
edge[tot++].t = t;
edge[tot].u = v;
edge[tot].v = u;
edge[tot++].t = t;
}
for (int i=0; i<w; ++i)
{
scanf("%d%d%d", &u, &v, &t);
edge[tot].u = u;
edge[tot].v = v;
edge[tot++].t = -t;
}
bool flag = Bellman_Ford();
if (flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
POJ 3259 Bellman_Ford算法的更多相关文章
- poj 3259 (Bellman_Ford判断负环)
题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...
- Wormholes - poj 3259 (Bellman-Ford算法)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34934 Accepted: 12752 Description W ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- ShortestPath:Wormholes(POJ 3259)
田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...
随机推荐
- Python time strptime()与time strftime()
time strftime()接收时间元组,返回表示时间的字符串. time strptime()把时间字符串,解析成一个时间元组. import time t = time.strftime('%Y ...
- 3D CNN for Video Processing
3D CNN for Video Processing Updated on 2018-08-06 19:53:57 本文主要是总结下当前流行的处理 Video 信息的深度神经网络的处理方法. 参考文 ...
- FAST Hello World - Preparation for software's running environment
Ubuntu 14.04 安装 libpcap-1.1.1 & libpnet-1.1.4 & NMAC function lib 参考: NetMagic.org yacc: com ...
- python argparse模块--转载
add_argument:读入命令行参数,该调用有多个参数 ArgumentParser.add_argument(name or flags…[, action][, nargs][, const] ...
- Python 实现协程
协程的概念 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程.(其实并没有说明白~) 我觉得单说协程,比较抽象,如果对线程有一定了解的话,应该就比较 ...
- html 居中的内容显示框
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java 集合、Iterator迭代器、泛型等
01集合使用的回顾 A:集合使用的回顾 a.ArrayList集合存储5个int类型元素 public static void main(String[] args) { ArrayList<I ...
- 文件上传allowedTypes和文件下载contentType(mimeType)
我们在做文件上传和下载时,常常要用到以下mimeType,下面列出来供大家参考参考!希望多顶顶 '.a' : 'application/octet-stream', '.ai' ...
- netcat使用指南
在网络工具中有“瑞士军刀”美誉的NetCat, 在我们用了N年了至今仍是爱不释手.因为它短小精悍(这个用在它身上很适合,现在有人已经将其修改成大约10K左右,而且功能不减少).现在就我的一些使用心得和 ...
- Qt5_容器_知识点记录
1.删除: 1.1.erase 1.2.remove / removeAt 2. 3. 4. 5.