QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#643865#2289. Jail or JoyridewlzWA 0ms3572kbC++203.8kb2024-10-16 03:30:262024-10-16 03:30:27

Judging History

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

  • [2024-10-16 03:30:27]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3572kb
  • [2024-10-16 03:30:26]
  • 提交

answer

#ifndef DEBUG
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif

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

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define rep(i, a, b) for (auto i = a; i < (b); ++i)
#define repr(i, a, b) for (auto i = (a) - 1; i >= (b); --i)
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pii>;

template<class T> inline bool cmax(T &a, const T &b)
{ return a < b ? a = b, 1 : 0; }
template<class T> inline bool cmin(T &a, const T &b)
{ return b < a ? a = b, 1 : 0; }

const int inf = 0x3f3f3f3f;
const ll linf = 1e18;
const double dinf = numeric_limits<double>::infinity();

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    int n, m, p, t;
    cin >> n >> m >> p >> t;

    vector< vector< pair<int, ll> > > g(n + 1);
    vector<vll> dist(n + 1, vll(n + 1, linf));
    rep(i, 1, n + 1) {
        dist[i][i] = 0;
    }
    rep(i, 0, m) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].eb(v, w);
        g[v].eb(u, w);
        dist[u][v] = dist[v][u] = w;
    }

    rep(k, 1, n + 1) {
        rep(i, 1, n + 1) {
            rep(j, 1, n + 1) {
                cmin(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
    vector<vi> far(n + 1);
    rep(i, 1, n + 1) {
        if (sz(g[i]) <= 1) {
            continue;
        }
        ll mx = 0;
        rep(j, 1, n + 1) {
            if (dist[i][j] == mx) {
                far[i].pb(j);
            } else if (dist[i][j] > mx) {
                far[i] = {j};
                mx = dist[i][j];
            }
        }
    }
    // debug(far);

    int p1 = -1;
    for (auto &[u, _] : g[t]) {
        if (dist[p][t] == dist[p][u] + dist[u][t]) {
            p1 = u;
        }
    }

    vi reach(n + 1, 0);
    auto dfs = [&](auto &&self, int u) -> void {
        reach[u] = 1;
        for (auto &[v, _] : g[u]) {
            if (!((v == p1 && u == t) || (u == p1 && v == t)) && !reach[v]) {
                self(self, v);
            }
        }
    };
    dfs(dfs, t);
    
    ll md = -linf;
    vi t1;
    rep(i, 1, n + 1) {
        if (!reach[i]) {
            continue;
        }
        if (dist[t][i] > md) {
            md = dist[t][i];
            t1 = {i};
        } else if (dist[t][i] == md) {
            t1.pb(i);
        }
    }

    rep(i, 1, n + 1) {
        for (auto &j : far[i]) {
            cout << j << ' ';
        }
        cout << '\n';
    }

    debug(t1);
    ll ans = dist[p][t];
    
    vi was(n + 1, 0);
    vll mx(n + 1, 0);
    auto dfs2 = [&](auto &&self, int u) -> ll {
        if (was[u] == 2) {
            return mx[u];
        } else if (was[u] == 1) {
            return linf;
        }
        // if (sz(g[u]) == 1) {
        //     return 0;
        // }
        was[u] = 1;
        for (auto &v : far[u]) {
            cmax(mx[u], self(self, v) + dist[u][v]);
            // if (was[v] == 0) {
            //     cmax(mx[u], dist[u][v] + self(self, v));
            // } else if (was[v] == 1) {
            //     return mx[u] = linf;
            // } else {
            //     cmax(mx[u], dist[u][v] + mx[v]);
            // }
        }
        was[u] = 2;
        return mx[u];
    };
    
    for (auto &i : t1) {
        debug(i, dfs2(dfs2, i));
        cmax(ans, dfs2(dfs2, i) + dist[p][t] + dist[t][i]);
        // debug(ans);
    }   

    if (ans > ll(1e17)) {
        cout << "impossible\n";
    } else {
        cout << ans << '\n';
    }

    return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3572kb

input:

9 10 1 2
1 2 225869
2 3 1772
3 4 314393
4 5 692250
5 6 684107
4 6 532792
3 7 441133
7 8 468183
8 9 186297
7 9 228792

output:


5 
5 
8 
8 
8 
5 
5 
5 
impossible

result:

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