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)
田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...
随机推荐
- Linux进程间通信--使用信号量【转】
本文转载自:http://blog.csdn.net/ljianhui/article/details/10243617 这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号 ...
- AcceptAsync和BeginAccept的区别
Difference between […]Async and Begin[…] .net asynchronous APIs Note that most *Async methods (with ...
- 一种斐波那契博弈(Fibonacci Nim)
事实上我也不知道这算是哪个类型的博弈 是在复习$NOIP$初赛的时候看到的一个挺有趣的博弈 所以就写出来分享一下 $upd \ on \ 2018.10.12$忽然发现这个其实就是$Fibonacci ...
- 论文笔记——NEURAL ARCHITECTURE SEARCH WITH REINFORCEMENT LEARNING
论文地址:https://arxiv.org/abs/1611.01578 1. 论文思想 强化学习,用一个RNN学一个网络参数的序列,然后将其转换成网络,然后训练,得到一个反馈,这个反馈作用于RNN ...
- json封装数据,然后通过创造的标签调用
<ul class="list"> <li></li> <li></li> <li></li> ...
- java进制转换代码
定义十进制的数直接写,定义8进制的数以0开头,定义二进制的数以0b开头,定义十六进制的数以0x开头需要将十进制的数以二进制的数表示出来可以参照下例: int a = 10; System.out.pr ...
- 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 ...
- HDU 6083 度度熊的午饭时光(01背包+记录路径)
http://acm.hdu.edu.cn/showproblem.php?pid=6083 题意: 思路: 01背包+路径记录. 题目有点坑,我一开始逆序枚举菜品,然后一直WA,可能这样的话路径记录 ...
- HDU 3466 Proud Merchants(0-1背包)
http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意: 最近,iSea去了一个古老的国家.在这么长的时间里,它是世界上最富有和最强大的王国.结果,这个国家 ...
- Python简单做二维统计图
先上一张效果图: 以上图是一段时间内黄金价格的波动图. 代码如下: import datetime as DT from matplotlib import pyplot as plt from ma ...