TLE飞了,快崩溃了

UVA1599 Ideal Path

yu1102 @ 2023-07-17 22:25:30

真的不知道错在哪了。跟LRJ的做法一模一样,理论上是不会超时的呀。

请dalao指点,悬赏关注。

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

const int MAXN = 2e5 + 10, MAXM = 2e5 + 10, INF = 0x3f3f3f3f;

struct Edge
{
    int from, to, nxt, c;
    Edge(int u, int v, int _nxt, int _c):from(u), to(v), nxt(_nxt), c(_c){}
    Edge(){}
} e[MAXM * 2];
int edge_cnt, hd[MAXN];
void add(int u, int v, int c)
{
    e[++edge_cnt] = Edge(u, v, hd[u], c);
    hd[u] = edge_cnt;
}

int n, m;
int d[MAXN];

void bfs()
{
    static int q[MAXN], front, rear;

    front = rear = 1;
    q[rear++] = n;

    memset(d, 0x3f, sizeof(d));
    d[n] = 0;

    while (front < rear)
    {
        int u = q[front++];
        for (int i = hd[u]; i; i = e[i].nxt)
        {
            int v = e[i].to;
            if (d[v] == INF)
            {
                d[v] = d[u] + 1;
                q[rear++] = v;
            }
        }
    }
}

void output()
{
    cout << d[1] << endl;

    static int vx[MAXN], sizx, vy[MAXN], sizy;
    static int ans[MAXN];

    vx[sizx = 1] = 1;
    for (int i = 1; i <= d[1]; i++)
    {
        int minc = INF;
        for (int j = 1; j <= sizx; j++)
        {
            int u = vx[j];
            for (int k = hd[u]; k; k = e[k].nxt) if (d[u] == d[e[k].to] + 1)
                minc = min(minc, e[k].c);           
        }

        ans[i] = minc;

        sizy = 0;
        for (int j = 1; j <= sizx; j++)
        {
            int u = vx[j];
            for (int k = hd[u]; k; k = e[k].nxt) if (d[u] == d[e[k].to] + 1)
                if (e[k].c == minc)
                    vy[++sizy] = e[k].to;           
        }

        sizx = sizy;
        for (int j = 1; j <= sizx; j++)
            vx[j] = vy[j];
    }

    cout << ans[1];
    for (int i = 2; i <= d[1]; i++)
        cout << ' ' << ans[i];

    cout << endl;
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    while (cin >> n >> m)
    {
        edge_cnt = 0;
        memset(hd, 0, sizeof(hd));
        int u, v, c;
        for (int i = 1; i <= m; i++)
        {
            cin >> u >> v >> c;
            if (u == v) continue;
            add(u, v, c);
            add(v, u, c);
        }

        bfs();
        output();
    }
}

by justin_jiajia @ 2024-08-12 10:06:58

output没有判重

改成这样:

void output()
{
    cout << d[1] << endl;

    static int vx[MAXN], sizx, vy[MAXN], sizy;
    static int ans[MAXN];

    int vis[MAXN]; #这里
    memset(vis,0,sizeof vis); #这里

    vx[sizx = 1] = 1;
    for (int i = 1; i <= d[1]; i++)
    {
        int minc = INF;
        for (int j = 1; j <= sizx; j++)
        {
            int u = vx[j];
            for (int k = hd[u]; k; k = e[k].nxt) if (d[u] == d[e[k].to] + 1&&!vis[e[k].to]) #这里
                minc = min(minc, e[k].c);           
        }

        ans[i] = minc;

        sizy = 0;
        for (int j = 1; j <= sizx; j++)
        {
            int u = vx[j];
            for (int k = hd[u]; k; k = e[k].nxt) if (d[u] == d[e[k].to] + 1&&!vis[e[k].to]) #这里
                if (e[k].c == minc)
                    vy[++sizy] = e[k].to,vis[e[k].to]=true;         
        }

        sizx = sizy;
        for (int j = 1; j <= sizx; j++)
            vx[j] = vy[j];
    }

    cout << ans[1];
    for (int i = 2; i <= d[1]; i++)
        cout << ' ' << ans[i];

    cout << endl;
}

就可以AC了


by justin_jiajia @ 2024-08-12 10:07:55

@yu1102


by yu1102 @ 2024-08-23 20:25:47

@justin_jiajia 谢谢你!我去年NOIP之后就不学了,没想到还有这个历史遗留问题。这真是可悲啊!


|