QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#738053#7991. 最小环MiniLongTL 1385ms212480kbC++145.7kb2024-11-12 17:36:012024-11-12 17:36:01

Judging History

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

  • [2024-11-12 17:36:01]
  • 评测
  • 测评结果:TL
  • 用时:1385ms
  • 内存:212480kb
  • [2024-11-12 17:36:01]
  • 提交

answer

#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<ll, ll> PII;
namespace fastio{
    #ifdef ONLINE_JUDGE
    char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
    #define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
    #else
    #define get() getchar()
    #endif
    template<typename T> inline void read(T &t){
        T x = 0, f = 1;
        char c = getchar();
        while(!isdigit(c)){
            if(c == '-') f = -f;
            c = getchar();
        }
        while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
        t = x * f;
    }
    template<typename T, typename ... Args> inline void read(T &t, Args&... args){
        read(t);
        read(args...);
    }
    template<typename T> void write(T t){
        if(t < 0) putchar('-'), t = -t;
        if(t >= 10) write(t / 10);
        putchar(t % 10 + '0');
    }
    template<typename T, typename ... Args> void write(T t, Args... args){
        write(t), putchar(' '), write(args...);
    }
    template<typename T> void writeln(T t){
        write(t);
        puts("");
    }
    template<typename T> void writes(T t){
        write(t), putchar(' ');
    }
    #undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
    const ll mod = 998244353;
    ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
    void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
    void add(ll &x, ll y){x = (x + y) % mod;}
    void mul(ll &x, ll y){x = x * y % mod;}
    ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
    ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
    ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 4e5 + 5, M = 3005;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n, m;
vector<PII> G[N], g[N];
struct edge{
    int u, v, w;
}e[N];
int cnt, id[N], rev[N];
int dep[N], w[N];
ll dis[N];
bool flag[N];
void dfs(int u, int fa, int lst = 0){
    dep[u] = dep[fa] + 1;
    for(auto &[v, i] : G[u]){
        if(dep[v]){
            if(i != lst) flag[i] = 1;
            continue;
        }
        g[u].pb({v, i});
        w[v] = e[i].u == u, dis[v] = dis[u] + e[i].w;
        dfs(v, u, i);
    }
}
int pre[N][25];
PII f[N][25];
PII operator+(PII x, PII y){return make_pair(x.fi + y.fi, (x.se == -1 || y.se == -1 || x.se != y.se) ? -1 : x.se);}
void dfs2(int u){
    _rep(i, 1, 20) f[u][i] = f[u][i - 1] + f[pre[u][i - 1]][i - 1], pre[u][i] = pre[pre[u][i - 1]][i - 1];
    for(auto &[v, i] : g[u]){
        f[v][0] = {e[i].w, w[v]}, pre[v][0] = u;
        dfs2(v);
    }
}
int lca(int x, int y){
    if(dep[x] < dep[y]) swap(x, y);
    int h = dep[x] - dep[y], p = 0;
    while(h){if(h & 1) x = pre[x][p]; h >>= 1, p++;}
    if(x == y) return x;
    _req(i, 20, 0) if(pre[x][i] != pre[y][i]) x = pre[x][i], y = pre[y][i];
    return pre[x][0];
}
PII jump(int u, int h){
    PII cur = {0, 0};
    _req(i, 20, 0){
        if(h >> i & 1){
            if(!cur.fi) cur = f[u][i];
            else cur = cur + f[u][i];
            u = pre[u][i];
        }
    }
    return cur;
}
ll d[M], nxt[M][M];
bool vis[N];
ll dij(int s, ll *dis){
    _rep(i, 1, cnt) dis[i] = inf, vis[i] = 0;
    priority_queue<PII, vector<PII>, greater<PII> > q;
    q.push({dis[s] = 0, s});
    while(q.size()){
        int u = q.top().se; q.pop();
        if(vis[u]) continue;
        vis[u] = 1;
        for(auto &[v, w] : G[u]){
            if(dis[v] > dis[u] + w){
                dis[v] = dis[u] + w;
                q.push({dis[v], v});
            }
        }
    }
    ll res = inf;
    _rep(i, 1, cnt) if(nxt[i][s] != inf) res = min(res, dis[i] + nxt[i][s]);
    return res;
}
int main(){
    read(n, m);
    _rep(i, 1, m){
        int u, v, w; read(u, v, w);
        e[i] = {u, v, w};
        G[u].pb({v, i}), G[v].pb({u, i});
    }
    dfs(1, 0);
    _rep(i, 1, n) G[i].clear();
    _rep(i, 1, m){
        if(!flag[i]) continue;
        if(!id[e[i].u]) rev[id[e[i].u] = ++cnt] = e[i].u;
        if(!id[e[i].v]) rev[id[e[i].v] = ++cnt] = e[i].v;
        G[id[e[i].u]].pb({id[e[i].v], e[i].w});
    }
    dfs2(1);
    _rep(i, 1, cnt){
        _rep(j, 1, cnt){
            if(i == j) continue;
            int x = rev[i], y = rev[j], z = lca(x, y);
            int u = i, v = j;
            if(dep[x] > dep[y]) swap(x, y), swap(u, v);
            if(z == x){
                PII cur = jump(y, dep[y] - dep[x]);
                if(cur.se == -1) continue;
                if(cur.se == 1) G[u].pb({v, cur.fi});
                else G[v].pb({u, cur.fi});
                continue;
            }
            // debug("x:%d y:%d z:%d\n", x, y, z);
            PII L = jump(x, dep[x] - dep[z]), R = jump(y, dep[y] - dep[z]);
            if(L.se == -1 || R.se == -1 || L.se == R.se) continue;
            if(L.se) G[v].pb({u, L.fi + R.fi});
            else G[u].pb({v, L.fi + R.fi});
        }
    }
    _rep(i, 1, cnt) _rep(j, 1, cnt) nxt[i][j] = inf;
    _rep(i, 1, cnt) for(auto &[j, w] : G[i]) nxt[i][j] = min(nxt[i][j], w);
    ll ans = inf;
    _rep(i, 1, cnt){
        ans = min(ans, dij(i, d));
    }
    writeln(ans == inf ? -1 : ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 32456kb

input:

4 6
1 2 1
4 3 3
4 1 9
2 4 1
3 1 2
3 2 6

output:

7

result:

ok single line: '7'

Test #2:

score: 0
Accepted
time: 3ms
memory: 24068kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 0ms
memory: 30496kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 230ms
memory: 210676kb

input:

258420 258419
33061 33062 767169384
212916 212917 1741339
229881 229882 896760805
173467 173468 273055172
233189 233190 800433307
10157 10158 126766550
174605 174606 552176083
224030 224031 886617880
229102 229103 783848581
67588 67589 510826095
233648 233649 879695751
214453 214454 867104578
153140...

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 821ms
memory: 202520kb

input:

248016 248896
82688 82755 592665252
202847 203260 294408121
26821 28237 269132335
150967 152178 3125829
246069 247390 29492546
7470 7673 55843492
33975 35414 442802995
28451 28948 193736988
34484 34637 84441058
60168 60309 501991354
79579 79844 26854803
239672 239706 111702667
73548 73658 149840530
...

output:

98674714245

result:

ok single line: '98674714245'

Test #6:

score: 0
Accepted
time: 689ms
memory: 212480kb

input:

270530 271285
80489 81855 218173724
188930 190845 783975756
29830 30626 22189315
234320 234472 70840355
198096 198272 300313423
224194 226906 105128197
115010 115834 37228105
134788 135583 18647938
257292 257358 98569041
146988 147215 69398857
248752 250002 409565478
62128 63751 839744551
121918 122...

output:

133486910467

result:

ok single line: '133486910467'

Test #7:

score: 0
Accepted
time: 1385ms
memory: 203488kb

input:

222087 223141
123107 123811 2984035
216346 217464 675263
139741 141286 892140
77973 78018 453931100
38603 39546 157182459
13105 14616 775862
97035 97704 379136464
86254 88311 84193802
83968 84398 246202498
152486 160164 65619516
73213 73517 1129576
15618 16541 498613468
192241 195576 889879
21363 21...

output:

47599478278

result:

ok single line: '47599478278'

Test #8:

score: -100
Time Limit Exceeded

input:

212718 214066
104602 105717 148385760
163427 165307 437059346
108663 111803 784753745
15490 15784 789609
77598 80118 53908869
97776 98040 78287597
26994 27717 989577
134781 134919 531908
22362 24185 185680
114422 114890 609661
192852 192861 155477
45695 45800 35773
150695 152662 511678590
101629 102...

output:


result: