0分求调(玄关)
查看原帖
0分求调(玄关)
712647
rhx103513yyds楼主2024/7/25 16:16
#include<bits/stdc++.h>
//#pragma GCC optimize(3)
using namespace std;
#define int long long
int n,m;
struct node{
	int y,w;
};
vector<node>a[200005];
int d1[2];
int d2[2];
bool v[200005];
int dm1[200005];
int dm2[200005];
void dfs1(int x,int s){
	if(v[x])return ;
//	cout<<x<<" "<<s<<" "<<v[x]<<"---\n";
	if(d1[0]<s){
		d1[0]=s;
		d1[1]=x;
	}
	v[x]=1;
	for(auto i:a[x])
		dfs1(i.y,s+i.w);
	v[x]=0;
}
void dfs2(int x,int s){
	if(v[x])return ;
	if(d2[0]<s){
		d2[0]=s;
		d2[1]=x;
	}
	v[x]=1;
	for(auto i:a[x])
		dfs2(i.y,s+i.w);
	v[x]=0;
}
void dfs3(int x){
	if(v[x])return ;
	v[x]=1;
	for(auto i:a[x]){
		dm1[i.y]=dm1[x]+i.w;
		dfs3(i.y);
	}
	v[x]=0;
}
void dfs4(int x){
	if(v[x])return ;
	v[x]=1;
	for(auto i:a[x]){
		dm2[i.y]=dm2[x]+i.w;
		dfs4(i.y);
	}
	v[x]=0;
}
int ans;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y,w;
		cin>>x>>y>>w;
		a[x].push_back({y,w});
		a[y].push_back({x,w});
	}
	dfs1(1,0);
//	cout<<d1[1]<<" "<<d2[1]<<"--\n";
	memset(v,0,sizeof(v));
	dfs2(d1[1],0);
	memset(v,0,sizeof(v));
	dfs3(d1[1]);
	memset(v,0,sizeof(v));
	dfs4(d2[1]);
	for(int i=1;i<=n;i++){
//		cout<<dm1[i]<<" "<<dm2[i]<<"\n";
		ans=max(ans,min(dm1[i],dm2[i]));
	}
	cout<<ans;
	return 0;
}

/*
4 3
1 2 1
1 3 2
3 2 100
*/
2024/7/25 16:16
加载中...