POJ 3259 Wormholes(负权环路)

2023-05-16

题意:

农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单向的,去到虫洞之后时间会倒退T秒,如果能遇到离开之前的自己就输出YES,反之就是NO。

分析:

就是求一幅图中有没有负权环路, 可以bellman n-1次后再跑一次看看能不能更新, 能更新说明有环。

也可以spfa记录入队次数, 入队次数大于等于N说明有负权环路


#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define _rep(i,a,b) for(int i = a; i <= b; i++)
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 1000, maxm = 10000;
int N,M,W;
struct E
{
    int v, time, nxt;
}Edge[maxm];

int cnt = 0;
int head[maxn], enter_cnt[maxn];
int dis[maxn], vis[maxn];
void add_edge(int u , int v, int d){
    Edge[cnt].v = v;
    Edge[cnt].time = d;
    Edge[cnt].nxt = head[u];
    head[u] = cnt++;
}
bool spfa(){
    fill(dis, dis+maxn, inf);
    memset(vis, 0, sizeof(vis));
    memset(enter_cnt, 0, sizeof(enter_cnt));
    queue<int> q;
    q.push(1);
    dis[1] = 0;
    vis[1] = 1;
    enter_cnt[1]++;
    while(!q.empty()){
        int u = q.front();
        for(int i = head[u]; i != -1; i = Edge[i].nxt){
            int v = Edge[i].v, d = Edge[i].time;
            if(dis[v] > dis[u] + d){
                if(++enter_cnt[v] >= N) return false; //如果入队次数 >= N, 那么一定有负权环路 
                dis[v] = dis[u] + d;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
        vis[u] = 0;
        q.pop();
    }
    return true;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(head, -1, sizeof(head));
        cnt = 0;

        scanf("%d %d %d", &N, &M, &W);
        for(int i = 0; i < M; i++)
        {
            int tu,tv,tt;
            scanf("%d %d %d", &tu, &tv, &tt);
            add_edge(tu,tv,tt);
            add_edge(tv,tu,tt);
        }
        for(int i = 0; i < W; i++)
        {
            int tu,tv,tt;
            scanf("%d %d %d", &tu, &tv, &tt);
            add_edge(tu,tv,-tt);
        }

        if(!spfa()) printf("YES\n");
        else printf("NO\n");
    }
}  

 

转载于:https://www.cnblogs.com/Jadon97/p/8340657.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POJ 3259 Wormholes(负权环路) 的相关文章

  • POJ 题目1105 S-Trees(二叉树模拟)

    S Trees Time Limit 1000MS Memory Limit 10000KTotal Submissions 1499 Accepted 807 Description A Strange Tree S tree over
  • POJ-2453

    As we known data stored in the computers is in binary form The problem we discuss now is about the positive integers and
  • POJ 2449 Remmarguts‘ Date---SPFA求评估函数 + A*最小堆BFS

    POJ 2449 Remmarguts Date Time Limit 4000MS Memory Limit 65536K Description Good man never makes girls wait or breaks an
  • POJ 3259 Wormholes(负权环路)

    题意 xff1a 农夫约翰农场里发现了很多虫洞 xff0c 他是个超级冒险迷 xff0c 想利用虫洞回到过去 xff0c 看再回来的时候能不能看到没有离开之前的自己 xff0c 农场里有N块地 xff0c M条路连接着两块地 xff0c W
  • poj 1068 parencondings

    题目描述 xff1a 定义 S 为一个合法的括号字符串 S 可以用以下两种方式编码 xff1a 1 用一个整数数组 P 来表示 xff0c 其中元素 p i 是 S 中每个 39 39 前的 39 39 的个数 xff1b 2 用一个整数数
  • poj 2513 colored sticks

    代码 include lt iostream gt include lt stdio h gt using namespace std define MAX 27 define S 500003 struct Node int id Nod
  • 汉诺塔问题(Hanoi)-python递归实现

    描述 描述 一 汉诺塔问题 有三根杆子A B C A杆上有N个 N gt 1 穿孔圆盘 盘的尺寸由下到上依次变小 要求按下列规则将所有圆盘移至C杆 每次只能移动一个圆盘 大盘不能叠在小盘上面 提示 可将圆盘临时置于B杆 也可将从A杆移出的圆
  • poj 2096 Collecting Bugs

    Problem poj org problem id 2096 vjudge net contest 151678 problem Q Reference blog csdn net xingyeyongheng article detai
  • poj 1742 Coins

    Problem poj org problem id 1742 Reference www cppblog com flyinghearts archive 2010 09 01 125555 html blog csdn net wang
  • POJ--2709:Painter (贪心)

    1 题目源地址 http poj org problem id 2709 2 解题思路 每个颜料盒可能有3 12种颜色 其中每种颜色50ml 任意三种颜色 假设每种颜色Xml 可以混合出Xml的灰色 现在给出所需颜色的种数N 给出N个值分别
  • POJ 2479 Dual Core CPU|网络流|dinic模版

    问题描述 总时间限制 15000ms 单个测试点时间限制 5000ms 内存限制 65536kB 描述 As more and more computers are equipped with dual core CPU SetagLilb
  • 【题解】poj2689(LibreOJ10197) 线性筛

    题目链接 筛出2到sqrt u 的所有质数 再标记 l u 中是质数p倍数的数 最后枚举相邻质数 部分代码实现参考了大佬题解 题目描述 给定两个整数 L R L R L R 求闭区间 L
  • POJ--1458:Common Subsequence (DP求最长公共子序列)

    1 题目源地址 http poj org problem id 1458 2 基本题意 给出两个序列 求出最长子序列的长度并输出 经典的动态规划求解 求最长公共子序列的经典DP解法代价为O mn 其中m和n分别为两个字符串的长度 具体实现如
  • poj 1330 Nearest Common Ancestors

    Problem poj org problem id 1330 vjudge net contest 80844 problem C Meaning 求最近公共祖先 Note 真 LCA 模版题 那就备份一发 LCA 模版 链式前向星存图
  • POJ--1159:Palindrome (DP求最长公共子序列)

    1 题目源地址 http poj org problem id 1159 2 题目大意 题目就是给你一个字符串 问你添加最少几个字符之后字符串变成回文字符串 求给出的字符串和逆序的字符串的最长公共子序列 用总长度减去这个最长公共子序列的长度
  • POJ 2659 Raid|分治法|平面最近点对

    题目描述 总时间限制 1000ms 内存限制 65536kB 描述 After successive failures in the battles against the Union the Empire retreated to its
  • poj 2155 Matrix

    Problem poj org problem id 2155 vjudge net contest 146952 problem A Meaning 一个 N N 的矩阵 A 初始时全部值为 0 有两种操作 1 C x1 y1 x2 y2
  • poj 1195 Mobile phones

    Problem poj org problem id 1195 vjudge net contest 146952 problem C Meaning 有一个 S S 的正方形区域 两维的下标范围都是是 0 S 1 有 4 种操作 1 0
  • poj1240

    本题为已知M 叉树的前序遍历与后序遍历 要求给出对应树有多少种可能 与poj2255类似 只要划分出子树 就把问题规模缩小了 然后就可以递归 目前只会写递归 M叉树的前序遍历为 根 子树1 子树2 子树K K lt M M叉树的后序遍历为
  • poj1463

    1

随机推荐