hdu 5886 Tower Defence(树形dp)

题目链接

Tower Defence

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 135    Accepted Submission(s): 40


Problem Description
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  N1  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×(N1) .
 

Input
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(N100000) .
The  i -th line of the next  N1  lines describes the  i -th edge. Three integers  u,v,w (0w1000)  describe an edge between  u  and  v  of length  w .
 

Output
For each test cases, output  expectationoftacticaldepth×(N1) .
 

Sample Input
  
  
2 3 2 1 2 3 2 5 5 2 1 7 3 1 7 4 2 5 5 2 6
 

Sample Output
  
  
7 63
 

Source


题意:给出 n 个点的无向带权树,问删掉每条边后的直径之和。



题解:找出树的一条直径 (a,b),然后分别以 a,b 为根,dp 预处理子树 v 的直径。

处理的时候先分别以a,b为根dfs,得到每个结点的子树中与根距离最长和第二长长度,然后再分别dp求每个结点子树的最长直径。

如果删掉的边不在直径上,那么有一颗子树的直径就是原树的直径,另一边已经预处理好了。如果在直径上,很显然两遍都预处理了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN=100000+1000;
typedef long long ll;
struct Edge
{
    int from,to,next,flag;
    ll w;
}edge[MAXN*2];
struct fun
{
    ll dis;
	int v;
    bool operator <(const fun& a)const{
    return dis>a.dis;
    }
}a[MAXN*2][3];
int tol;
int head[MAXN];
ll d[MAXN],d1[MAXN];
int pre[MAXN],preve[MAXN];
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,ll w)
{
    edge[tol].from=u;
    edge[tol].to=v;
    edge[tol].w=w;
    edge[tol].next=head[u];
    edge[tol].flag=1;
    head[u]=tol++;
    edge[tol].from=v;
    edge[tol].to=u;
    edge[tol].w=w;
    edge[tol].next=head[v];
    edge[tol].flag=1;
    head[v]=tol++;
}
ll _max;
int flag;
void dfs(int u,int fa,ll d)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==fa) continue;
        pre[v]=u;
        preve[v]=i;
        if(d+edge[i].w>_max)
        {
            _max=d+edge[i].w;
            flag=v;
        }
        dfs(v,u,d+edge[i].w);
    }
}
void dfs1(int u,int fa)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==fa) continue;
        dfs1(v,u);
        a[u][2].dis=a[v][0].dis+edge[i].w;
        sort(a[u],a[u]+3);
    }
}
void dp(int u,int fa)
{
    d[u]=a[u][0].dis+a[u][1].dis;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==fa) continue;
        dp(v,u);
        d[u]=max(d[v],d[u]);
    }
}
void dp1(int u,int fa)
{
    d1[u]=a[u][0].dis+a[u][1].dis;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==fa) continue;
        dp1(v,u);
        d1[u]=max(d1[v],d1[u]);
    }
}
int main()
{
//	freopen("ini.txt","r",stdin);
//	freopen("out1.txt","w",stdout);
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        init();
        int n;
        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            int u,v;
			ll w;
            scanf("%d%d%lld",&u,&v,&w);
            addedge(u,v,w);
        }
        _max=0;
        dfs(1,-1,0);
        _max=0;
        int u=flag;
        memset(pre,0,sizeof(pre));
        memset(preve,0,sizeof(preve));
        dfs(u,-1,0);
        int v=flag;
        int cnt=0,cur=v;
        while(cur!=u)
        {
            edge[preve[cur]].flag=0;
            cnt++;
            cur=pre[cur];
        }
        ll ans=_max*(ll)(n-1-cnt);
        memset(a,0,sizeof(a));
        memset(d,0,sizeof(d));
        memset(d1,0,sizeof(d1));
        dfs1(u,-1);
        dp(u,-1);
        memset(a,0,sizeof(a));
        dfs1(v,-1);
        dp1(v,-1);
        for(int i=0;i<tol;i++)
        {
            if(!edge[i].flag)
            {
                int u=edge[i].from,v=edge[i].to;
                ll res=max(d[v],d1[u]);
                ans+=res;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


这一题因为没有用long long存结果wa了,然后写了个对拍,以下是随意生成树的代码,供大家debug:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<ctime>
#include<cstdlib>
using namespace std;
const int MAXN=10000;
int vis[MAXN][MAXN];
int head[MAXN],pa[MAXN];
int tol=0;
struct Edge
{
	int from,to,w;
}edge[MAXN];
void addedge(int u,int v,int w)
{
	edge[tol].from=u,edge[tol].to=v,edge[tol++].w=w;
}
int find(int x)
{
	return pa[x]==x?x:pa[x]=find(pa[x]);
}
int main()
{
	//freopen("ini.txt","w",stdout);
	int cas=50;
	printf("%d\n",cas);
	srand((int)(time(NULL)));
	int tot=0;
	while(tot<cas)
	{
		memset(vis,0,sizeof(vis));
		tol=0;
		int n=rand()%1000+1;
		for(int i=1;i<=n;i++) pa[i]=i;
		for(int i=1;i<n;i++)
		{
			int u=rand()%n+1;
			int v=rand()%n+1;
			while(u==v||vis[u][v]||vis[v][u])
			v=rand()%n+1;
			vis[u][v]=1,vis[v][u]=1;
			int w=rand()%1000+1;
			addedge(u,v,w);
			int fu=find(u),fv=find(v);
			pa[fu]=fv;
		}
		int cnt=0;
		for(int i=1;i<=n;i++)
		if(pa[i]==i) cnt++;
		if(cnt==1)
		{
			printf("%d\n",n);
			for(int i=0;i<tol;i++)
			printf("%d %d %d\n",edge[i].from,edge[i].to,edge[i].w);
			tot++;
		}
		
	}
	printf("%d\n",tot);
	
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值