Submission #1282557

#TimeUsernameProblemLanguageResultExecution timeMemory
1282557vibeduckRelay Marathon (NOI20_relaymarathon)C++20
0 / 100
72 ms10124 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; } }; void dfs(int node, vi &vis) { vis[node] = 1; if (a[node]) sp.pb(node); for (auto &x : adj[node]) { if (vis[x.fi]) continue; dfs(x.fi, vis); } } 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()); if (vis[cur.n]) continue; //cout << cur.n + 1 << " " << cur.s + 1 << " " << cur.d << " is in queue.\n"; vis[cur.n] = 1; if (cur.n != cur.s) {src[cur.n] = cur.s;} //cout << "set src to " << cur.s + 1 << '\n';} if (cur.s != cur.n) {dist[cur.n] = min(dist[cur.n], cur.d);} //cout << "set dist to " << cur.d << '\n';} for (auto &neighbour : adj[cur.n]) { //cout << "checking neighbour " << neighbour.fi + 1 << '\n'; if (dist[neighbour.fi] == 1e18) { //cout << "unvis\n"; if (cur.s != neighbour.fi) { //cout << "looks good\n"; dk.insert({neighbour.fi, cur.s, cur.d + neighbour.se}); dist[neighbour.fi] = cur.d + neighbour.se; } } else { //cout << "check1\n"; if (cur.d + neighbour.se >= dist[neighbour.fi]) continue; //cout << "check2\n"; if (cur.s == neighbour.fi) continue; //cout << "check3\n"; 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]}); //cout << "added " << neighbour.fi + 1 << " " << cur.s + 1 << " " << dist[neighbour.fi] << '\n'; } } } // 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'; 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:148:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:149:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |     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...