Submission #337712

#TimeUsernameProblemLanguageResultExecution timeMemory
337712BlancaHMDreaming (IOI13_dreaming)C++14
59 / 100
1047 ms17508 KiB
#include <iostream> #include <vector> #include <queue> #include <map> #include <algorithm> #include "dreaming.h" using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef vector<pii> vpii; typedef vector<vpii> vvpii; pii calcDist(int i, vvpii & G, vector<bool> & visited) { queue<int> Q; Q.push(i); int u, v, t, maxNode = i; vector<int> vertices = {}; map<int, int> dist1, dist2; dist1[i] = 0; while(!Q.empty()) { u = Q.front(); Q.pop(); visited[u] = true; vertices.push_back(u); if (dist1[u] > dist1[maxNode]) maxNode = u; for (auto connection: G[u]) { v = connection.first; t = connection.second; if (dist1.find(v) == dist1.end()) { dist1[v] = dist1[u]+t; Q.push(v); } } } dist2[maxNode] = 0; Q.push(maxNode); while(!Q.empty()) { u = Q.front(); Q.pop(); if (dist2[u] > dist2[maxNode]) maxNode = u; for (auto connection: G[u]) { v = connection.first; t = connection.second; if (dist2.find(v) == dist2.end()) { dist2[v] = dist2[u]+t; Q.push(v); } } } int diam = dist2[maxNode]; dist1.clear(); dist1[maxNode] = 0; Q.push(maxNode); while(!Q.empty()) { u = Q.front(); Q.pop(); for (auto connection: G[u]) { v = connection.first; t = connection.second; if (dist1.find(v) == dist1.end()) { dist1[v] = dist1[u]+t; Q.push(v); } } } int ecc = 1e9; for (auto vert: vertices) { //cout << vert << " " << dist1[vert] << " " << dist2[vert] << "\n"; ecc = min(ecc, max(dist1[vert], dist2[vert])); } return make_pair(ecc, diam); } int travelTime(int N, int M, int L, int A[], int B[], int T[]) { vvpii G(N, vpii()); vector<bool> visited(N, false); for (int i = 0; i < M; i++) { G[A[i]].push_back(make_pair(B[i], T[i])); G[B[i]].push_back(make_pair(A[i], T[i])); } int CC = 0; vi diam = {}, ecc = {}; pii curCC; for (int i = 0; i < N; i++) { if (!visited[i]) { curCC = calcDist(i, G, visited); ecc.push_back(curCC.first); diam.push_back(curCC.second); CC++; } } int ans = diam[0]; for (int i = 1; i < CC; i++) ans = max(ans, diam[i]); sort(ecc.begin(), ecc.end(), greater<int>()); for (int i = 1; i < CC; i++) ans = max(ans, ecc[i]+ecc[0]+L); for (int i = 1; i < CC; i++) { for (int j = i+1; j < CC; j++) ans = max(ans, ecc[i]+ecc[j]+2*L); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...