제출 #59247

#제출 시각아이디문제언어결과실행 시간메모리
59247aome꿈 (IOI13_dreaming)C++17
100 / 100
112 ms16116 KiB
#include "dreaming.h"

#include <bits/stdc++.h>

using namespace std;

const int N = 100005;
const int INF = 1e9;

int f[N], g[N];
bool visit[N];
vector<int> vec;
vector< pair<int, int> > G[N];

void dfs1(int u, int p) {
	visit[u] = 1, vec.push_back(u);
	for (auto i : G[u]) {
		int v = i.first, w = i.second;
		if (v == p) continue;
		dfs1(v, u);
		f[u] = max(f[u], f[v] + w);
	}
}

void dfs2(int u, int p) {
	int v0 = -INF, v1 = -INF;
	for (auto i : G[u]) {
		int v = i.first, w = i.second;
		if (v == p) continue;
		int tmp = f[v] + w;
		if (v0 < tmp) v1 = v0, v0 = tmp;
		else if (v1 < tmp) v1 = tmp;
	}
	for (auto i : G[u]) {
		int v = i.first, w = i.second;
		if (v == p) continue;
		g[v] = g[u] + w;
		if (v0 == f[v] + w) g[v] = max(g[v], v1 + w);
		else g[v] = max(g[v], v0 + w);
		dfs2(v, u);
	}
}

int travelTime(int n, int m, int L, int A[], int B[], int T[]) {
    for (int i = 0; i < m; ++i) {
    	G[A[i]].push_back({B[i], T[i]});
    	G[B[i]].push_back({A[i], T[i]});
    }
    vector<int> vec2;
    int res = -INF;
    for (int i = 0; i < n; ++i) {
    	if (visit[i]) continue;
    	vec.clear(), dfs1(i, i), dfs2(i, i);
    	int mn = INF;
    	for (auto j : vec) {
    		mn = min(mn, max(f[j], g[j]));
    		res = max(res, max(f[j], g[j]));
    	}
    	vec2.push_back(mn);
    }
    sort(vec2.begin(), vec2.end(), greater<int>());
    if (vec2.size() == 1) {
    	return max(res, vec2[0]);
    }
    if (vec2.size() == 2) {
    	return max(res, vec2[0] + vec2[1] + L);
    }
    return max(res, max(vec2[0] + vec2[1] + L, vec2[1] + vec2[2] + 2 * L));
}
#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...