10倍还RE!紧急求助!
查看原帖
10倍还RE!紧急求助!
302999
Wangxuelong楼主2020/8/14 21:24

紧急求助,求大佬看看 边数开10倍也会RE

为什么边数要开题目范围的10+倍才能过??

按理来说就算是双向边不也最多2倍的范围吗

用的是SPFA 数组模拟邻接表存边 希望大佬看一看555

#include <bits/stdc++.h>
using namespace std;

const int N = 2e3 + 10, M = 40000; //这里开30000都会RE 开40000才过了
int n, m;
int e[M], ne[M], w[M], h[N], idx;
bool st[N];
int dist[N], cnt[N];

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;
    memset(dist, 0x3f3f3f3f, sizeof dist);
    memset(cnt, 0, sizeof cnt);
    memset(st, 0, sizeof st);
    dist[1] = 0;
    q.push(1);
    st[1] = true;

    while (!q.empty())
    {
        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])
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }
    return false;
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        memset(h, -1, sizeof h);
        for (int i = 0; i < m; ++i)
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            if (c < 0)
                add(a, b, c);
            else
            {
                add(a, b, c);
                add(b, a, c);
            }
        }
        if (spfa())
            puts("YES");
        else
            puts("NO");
    }
}
2020/8/14 21:24
加载中...