# BZOJ4890 & 洛谷3761：[TJOI2017]城市——题解

https://www.lydsy.com/JudgeOnline/problem.php?id=4890

https://www.luogu.org/problemnew/show/P3761

#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=5005;
const int INF=1e9;
int X=0,w=0;char ch=0;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
struct node{
int w,to,nxt;
}e[N*2];
bool vis[N];
inline void add(int u,int v,int w){
}
int ans;
void dfs1(int u,int fa){
int v=e[i].to,w=e[i].w;
if(vis[v]||fa==v)continue;
dfs1(v,u);
if(f[u][0]<f[v][0]+w){
f[u][1]=f[u][0];
f[u][0]=f[v][0]+w;
}else if(f[u][1]<f[v][0]+w)
f[u][1]=f[v][0]+w;
}
ans=max(ans,f[u][0]+f[u][1]);
}
void dfs2(int u,int fa){
int v=e[i].to,w=e[i].w;
if(vis[v]||fa==v)continue;
f[v][2]=f[u][2]+w;
if(f[u][0]-w!=f[v][0])f[v][2]=max(f[v][2],f[u][0]+w);
else f[v][2]=max(f[v][2],f[u][1]+w);
dfs2(v,u);
}
ans=min(ans,max(f[u][0],f[u][2]));
}
int main(){
for(int i=1;i<n;i++){
}
int sum=INF;
for(int i=1;i<=cnt;i+=2){
int u=e[i].to,v=e[i+1].to,w=e[i].w;
int w1,w2,tmp=0;
vis[v]=1;
memset(f,0,sizeof(f));
ans=0;dfs1(u,0);tmp=max(tmp,ans);
ans=INF;dfs2(u,0);w1=ans;
vis[v]=0;vis[u]=1;
ans=0;dfs1(v,0);tmp=max(tmp,ans);
ans=INF;dfs2(v,0);w2=ans;
vis[u]=0;
tmp=max(tmp,w1+w2+w);
sum=min(sum,tmp);
}
printf("%d\n",sum);
return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者：luyouqi233。 　　　　　　　　　　　　　　+

+欢迎访问我的博客：http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

posted @ 2018-06-18 11:59  luyouqi233  阅读(236)  评论(0编辑  收藏  举报