P2850 [USACO06DEC] Wormholes G

原题链接

题解

1.虫洞等价于建立负权边
2.回到过去等价于存在负权环

这里就相当于检测是否存在负权环,怎么判定呢?广搜,对于任意不含有负权环的,任意两点间的点数一定小于n
如果存在负权环,那么搜索会一直沿着这个环进行下去,其路径的点数会大于n

code

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int to,val;
};

vector<node> G[600];
int n,m,w;
int road[600]={0};
int times[600]={0};
int ss(int now)
{
    queue<int> q;
    q.push(now);
    road[now]=1;
    times[now]=0;
    while(q.size())
    {
        int now=q.front();
        q.pop();

        if(road[now]>n)  return 1;

        for(auto next:G[now])
        {
            int to=next.to,val=next.val;

            if(times[to]>times[now]+val)
            {
                road[to]=road[now]+1;//代表最新一次更新的情况,即最短路上包含的点数
                times[to]=times[now]+val;
                q.push(to);
            }
        }
    }

    return 0;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>w;
        for(int i=1;i<=m;i++)
        {
            int x,y,c;
            cin>>x>>y>>c;
            G[x].push_back({y,c});
            G[y].push_back({x,c});
        }

        for(int i=1;i<=w;i++)
        {
            int x,y,c;
            cin>>x>>y>>c;
            G[x].push_back({y,-c});
        }

        memset(road,0,sizeof road);
        memset(times,0x3f,sizeof times);

        int i=1;
        for(;i<=n;i++)  if(!road[i]&&ss(i)) break;

        if(i==n+1) puts("NO");
        else puts("YES");
        for(int i=1;i<=n;i++) G[i].clear();
    }
    return 0;
}


posted @   纯粹的  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示