kuangbin专题专题四 Wormholes POJ - 3259
题目链接:https://vjudge.net/problem/POJ-3259
思路:求有无负环,起点随意选就可以,因为目的只是找出有没有负环,有了负环就可以让时间一直回退,那么一定能回到当初,这里我选择从1号点开始。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
using namespace std; typedef long long LL;
#define inf (1LL << 30) - 1
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = ;
int dis[N];
int n,m,t;
int u,v,w; struct node{
int u,v,w;
}; vector<node> E; void init(){
memset(dis,inf,sizeof(dis));
E.clear();
} void input(){ rep(i,,m){ cin >> u >> v >> w;
E.push_back(node{u,v,w});
E.push_back(node{v,u,w});
} rep(i,,t){ cin >> u >> v >> w;
E.push_back(node{u,v,-w});
}
} bool bellman_ford(){ dis[] = ;
bool flag = true;
rep(i,,n){ flag = false;//判断一层循环中有没有出现可以更新的点
for(int o = ; o < E.size(); o++){
if(dis[E[o].v] > dis[E[o].u] + E[o].w)
dis[E[o].v] = dis[E[o].u] + E[o].w; flag = true;//还有可以更新的点
} if(!flag) break;//不存在可以更新的点了,直接退出
} //判断有无负环出现,即时间能不能减少
for(int o = ; o < E.size(); o++){
if(dis[E[o].v] > dis[E[o].u] + E[o].w) return true;
} return false;
} int main(){ ios::sync_with_stdio(false);
cin.tie(); int T;
cin >> T;
while (T--){
cin >> n >> m >> t; init();
input();
if(bellman_ford()) cout << "YES" << endl;
else cout << "NO" << endl;
} getchar();getchar();
return ;
}
kuangbin专题专题四 Wormholes POJ - 3259的更多相关文章
- (最短路 spfa)Wormholes -- poj -- 3259
http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions ...
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- Wormholes - poj 3259 (Bellman-Ford算法)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34934 Accepted: 12752 Description W ...
- ShortestPath:Wormholes(POJ 3259)
田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...
- Wormholes POJ - 3259 spfa判断负环
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...
- 「kuangbin带你飞」专题十四 数论基础
layout: post title: 「kuangbin带你飞」专题十四 数论基础 author: "luowentaoaa" catalog: true tags: mathj ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- 最短路(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 ...
随机推荐
- glyphicons-halflings-regular.ttf:1 Failed to load resource: net::ERR_FILE_NOT_FOUND
下载替换 https://gitlab.com/mailman/mailman-website/tree/a97d6b4c5b29594004e3855f1ab1222449d0c211/conten ...
- WGS 全基因组测序数据分析
1. DNA测序技术 https://www.jianshu.com/p/6122cecec54a 2.FASTA和FASTQ文件格式 https://www.jianshu.com/p/50ff30 ...
- dnsperf
github 地址:https://github.com/DNS-OARC/dnsperf mac安装:brew install dnsperf 参数详解 Dnsperf 支持下面的这些命令行参数: ...
- k8s 相关的命令
查看node信息 kubectl describe node k8s-node- 查看dashboard token kubectl describe pod kubernetes-dashboard ...
- java利用注解及反射做通用的入参校验
一.原理: 1.做一个field注解,注解有两个参数:是否必填.toString之后的最大长度 2.对某个request类(或基类),使用注解标记某个字段的校验详情 3.通用的static方法,利用反 ...
- HP Client Security Manager
HP Client Security Manager - SP77916 操作系统:windows 10 64位 HP Client Security Manager - SP77916.ex ...
- Nginx配置反向代理支持WebSocket
http { #WebSocket代理配置 map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { ...
- JS面向对象的类 实例化与继承
JS中 类的声明有两种形式: // 类的声明 function Animal() { this.name = 'name' } // ES6中的class声明 class Animal2 { cons ...
- 哈夫曼树的构建(C语言)
哈夫曼树的构建(C语言) 算法思路: 主要包括两部分算法,一个是在数组中找到权值最小.且无父结点两个结点位置,因为只有无父结点才能继续组成树: 另一个就是根据这两个结点来修改相关结点值. 结构定义 ...
- [EXP]CVE-2019-0604 Microsoft SharePoint RCE Exploit
研表究明,汉字的序顺并不定一能影阅响读,比如当你看完这句话后,才发这现里的字全是都乱的. 剑桥大学的研究结果,当单词的字母顺序颠倒时,你仍旧可以明白整个单词的意思.其中重要的是:只要单词的第一个字母和 ...