历届试题 大臣的旅费
作者:互联网
树的直径,利用树形dp
待补dfs*2的方法
#include<bits/stdc++.h>
using namespace std;
#define fore(i,a,b) for(int i=a;i<=b;i++)
#define forb(i,a,b) for(int i=a;i>=b;i--)
#define fir first
#define sec second
#define ABS(a) ((a)>0?(a):-(a))
#define PI acos(-1,0)
typedef long long ll;
typedef long double ld;
const int MAXN=100005;
int ver[MAXN*10],Next[MAXN],edge[MAXN],head[MAXN];
bool vis[MAXN];
int tot;
int ans;
int d[MAXN],f[MAXN];
void add(int x,int y,int z){
ver[++tot]=y,edge[tot]=z;
Next[tot]=head[x],head[x]=tot;
ver[++tot] = x, edge[tot] = z;
Next[tot] = head[y], head[y] = tot;
}
void dp(int x){
vis[x]=true;
for(int i=head[x];i;i=Next[i]){
int y=ver[i];
if(vis[y]) continue;
dp(y);
ans=max(ans,d[x]+d[y]+edge[i]);
d[x] = max(d[x], d[y] + edge[i]);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
dp(1);
cout<<(ans*10+(ans+1)*ans/2);
return 0;
}
标签:head,试题,int,tot,edge,MAXN,旅费,历届,define 来源: https://www.cnblogs.com/rign/p/10472660.html