int n,m;
ll ans;
ll dp[MX];//i
int head[MX],tot;
struct IN{int v,w,nxt;}edge[MX<<1];
void add(int u,int v,int w)
{
edge[tot].v=v;edge[tot].w=w,edge[tot].nxt=head[u];
head[u]=tot++;
}
void dfs(int x,int fx)
{
for(int i=head[x];i!=-1;i=edge[i].nxt)
{
int v=edge[i].v;
if(v==fx) continue;
dfs(v,x);
ans=max(ans,dp[x]+edge[i].w+dp[v]);
dp[x]=max(dp[x],dp[v]+edge[i].w);
}
}
signed main()
{
while(cin>>n>>m)
{
memset(head,-1,sizeof(head));
memset(dp,0,sizeof(dp));
tot=0;
ans=0;
rpp(i,m)
{
string c;
int u,v,w;cin>>u>>v>>w>>c;
add(u,v,w);add(v,u,w);
}
dfs(1,0);
cout<<ans<<endl;
}
}
POJ - 1985 Cow Marathon (树的直径-----DP版)
最新推荐文章于 2021-07-28 21:34:28 发布