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)
田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...
随机推荐
- 20145220韩旭飞《网络对抗》Exp7 网络欺诈技术防范
20145220韩旭飞<网络对抗>Exp7 网络欺诈技术防范 应用SET工具建立冒名网站 要让冒名网站在别的主机上也能看到,需要开启本机的Apache服务,并且要将Apache服务的默认端 ...
- 在Android Studio中创建项目和模拟器
北京电子科技学院 实 验 报 告 课程:移动平台应用开发实践 班级:201592 姓名:杨凤 学号:20159213 成绩:___________ 指导老师:娄嘉 ...
- Java实现心跳机制
一.心跳机制简介 在分布式系统中,分布在不同主机上的节点需要检测其他节点的状态,如服务器节点需要检测从节点是否失效.为了检测对方节点的有效性,每隔固定时间就发送一个固定信息给对方,对方回复一个固定信息 ...
- Python3基础 sys.path 查看搜索路径变量
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Leetcode ——Lowest Common Ancestor of a Binary Tree
Question Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. ...
- CSS3 常用选择器
p:last-of-type{background-color: red;} 选择p中最后一项 p:nth-of-type(2n){background-color: red;} 隔行变色里面也可以填 ...
- CVS导出&&自定义Attribute的使用
1.cvs导出:List转为byte[] /// <summary> /// CvsExport帮助类 /// </summary> public static class C ...
- Netty原理剖析
1. Netty简介 Netty是一个高性能.异步事件驱动的NIO框架,基于JAVA NIO提供的API实现.它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作 ...
- 【TCP/IP详解 卷一:协议】第十二章 广播和多播
建议参考:广播和多播 IGMP 12.1 引言 IP地址知识点回顾: IP地址分为三种:(1)单播地址 (2)广播地址 (3)多播地址 另外一种是,IP地址一般划分成五类:A-E类. 单播 考虑 类似 ...
- google nmt 实验踩坑记录
最近因为要做一个title压缩的任务,所以调研了一些text summary的方法. text summary 一般分为抽取式和生成式两种.前者一般是从原始的文本中抽取出重要的word o ...