为什么 80pts?
查看原帖
为什么 80pts?
481621
Zhang_Wenjie楼主2024/7/12 22:22
#include <bits/stdc++.h>
#define re register int 

using namespace std;
const int N = 5e3 + 10, inf = 0x3f3f3f3f;

struct Node
{
	int x, y, w;
}a[N];

struct Edge
{
	int to, w, next;
}e[N << 1];
int top, h[N];
int len, p, q, maxd, ans = inf;
int n;
bool st[N];

inline void add(int x, int y, int w)
{
	e[++ top] = (Edge){y, w, h[x]};
	h[x] = top;
}

void dfs(int u, int fa, int size, int & ver, int type)
{
	if (size > maxd)
	{
		maxd = size;
		ver = u;
		
		if (type) len = size;
	}
	for (re i = h[u]; i; i = e[i].next)
	{
		int v = e[i].to, w = e[i].w;
		
		if (v == fa || st[v]) continue;
		dfs(v, u, size + w, ver, type);
	}
}

inline void work()
{
	for (re i = 1; i < n; i ++)
	{		
		int x = a[i].x, y = a[i].y, w = a[i].w;
		int res1, res2;
		
		st[y] = true;
		
		dfs(x, 0, 0, p, 0); maxd = 0;
		dfs(p, 0, 0, q, 1);
		res1 = len; 
		
		st[y] = false;
		st[x] = true;
		maxd = 0, len = 0, p = 0, q = 0;
		
		dfs(y, 0, 0, p, 0); maxd = 0;
		dfs(p, 0, 0, q, 1);
		res2 = len;
		
		maxd = 0, len = 0, p = 0, q = 0;
		st[x] = false;
		
		int res3 = ceil((double)res1 / 2) + ceil((double)res2 / 2) + w;
		
		ans = min(ans, max(res3, max(res1, res2)));
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	for (re i = 1; i < n; i ++)
	{
		int x, y, w; cin >> x >> y >> w;
		add(x, y, w);
		add(y, x, w);
		a[i] = (Node){x, y, w};
	}
	work();
	cout << ans << '\n';
	
	return 0;
}
2024/7/12 22:22
加载中...