Submission #1301131

#TimeUsernameProblemLanguageResultExecution timeMemory
1301131azamuraiOlympic Bus (JOI20_ho_t4)C++20
21 / 100
380 ms1924 KiB
#include <bits/stdc++.h>

#define ll long long
#define fi first
#define se second
#define mp make_pair
#define Sz(x) (int)x.size()

using namespace std;

const int N = 205;
const int inf = 1e9;
int n, m, can_to1[N], can_toN[N], can_from1[N], can_fromN[N], used[N], cnt[N][N], mn[N][N];
vector <int> g[N];

void dfs(int v, int tp) {
    used[v] = 1;
    if (!tp) can_from1[v] = 1;
    else can_fromN[v] = 1;
    for (auto to : g[v]) {
        if (!used[to]) {
            dfs(to, tp);
        }
    }
}

void dfs2(int v, int tp) {
    used[v] = 1;
    if (!tp) can_to1[v] = 1;
    else can_toN[v] = 1;
    for (auto to : g[v]) {
        if (!used[to]) {
            dfs2(to, tp);
        }
    }
}

bool check(int v, int goal) {
    used[v] = 1;
    if (v == goal) return true;
    for (auto to : g[v]) {
        if (!used[to] && cnt[v][to] > 0) {
            if (check(to, goal)) return true;
        }
    }
    return false;
}

void solve() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            mn[i][j] = inf;
        }
    }
    vector <pair<int,pair<int,int>>> save;
    vector <pair<int,int>> save2;
    for (int i = 1; i <= m; i++) {
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        cnt[u][v]++;
        if (cnt[u][v] == 1) save2.push_back(mp(u, v));
        mn[u][v] = min(mn[u][v], d);
        g[u].push_back(v);
        save.push_back(mp(d, mp(u, v)));
    }

    int ok1 = check(1, n);
    for (int i = 1; i <= n; i++) {
        used[i] = 0;
    }
    int ok2 = check(n, 1);

    if (ok1 && ok2) {
        cout << 0;
        return;
    }
    
    for (int i = 1; i <= n; i++) {
      used[i] = 0;
    }
    sort(save.begin(), save.end());
    dfs(1, 0);
    for (int i = 1; i <= n; i++) {
        used[i] = 0;
    }
    dfs(n, 1);
    for (int i = 1; i <= n; i++) {
        used[i] = 0;
        g[i].clear();
    }
    for (auto to : save) {
        int u = to.se.fi, v = to.se.se;
        g[v].push_back(u);
    }
    dfs2(1, 0);
    for (int i = 1; i <= n; i++) {
        used[i] = 0;
    }
    dfs2(n, 1);

    if (!ok1 && !ok2) {
        for (auto to : save) {
            int d = to.fi, u = to.se.fi, v = to.se.se;
            if (can_from1[v] && can_fromN[v] && can_to1[u] && can_toN[u]) {
                cout << d;
                return;
            }
        }
        cout << -1;
        return;
    }

    for (int i = 1; i <= n; i++) {
        g[i].clear();
    }
    for (auto to : save) {
        g[to.se.fi].push_back(to.se.se);
    }
    int res = inf;
    for (auto to : save2) {
        int u = to.fi, v = to.se;
        int d = mn[u][v];
        for (int j = 1; j <= n; j++) {
            used[j] = 0;
        }
        g[v].push_back(u);
        cnt[u][v]--;
        cnt[v][u]++;
        int Ok1 = check(1, n);
        for (int j = 1; j <= n; j++) {
            used[j] = 0;
        }
        int Ok2 = check(n, 1);
        g[v].pop_back();
        cnt[u][v]++;
        cnt[v][u]--;
        if (Ok1 && Ok2) {
            res = min(res, d);
        }
    }
    if (res == inf) cout << -1;
    else cout << res;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int tt = 1;
    // cin >> tt;

    while (tt--) {
        solve();
        // cout << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...