Submission #1282664

#TimeUsernameProblemLanguageResultExecution timeMemory
1282664vibeduckRelay Marathon (NOI20_relaymarathon)C++20
0 / 100
47 ms10072 KiB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()

typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;

const int mod = 1e9 + 7;
const int inf = INTMAX_MAX;
const bool tc = false;

int n, m, k;
const int mxn = 1e5 + 1;
vector<pii> adj[mxn]; int a[mxn];
vi sp;

struct dijk {
    int n, s, d;
    bool operator<(const dijk &other) const {
        if (d != other.d) return d < other.d;
        if (n != other.n) return n < other.n;
        return s < other.s; 
    }
};

pair<int, pii> comp(vi &active, vi &vis) {
    // find all special nodes
    sp.clear(); vi dist(n, 1e18); vi src(n);
    set<dijk> dk; vis.assign(n, 0);
    for (int i = 0; i < n; i++) {
        if (!a[i]) continue;
        if (!active[i]) continue;
        dijk tmp;
        tmp.n = i;
        tmp.s = i;
        tmp.d = 0;
        dk.insert(tmp);
        sp.pb(i);
        //cout << "added " << i + 1 << " to start\n";
    }

    while (dk.size()) {
        auto cur = *dk.begin();
        dk.erase(dk.begin());

        // finalize ONLY if cur.s != cur.n
        if (cur.s != cur.n) {
            if (vis[cur.n]) continue;         // already finalized from some other source
            vis[cur.n] = 1;
            src[cur.n] = cur.s;
            dist[cur.n] = cur.d;              // finalize distance from the other source
        }
        // else (cur.s == cur.n): do NOT mark visited;
        // let other sources reach this node later.

        for (auto &neighbour : adj[cur.n]) {
            if (cur.s == neighbour.fi) continue; // avoid pairing with itself

            if (cur.d + neighbour.se < dist[neighbour.fi]) {
                dk.erase({neighbour.fi, src[neighbour.fi], dist[neighbour.fi]});
                dist[neighbour.fi] = cur.d + neighbour.se;
                src[neighbour.fi] = cur.s;
                dk.insert({neighbour.fi, cur.s, dist[neighbour.fi]});
            }
        }
    }


    // for (auto &x : dist) cout << x << ' ';
    // cout << '\n';

    // for (auto &x : src) cout << x + 1 << ' ';
    // cout << '\n';

    pair<int, pii> ans = {(int)1e18, {(int)-1, (int)-1}};
    for (auto &i : sp) ans = min(ans, {dist[i], {i, src[i]}});
    return ans;
}

pair<int, pii> find(vi &active) {
    vi v(n);
    return comp(active, v);
}

inline void solve() {
    cin >> n >> m >> k;
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }
    for (int i = 0; i < k; i++) {
        int x; cin >> x; x--;
        a[x] = 1;
    }

    vi active(n, 1);
    pair<int, pii> f = find(active);
    active[f.se.fi] = 0;
    active[f.se.se] = 0;
    pair<int, pii> s = find(active);

    // cout << f.fi << " " << f.se.fi + 1 << " " << f.se.se + 1 << '\n';
    // cout << s.fi << " " << s.se.fi + 1 << " " << s.se.se + 1 << '\n';

    assert(f.fi + s.fi < 1e18);
    cout << f.fi + s.fi << '\n';
}

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

signed main() {
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    //setIO();

    int t = 1;
    if (tc) {
        cin >> t;
    }

    while (t--) {
        solve();
    }
}

Compilation message (stderr)

RelayMarathon.cpp: In function 'void setIO(std::string)':
RelayMarathon.cpp:134:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:135:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...