2018银川邀请赛 G - Factories Gym - 102222G(树形DP)

44 篇文章 0 订阅

Byteland has n cities numbered from 1 to n, and n−1 bi-directional roads connecting them. For each pair of cities, the residents can arrive one from another one through these roads (which also means the road network in Byteland is a tree).

Ghaliyah, the queen of the land, has decided to construct k new factories. To avoid contamination, she requires that a factory can locate at a city with only one road (which also means this city is a leaf in the road network). Besides, a city can only have one factory.

You, as the royal designer, are appointed to arrange the construction and meanwhile, minimize the sum of distances between every two factories.

Input
The input contains several test cases, and the first line is a positive integer T indicating the number of test cases which is up to 103.

For each test case, the first line contains two integers n (2≤n≤105) and k (1≤k≤100) indicating the number of cities in Byteland and the number of new factories which are asked to construct.

Each of the following n−1 lines contains three integers u,v (1≤u,v≤n) and w (1≤w≤105) which describes a road between the city u and the city v of length w.

We guarantee that the number of leaves in the road network is no smaller than k, and the sum of n in all test cases is up to 106.

Output
For each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y is the minimum sum of distances between every two factories.

Example
Input
2
4 2
1 2 2
1 3 3
1 4 4
4 3
1 2 2
1 3 3
1 4 4
Output
Case #1: 5
Case #2: 18

题意:
n个城市构成的树。叶子节点上可以布置工厂。布置k个工厂。
求怎样布置工厂,使得工厂之间的距离和最小

思路:
直接算点之间的距离和算不出来,改为算边的贡献。
树形dp算到u点某子树对应的边权w时。假设这棵子树里布置了j个点,那么这个边的贡献就是w * (k - j) * j。
定义状态为f[u][k],代表u为根节点下布置了k个炸弹的最小边贡献。
转移方程:f[u][i] = min(f[u][i],f[u][i - j] + f[v][j] + w * (k - j) * j);

因为工厂只能布置在叶子节点,那么初始化的时候叶子节点上f[u][1] = 0。
因为递归关系是相同的,根节点是谁并没有关系。看到很多博客代码不用叶子节点当根,表示很不理解。。。

ACNEW

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;

typedef long long ll;

const int maxn = 1e5 + 7;
const ll INF = 1e18;
int n,k;
int head[maxn],nex[maxn << 1],to[maxn << 1],deg[maxn],siz[maxn],tot;
ll val[maxn << 1],dp[maxn][105];

void init1() {
    tot = 0;
    for(int i = 1;i <= n;i++) {
        siz[i] = deg[i] = head[i] = 0;
    }
}

void init2() {
    for(int i = 1;i <= n;i++) {
        siz[i] = 0;
        for(int j = 1;j <= k;j++) {
            dp[i][j] = INF;
        }
        if(deg[i] == 1) {
            dp[i][1] = 0;
            siz[i] = 1;
        }
    }
}

void add(int x,int y,int z) {
    to[++tot] = y;
    nex[tot] = head[x];
    val[tot] = z;
    head[x] = tot;
}

void DP(int u,int fa) {
    for(int i = head[u];i;i = nex[i]) {
        int v = to[i];
        ll w = val[i];
        if(v == fa) continue;
        
        DP(v,u);
        siz[u] += siz[v];
        
        for(int j = min(siz[u],k);j >= 1;j--) {
            for(int q = 1;q <= min(siz[v],j);q++) {
                dp[u][j] = min(dp[u][j],dp[u][j - q] + dp[v][q] + q * (k - q) * w);
            }
        }
    }
}

int main() {
    int T;scanf("%d",&T);
    int kase = 0;
    while(T--) {
        scanf("%d%d",&n,&k);
        init1();
        for(int i = 1;i < n;i++) {
            int x,y,z;scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);add(y,x,z);
            deg[x]++;deg[y]++;
        }
        init2();
        DP(1,-1);
        printf("Case #%d: %lld\n",++kase,dp[1][k]);
    }
    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;
const ll INF = 1e18;
const int maxn = 1e5 + 7;
int head[maxn << 1],nex[maxn << 1],to[maxn << 1],val[maxn << 1],tot;
int deg[maxn],siz[maxn];
ll f[maxn][105];
int n,k;

void add(int x,int y,int z)
{
    to[++tot] = y;
    nex[tot] = head[x];
    val[tot] = z;
    head[x] = tot;
}

void init1()
{
    tot=0;
    for(int i = 1;i <= n;i++)
    {
        deg[i] = siz[i] = head[i] = 0;
    }
    for(int i = 1;i <= n;i++)
    {
        head[i + n] = 0;
    }
}
void init2()
{
    for(int i = 1;i <= n;i++)
    {
        f[i][0] = 0;
        for(int j = 1;j <= k;j++)f[i][j] = INF;
        if(deg[i] == 1)
        {
            f[i][1] = 0;
            siz[i] = 1;
        }
    }
}

void DP(int u,int fa)
{
    for(int i = head[u];i;i = nex[i])
    {
        int v = to[i],w = val[i];
        if(v == fa)continue;
        DP(v,u);
        siz[u] += siz[v];

        for(int i = min(k,siz[u]);i >= 1;i--)
        {
            for(int j = 1;j <= min(i,siz[v]);j++)
            {
                f[u][i] = min(f[u][i],f[u][i - j] + f[v][j] + w * (k - j) * j);
            }
        }
    }
}


int main()
{
    int T;scanf("%d",&T);
    int kase = 0;
    while(T--)
    {
        scanf("%d%d",&n,&k);
        init1();
        for(int i = 1;i < n;i++)
        {
            int x,y,z;scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);add(y,x,z);
            deg[x]++;deg[y]++;
        }

        int rt = 1;
        for(int i = 1;i <= n;i++)
        {
            if(deg[i] > 1)
            {
                rt = i;
                break;
            }
        }

        init2();
        DP(rt,0);
        printf("Case #%d: %lld\n",++kase,f[rt][k]);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值