很急求调
查看原帖
很急求调
833124
BIOS楼主2023/5/24 22:04
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int N = 75, M = 1e3, INF = 0x3f3f3f3f;
int dist[N], h[N], e[M], ne[M], w[M], idx, n, m, k, q, a, b, c, cnt[N];
bool st[N];
int g[N][N];
typedef pair<int, int> PII;
void add(int a, int b, int c)
{
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
int dj(int sx, int ex)
{
    memset(st, false, sizeof(st));
    for (int i = 0; i < N; i++)
        cnt[i] = dist[i] = INF;
    dist[sx] = 0, cnt[sx] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> q;
    q.push({dist[sx], sx});
    while (q.size())
    {
        PII t = q.top();
        q.pop();
        int ver = t.second;
        if (st[ver])
            continue;
        st[ver] = true;
        if (cnt[ver] != INF && cnt[ver] >= k)
            continue;
        int distance = t.first;
        for (int i = h[ver]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > distance + w[i])
            {
                dist[j] = distance + w[i], q.push({dist[j], j});
                cnt[j] = min(cnt[j], cnt[ver] + 1);
            }
        }
    }
    if (cnt[ex] > k || dist[ex] == INF)
        dist[ex] = -1;
    return dist[ex];
}
signed main()
{
    memset(h, -1, sizeof(h));
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            g[i][j] = INF;
    while (m--)
        scanf("%d%d%d", &a, &b, &c), g[a][b] = min(g[a][b], c);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (g[i][j] != INF)
                add(i, j, g[i][j]);
    cin >> k >> q;
    if (k >= n - 1)
        k = n - 1;
    while (q--)
    {
        cin >> a >> b;
        if (a == b)
            puts("0");
        else
            cout << dj(a, b) << "\n";
    }
}

这个代码交上去,大部分是RE,小部分AC,还有两三个WA。我就是不知道这怎么RE的?

2023/5/24 22:04
加载中...