# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
231147 | 2020-05-12T21:59:49 Z | nickmet2004 | Dreaming (IOI13_dreaming) | C++11 | 0 ms | 0 KB |
#include<bits/stdc++.h> #include"dreaming.h" #define f first #define s second #define ll long long using namespace std; const int N = 2e5 + 500; typedef pair<int , int> ipair; int n , m , L; vector<ipair> adj[N]; ll component[N]; ll dpD[N] , dpU[N]; bool vis[N]; int cmp; ll MN[N]; int mx1[N] , mx2[N]; void dfsD(int u , int BOSS){ vis[u] = 1; component[u] = cmp; for(ipair x : adj[u]){ int v = x.f , w = x.s; if(vis[v])continue; dfsD(v , BOSS); dpD[u] = max(dpD[u] , dpD[v] + w); if(u == BOSS){ if(mx2[component[v]] < dpD[v] + w) mx2[component[v]] = dpD[v] + w; if(mx2[component[v]] > mx1[component[v]]) swap(mx2[component[v]] , mx1[component[v]]); } } } void dfsU(int u , int BOSS){ vis[u] = 1; for(ipair x : adj[u]){ int v = x.f , w = x.s; if(vis[v]) continue; if(u == BOSS){ if(dpD[v] + w == mx1[component[v]]){ if(mx2[component[v]] == -1){ dpU[v] = w; } else dpU[v] = mx2[component[v]] + w; } else { dpU[v] = mx1[component[v]] + w; } } else dpU[v] = dpU[u] + w; dfsU(v , BOSS); } } ll travelTime(int N , int M , int L , int A[] , int B[] , int C[]){ n = N; m = M; L = L; for(int i = 0; i < m; ++i){ adj[A[i]].emplace_back(B[i] , C[i]); adj[B[i]].emplace_back(A[i] , C[i]); } for(int i = 0; i < n; ++i){ mx1[cmp] = -1 , mx2[cmp] = -1; if(!vis[i]) {dfsD(i , i); cmp++;}; } for(int i = 0; i < n; ++i) vis[i] = 0; for(int i = 0; i < n; ++i){ if(!vis[i]) dfsU(i , i); } /* for(int i = 0; i < n; ++i){ cerr << i << " i : " << dpD[i] << " dpD " << " : " << " dpU " << dpU[i] << endl; } */ for(int i = 0; i < cmp; ++i) MN[i] = 1e18; for(int i = 0; i < n; ++i){ MN[component[i]] = min(MN[component[i]] , max(dpD[i] , dpU[i])); } //for(int i = 0; i < cmp; ++i) cout << i << " cmp " << MN[i] << endl; ll MX1 = -1 , MX2 = -1; for(int i = 0; i < cmp; ++i){ if(MX2 < MN[i]){ MX2 = MN[i]; if(MX2 > MX1) swap(MX2 , MX1); } } return MX1 + MX2 + L; } /* int main (){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> L; for(int i = 0; i < m; ++i){ int u , v , w; cin >> u >> v >> w; adj[u].emplace_back(v , w); adj[v].emplace_back(u , w); } for(int i = 0; i < n; ++i){ mx1[cmp] = -1 , mx2[cmp] = -1; if(!vis[i]) {dfsD(i , i); cmp++;}; } for(int i = 0; i < n; ++i) vis[i] = 0; for(int i = 0; i < n; ++i){ if(!vis[i]) dfsU(i , i); } /* for(int i = 0; i < n; ++i){ cerr << i << " i : " << dpD[i] << " dpD " << " : " << " dpU " << dpU[i] << endl; } for(int i = 0; i < cmp; ++i) MN[i] = 1e18; for(int i = 0; i < n; ++i){ MN[component[i]] = min(MN[component[i]] , max(dpD[i] , dpU[i])); } //for(int i = 0; i < cmp; ++i) cout << i << " cmp " << MN[i] << endl; ll MX1 = -1 , MX2 = -1; for(int i = 0; i < cmp; ++i){ if(MX2 < MN[i]){ MX2 = MN[i]; if(MX2 > MX1) swap(MX2 , MX1); } } cout << MX1 + MX2 + L << endl; return 0; } */