大佬们帮忙看看
查看原帖
大佬们帮忙看看
1128181
Evolution333楼主2023/12/13 10:41
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int,int> PII;

const int N = 1e5+10, M = 2e5+10;

int h[N], w[M], e[M], ne[M],idx;
int dist[N], cnt[N];
bool st[N];
int T,n,m;

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

bool spfa()
{
    queue<int> q;
    st[1] = true;
    q.push(1);

    while(q.size())
    {
        int t = q.front();
        q.pop();
        st[t] = false;

        for(int i=h[t];i != -1;i = ne[i])
        {
            int j = e[i];
            if(dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                cnt[j] = cnt[t] + 1;
                if(cnt[j] >= n) return true;
                if(!st[j])
                {
                    st[j] = true;
                    q.push(j);
                }
            }
        }
    }

    return false;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(h,-1,sizeof h);
        scanf("%d%d",&n,&m);
        while(m--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(c >= 0) add(b,a,c);
            add(a,b,c);

        }
        if(spfa()) puts("Yes");
        else puts("No");
    }

    return 0;
}


2023/12/13 10:41
加载中...