QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#649598#7991. 最小环ucup-team3519WA 288ms66084kbC++234.4kb2024-10-18 03:11:232024-10-18 03:11:24

Judging History

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

  • [2024-10-18 03:11:24]
  • 评测
  • 测评结果:WA
  • 用时:288ms
  • 内存:66084kb
  • [2024-10-18 03:11:23]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> pi;
typedef pair<LL, LL> pl;
typedef __int128_t LLL;
#define fi first
#define lb lower_bound
#define se second
#define V vector
#define pb push_back
#define all0(x) (x).begin(), (x).end()
#define all1(x) (x).begin() + 1, (x).end()
const int INF = 2e9 + 100;

const int N = 3e5 + 100;
namespace HDL {
    V<pl> e[N];
    LLL dep[N], rdep[N];
    int f[N], sz[N], hs[N], top[N];
    int l[N], r[N], id[N], tot;
    void dfs1(int x, LL w, int p) {
        dep[x] = dep[p] + w, f[x] = p, sz[x] = 1, hs[x] = -1;
        for(auto [y, w] : e[x]) {
            if(y == p) {
                rdep[x] = rdep[y] + w;
                continue;
            }
            dfs1(y, w, x);
            sz[x] += sz[y];
            if(hs[x] == -1 || sz[y] > sz[hs[x]]) hs[x] = y;
        }
    }
    void dfs2(int x, int t) {
        top[x] = t, l[x] = ++tot, id[l[x]] = x;
        if(hs[x] != -1) {
            dfs2(hs[x], t);
        }
        for(auto [y, w] : e[x]) {
            if(y == f[x] || y == hs[x]) continue;
            dfs2(y, y);
        }
        r[x] = tot;
    }
    void ini(int rt) {
        tot = 0;
        dfs1(rt, 1, 0), dfs2(rt, rt);
    }
    int LCA(int a, int b) {
        while(top[a] != top[b]) {
            if(dep[top[a]] > dep[top[b]]) a = f[top[a]];
            else b = f[top[b]];
        }
        return (dep[a] > dep[b] ? b : a);
    }
}
using HDL::LCA, HDL::e, HDL::dep, HDL::rdep;
bool cmp(int x, int y) {
    return HDL::l[x] < HDL::l[y];
}
V<V<pl>> ve;
V<int> vid;

LL my_abs(LLL x) {
    if(x < 0) x = -x;
    return min((LLL)1e18, x);
}

int u(int x) {
    return lb(all1(vid), x, cmp) - vid.begin();
}

int build(const V<int>& cur) {
    vid = cur;
    sort(all1(vid), cmp);
    for(int i = 1; i < (int)cur.size() - 1; i++) vid.pb(LCA(vid[i], vid[i + 1]));
    sort(all1(vid), cmp);
    vid.erase(unique(all1(vid)), vid.end());
    int tot = (int)vid.size() - 1;
    ve.assign(tot + 1, V<pl>());
    for(int i = 1; i < tot; i++) {
        int p = LCA(vid[i], vid[i + 1]);
        int son = vid[i + 1];
        if(my_abs(dep[son] - dep[p]) != (LL)1e18) {
            ve[u(p)].pb({i + 1, my_abs(dep[son] - dep[p])});
        } else {
            ve[u(son)].pb({u(p), my_abs(rdep[son] - rdep[p])});
        }
    }
    return tot;
}

namespace DSU {
    int f[N], siz[N];
    int par(int x) {
        if(x == f[x]) return x;
        return f[x] = par(f[x]);
    }
    void mer(int a, int b) {
        a = par(a), b = par(b);
        if(a == b) return;
        f[a] = b;
        siz[b] += siz[a];
    }
    void ini(int n) {
        for(int i = 1; i <= n; i++) f[i] = i, siz[i] = 1;
    }
}


void solve() {
    int n, m; cin >> n >> m;
    DSU::ini(n);
    
    LL ans = 1e18;

    V<array<int, 3>> extra;
    for(int i = 1; i <= m; i++) {
        int a, b, c; cin >> a >> b >> c;
        if(a == b) ans = min(ans, (LL)c);
        if(DSU::par(a) != DSU::par(b)) {
            e[a].pb({b, c});
            e[b].pb({a, (LL)1e18});
            DSU::mer(a, b);
        } else {
            extra.pb({a, b, c});
        }
    }

    HDL::ini(1);

    V<int> key_point(1);
    for(auto [x, y, c] : extra) {
        key_point.pb(x);
        key_point.pb(y);
    }
    sort(all1(key_point));
    key_point.erase(unique(all1(key_point)), key_point.end());

    int tot = build(key_point);

    for(auto [x, y, c] : extra) {
        ve[u(x)].pb({u(y), c});
    }

    priority_queue<pl, V<pl>, greater<pl>> pq;
    V<LL> dis(tot + 1, -1);


    for(int x = 1; x <= tot; x++) {
        dis.assign(tot + 1, -1);
        for(auto [y, w] : ve[x]) {
            pq.push({w, y});
        }
        while(pq.size()) {
            auto [w, x] = pq.top();
            pq.pop();
            if(w >= 1e18) continue;
            if(dis[x] != -1) continue;
            dis[x] = w;
            for(auto [y, w] : ve[x]) {
                if(dis[y] != -1) continue;
                pq.push({dis[x] + w, y});
            }
        }
        if(dis[x] != -1) ans = min(ans, dis[x]);
    }
    assert(ans >= 0);
    if(ans >= 1e17) {
        assert(ans == 1e18);
        cout << -1 << endl;
    } else cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t = 1; 
    // cin >> t;
    while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2ms
memory: 11840kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

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

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 115ms
memory: 66084kb

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: -100
Wrong Answer
time: 288ms
memory: 41676kb

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:

7410857674

result:

wrong answer 1st lines differ - expected: '98674714245', found: '7410857674'