题目链接: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的更多相关文章

  1. (最短路 spfa)Wormholes -- poj -- 3259

    http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions ...

  2. Wormholes POJ 3259(SPFA判负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  3. Wormholes - poj 3259 (Bellman-Ford算法)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34934   Accepted: 12752 Description W ...

  4. ShortestPath:Wormholes(POJ 3259)

    田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...

  5. Wormholes POJ - 3259 spfa判断负环

    //判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...

  6. 「kuangbin带你飞」专题十四 数论基础

    layout: post title: 「kuangbin带你飞」专题十四 数论基础 author: "luowentaoaa" catalog: true tags: mathj ...

  7. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  8. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  9. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

随机推荐

  1. python总结三

    1.线性表若采用链式存储结构的时候,要求内存中可用存储单位的地址是:连续或者不连续都可以 链式存储去找后继节点或者前驱节点是使用指针来实现的,不需要连续的内存,当然,也可以是连续的内存地址 2. 线性 ...

  2. vue子组件如何向父组件传值

    子组件: <template> <div class="app"> <input @click="sendMsg" type=&q ...

  3. 使用PhantomJS后台web界面截图

    自动化截web页面的图 一.工具介绍: PhantomJS是一个基于webkit的JavaScript API.它使用QtWebKit作为它核心浏览器的功能,使用webkit来编译解释执行JavaSc ...

  4. 修改mysql存储过程或函数的定义着

    以root用户登录mysql控制台 (1)首先查询 mysql> select definer from mysql.proc; (2)然后根据条件进行更新 update mysql.proc ...

  5. spring JDBC的应用

    原文地址:https://www.iteye.com/blog/chen106106-1574911 1:首先在类路径下面配置访问数据的一些基本信息,包括连接数据库的地址,用户,密码jdbc.prop ...

  6. 在vue中导出excel表格

    初学者学习vue开发,想把前端项目中导出Excel表格,查了众多帖子,踩了很多坑,拿出来与大家分享一下经验. 安装依赖 //npm npm install file-saver -S npm inst ...

  7. ASp.net Core EF ActionFilterAttribute AOP

    在项目中经常遇到一些数据的修改,很多时候业务方需要一个修改日志记录,这里我们计划用mssql数据库来存放日志记录,用EF来操作,记录日志可以用mvc的ActionFilterAttribute 来完成 ...

  8. UML统一建模语言介绍

    统一建模语言简介 统一建模语言(Unified Modeling Language,UML)是用来设计软件蓝图的可视化建模语言,1997 年被国际对象管理组织(OMG)采纳为面向对象的建模语言的国际标 ...

  9. 【java】单实例下的 流水号【21位】

    单实例环境,不是分布式 需要流水号 /** * 流水号生成器 * * 年+天号+毫秒+随机数 * 2019+134+480+11位随机数 * 4+3+3+11 = 21位 * * * @author ...

  10. Web负载均衡学习笔记之K8S内Ngnix微服务服务超时问题

    0x00 概述 本文是从K8S内微服务的角度讨论Nginx超时的问题 0x01 问题 在K8S内部署微服务后,发现部分微服务链接超时,Connection Time Out. 最近碰到了一个 Ngin ...