QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#442162#8232. Yet Another Shortest Path QueryRong7WA 48ms328140kbC++147.1kb2024-06-15 09:32:012024-06-15 09:32:02

Judging History

你现在查看的是最新测评结果

  • [2024-06-15 09:32:02]
  • 评测
  • 测评结果:WA
  • 用时:48ms
  • 内存:328140kb
  • [2024-06-15 09:32:01]
  • 提交

answer

// Not afraid to dark.

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

clock_t start_time, end_time;
#define GET_START start_time = clock ();
#define GET_END end_time = clock (); fprintf (stderr, "TIME COSSEMED : %0.3lf\n", 1.0 * (end_time - start_time) / CLOCKS_PER_SEC);
#define inline __inline__ __attribute__ ((always_inline))

namespace io {
    int read_pos, read_dt; char read_char;
    inline int read (int &p = read_pos){
        p = 0, read_dt = 1; read_char = getchar ();
        while (! isdigit (read_char)){
            if (read_char == '-')
                read_dt = - 1;
            read_char = getchar ();
        }
        while (isdigit (read_char)){
            p = (p << 1) + (p << 3) + read_char - 48;
            read_char = getchar ();
        }
        return p = p * read_dt;
    }
    int write_sta[65], write_top;
    inline void write (int x){
        if (x < 0)
            putchar ('-'), x = - x;
        write_top = 0;
        do
            write_sta[write_top ++] = x % 10, x /= 10;
        while (x);
        while (write_top)
            putchar (write_sta[-- write_top] + 48);
    }
    int llen;
    inline int get_string (char c[], int &len = llen){
        len = 0;
        read_char = getchar ();
        while (read_char == ' ' || read_char == '\n' || read_char == '\r')
            read_char = getchar ();
        while (read_char != ' ' && read_char != '\n' && read_char != '\r'){
            c[++ len] = read_char;
            read_char = getchar ();
        }
        return len;
    }
}

const int N = 1e6, inf = 0x3f3f3f3f;
int n, m;
int firs[N + 5], nex[N * 2 + 5], to[N * 2 + 5], w[N * 2 + 5], tot = 1;
int deg[N + 5];
bool vis[N + 5];

vector < int > gL[N + 5], tL[N + 5], gR[N + 5], tR[N + 5];

inline void Add (int u, int v, int l){
    ++ deg[u], ++ deg[v];
    ++ tot;
    nex[tot] = firs[u];
    firs[u] = tot;
    to[tot] = v;
    w[tot] = l;
    ++ tot;
    nex[tot] = firs[v];
    firs[v] = tot;
    to[tot] = u;
    w[tot] = l;
}
inline void Add_L (int u, int v, int e){
    gL[v].push_back (e ^ 1);
    tL[u].push_back (e);
}
inline void Add_R (int u, int v, int e){
    gR[v].push_back (e ^ 1);
    tR[u].push_back (e);
}

int Q, Ans[N * 10 + 5];
pair < int , int > Query[N + 5];

unordered_map < int , vector < int > > QS[N + 5], QT[N + 5];
unordered_map < int , vector < int > > qS[N + 5], qT[N + 5];
unordered_map < int , int > TT;

signed main (){
    GET_START

    io::read (n), io::read (m);
    for (int i = 1, u, v, l;i <= m;++ i){
        io::read (u), io::read (v), io::read (l);
        Add (u, v, l);
    }
    queue < int > Que;
    for (int i = 1;i <= n;++ i)
        if (deg[i] <= 5)
            Que.push (i);
    while (! Que.empty ()){
        int u = Que.front ();
        Que.pop ();
        vis[u] = true;
        for (int e = firs[u], v;e;e = nex[e]){
            v = to[e];
            -- deg[v];
            if (! vis[v])
                Add_L (v, u, e ^ 1), Add_R (u, v, e);
            if (deg[v] == 5)
                Que.push (v);
        }
    }
    io::read (Q);
    for (int i = 1, s, t;i <= Q;++ i){
        io::read (s), io::read (t);
        Query[i] = make_pair (s, t);
        QS[s][t].push_back (i), qS[s][t].push_back (i);
        QT[t][s].push_back (i), qT[t][s].push_back (i);
        int ned = i;
        for (int e : tR[s]){
            int u = to[e];
            ned += Q;
            qS[u][t].push_back (ned);
            qT[t][u].push_back (ned);
        }
        for (int e : gL[t]){
            int v = to[e];
            ned += Q;
            qS[s][v].push_back (ned);
            qT[v][s].push_back (ned);
        }
    }
    for (int i = 1;i <= Q;++ i)
        for (int j = 0;j < 10;++ j)
            Ans[i + j * Q] = inf;
    for (int i = 1;i <= n;++ i){
        TT.clear ();
        TT[i] = 0;
        int j, u, v, x, y, z;
        for (int ej : tL[i]){
            j = to[ej], x = w[ej];
            if (! TT.count (j) || TT[j] > x)
                TT[j] = x;
            for (auto eu : tR[j]){
                u = to[eu], y = w[eu];
                if (! TT.count (u) || TT[u] > x + y)
                    TT[u] = x + y;
            }
        }
        for (auto ej : tR[i]){
            j = to[ej], x = w[ej];
            if (! TT.count (j) || TT[j] > x)
                TT[j] = x;
            for (auto eu : tR[j]){
                u = to[eu], y = w[eu];
                if (! TT.count (u) || TT[u] > x + y)
                    TT[u] = x + y;
            }
        }
        for (auto re : TT){
            tie (j, x) = re;
            for (int t : qS[i][j])
                Ans[t] = min (Ans[t], x);
        }
        for (int ej : tL[i]){
            j = to[ej], x = w[ej];
            for (int eu : tR[j]){
                u = to[eu], y = w[eu];
                for (int ev : tR[u]){
                    v = to[ev], y = w[ev];
                    if (! TT.count (v) || TT[v] > x + y + z)
                        TT[v] = x + y + z;
                }
            }
        }
        for (auto re : TT){
            tie (j, x) = re;
            for (int t : QS[i][j])
                Ans[t] = min (Ans[t], x);
        }

        TT.clear ();
        TT[i] = 0;
        for (int ej : gR[i]){
            j = to[ej], x = w[ej];
            if (! TT.count (j) || TT[j] > x)
                TT[j] = x;
            for (int eu : gL[j]){
                u = to[eu], y = w[eu];
                if (! TT.count (u) || TT[u] > x + y)
                    TT[u] = x + y;
            }
        }
        for (int ej : gL[i]){
            j = to[ej], x = w[ej];
            if (! TT.count (j) || TT[j] > x)
                TT[j] = x;
            for (int eu : gL[j]){
                u = to[eu], y = w[eu];
                if (! TT.count (u) || TT[u] > x + y)
                    TT[u] = x + y;
            }
        }
        for (auto re : TT){
            tie (j, x) = re;
            for (int t : qT[i][j])
                Ans[t] = min (Ans[t], x);
        }
        for (int ej : gR[i]){
            j = to[ej], x = w[ej];
            for (int eu : gL[j]){
                u = to[eu], y = w[eu];
                for (int ev : gL[u]){
                    v = to[ev], z = w[ev];
                    if (! TT.count (v) || TT[v] > x + y + z)
                        TT[v] = x + y + z;
                }
            }
        }
        for (auto re : TT){
            tie (j, x) = re;
            for (int t : QT[i][j])
                Ans[t] = min (Ans[t], x);
        }
        qS[i].clear (), QS[i].clear (), qT[i].clear (), QT[i].clear ();
    }
    for (int i = 1, s, t;i <= Q;++ i){
        tie (s, t) = Query[i];
        int ned = i;
        for (int e : tR[s]){
            int l = w[e];
            ned += Q;
            Ans[i] = min (Ans[i], Ans[ned] + l);
        }
        for (int e : gL[t]){
            int l = w[e];
            ned += Q;
            Ans[i] = min (Ans[i], Ans[ned] + l);
        }
        if (Ans[i] ^ inf)
            io::write (Ans[i]), puts ("");
        else
            puts ("-1");
    }

    GET_END
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 48ms
memory: 328140kb

input:

6 9
1 2 4
2 3 6
3 6 5
6 5 3
5 4 2
4 1 3
3 4 9
1 3 100
5 3 1
5
1 3
1 6
3 4
3 5
2 5

output:

6
8
3
1
-107

result:

wrong answer 5th numbers differ - expected: '7', found: '-107'