您的位置:首页 > Web前端

Hdu-5886 Tower Defence(树形DP)

2016-09-20 22:12 351 查看
[align=left]Problem Description[/align]
There was a civil war between two factions in Skyrim, a province of the Empire on the continent of Tamriel. The Stormcloaks, led by Ulfric Stormcloak, are made up of Skyrim's native Nord race. Their goal is an independent Skyrim free
from Imperial interference. The Imperial Legion, led by General Tullius, is the military of the Empire that opposes the Stormcloaks and seeks to reunite and pacify the province.

The current target of General Tullius is to defend Whiterun City. Near by this city there are
N
towers under the Empire's control. There are N−1
roads link these tower, so solders can move from any tower to another one through these roads.

In military affairs, tactical depth means the longest path between two towers of all. Larger the tactical depth is, more stable these towers are.

According to the message sent by spies, General Tullius believe that Stormcloaks is planning to attack one of these roads, and his towers would be divided into two parts. However, Tullius does not know which one, so he supposes the possibility that Stormcloaks
attack these roads are the same. Now, General Tullius ask for your help, to calculate the expectation of tactical depth after this attack.

To avoid the issue of precision, you need to calculate
expectationoftacticaldepth×(N−1).
 

[align=left]Input[/align]
The first line of input contains an integer
t,
the number of test cases. t
test cases follow.

For each test case, in the first line there is an integer
N(N≤100000).

The i-th
line of the next N−1
lines describes the i-th
edge. Three integers u,v,w (0≤w≤1000)
describe an edge between u
and v
of length w.
 

[align=left]Output[/align]
For each test cases, output
expectationoftacticaldepth×(N−1).
 

[align=left]Sample Input[/align]

2
3
2 1 2
3 2 5
5
2 1 7
3 1 7
4 2 5
5 2 6

 

[align=left]Sample Output[/align]

7
63

题意:一棵边权数,问删除每条边后剩下的两个子树的最长路径的和。

分析:树形DP,考虑一条边(u,v),删除后以v为根的树的最长路径可以一遍dfs得到,然后考虑u所在的树上的最长路径,这时分类讨论,经过u的路径和不经过u的路径,这两种情况下向下的部分都可以在第一次dfs中得到,
第二次dfs我们处理从u父亲过来的情况,同样是一次树形dp。保留最大值次大值和第三大值辅助转移。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 1000000007
#define MAXN 100005
using namespace std;
typedef long long ll;
int T,n;
ll ans,Mvline[MAXN][3],Mvu[MAXN][2],Mline[MAXN],Maxu[MAXN],upedge[MAXN],upline[MAXN],Maxuv[MAXN];
struct edge
{
int v,val;
edge() {}
edge(int x,int y)
{
v = x;
val = y;
}
};
vector<edge> G[MAXN];
void update_line(int u,ll val)
{
Mvline[u][2] = max(Mvline[u][2],val);
if(Mvline[u][2] > Mvline[u][1]) swap(Mvline[u][2],Mvline[u][1]);
if(Mvline[u][1] > Mvline[u][0]) swap(Mvline[u][1],Mvline[u][0]);
}
void update_ans(int u,ll val)
{
Mvu[u][1] = max(Mvu[u][1],val);
if(Mvu[u][1] > Mvu[u][0]) swap(Mvu[u][1],Mvu[u][0]);
}
void dfs1(int u,int fa)
{
for(edge v : G[u])
if(v.v != fa)
{
upedge[v.v] = v.val;
dfs1(v.v,u);
update_line(u,1ll*v.val + Mline[v.v]); // 儿子最长链更新
update_ans(u,Maxu[v.v]);               // 儿子最长路径
}
Mline[u] = Mvline[u][0];
Maxu[u] = max(Mvu[u][0],Mvline[u][0] + Mvline[u][1]);
}
void dfs2(int u,int fa)
{
if(fa > 0)
{
upline[u] = upedge[u] + ((upedge[u] + Mline[u]) == Mvline[fa][0] ? Mvline[fa][1] : Mvline[fa][0]);
update_line(u,upline[u]);
// 更新最长向上链
// 更新以u为根节点的所有链
}
for(edge v : G[u])
if(v.v != fa)
{
ll tmp1 = 0,tot = Mline[v.v] + upedge[v.v];
if(tot == Mvline[u][0])
{
tmp1 = Mvline[u][1] + Mvline[u][2];
}
else
if(tot == Mvline[u][1])
{
tmp1 = Mvline[u][0] + Mvline[u][2];
}
else tmp1 = Mvline[u][0] + Mvline[u][1];
ll tmp2 = (Mvu[u][0] == Maxu[v.v]) ? Mvu[u][1] : Mvu[u][0];
if(fa > 0) tmp2 = max(tmp2,Maxuv[u]);
Maxuv[v.v] = max(tmp1,tmp2);
ans += max(Maxuv[v.v],Maxu[v.v]);
}
for(edge v : G[u])
if(v.v != fa) dfs2(v.v,u);
}
void Init()
{
for(int i = 1;i <= n;i++) G[i].clear();
memset(Mvline,0,sizeof(Mvline));
memset(Mvu,0,sizeof(Mvu));
memset(Mline,0,sizeof(Mline));
memset(Maxu,0,sizeof(Maxu));
memset(upline,0,sizeof(upline));
memset(Maxuv,0,sizeof(Maxuv));
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
Init();
for(int i = 1;i < n;i++)
{
int u,v,val;
scanf("%d%d%d",&u,&v,&val);
G[u].push_back(edge(v,val));
G[v].push_back(edge(u,val));
}
ans = 0;
dfs1(1,-1);
dfs2(1,-1);
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: