【UVa】【DP】12093 Protecting Zonk

10 篇文章 0 订阅

UVa 12093 Protecting Zonk

题目

◇题目传送门◆(由于UVa较慢,这里提供一份vjudge的链接)
◇题目传送门(vjudge)◆

题目大意

N N 个节点构成一个树形结构的图。现有两种机器A,B。A的花费为C1,B的花费为 C2 C 2 。若在某个节点上放一个A机器,则与该节点相连的边被覆盖;若在某节点上放一个B机器,则与该节点相连的边及其相邻节点相连的边会被覆盖。求将所有边覆盖的最小花费。

思路

这是一棵无根树,显然我们要人为的给它一个根。所以我们以1为它的根。

于是这道题变成了很明显的树形DP。但是状态非常复杂。。。

定义状态:

  1. f[u][0] f [ u ] [ 0 ] u u 不安装任何机器,且保证其与儿子相连得到边全部被儿子覆盖的最小花费;
  2. f[u][1]:在 u u 安装一台A机器的最小花费;
  3. f[u][2]:在 u u 安装一台B机器的最小花费;
  4. f[u][3] u u u的儿子都不安装任何机器, u u 与其儿子的边全部被u的父亲覆盖;

注意:在 u u 的儿子节点处安装一个B机器等同于在u安装一个A机器

我们逐个来分析状态:

对于 f[u][0] f [ u ] [ 0 ] ,此时, u u 的儿子需要安装A机器或者B机器,则此状态转移到min(f[v][1],f[v][2]) v v u的一个儿子);

对于 f[u][1] f [ u ] [ 1 ] ,此时, v v 有三种选择:不安装、安装A机器、安装B机器,则此状态转移到min(f[v][0],f[v][1],f[v][2])

注意此时我们仍需要将在 u u 安装A机器和在v安装B机器的花费作比较!

对于 f[u][2] f [ u ] [ 2 ] ,此时, v v 有四种选择,则此状态转移到min(f[v][0],f[v][1],f[v][2],f[v][3])

对于 f[u][3] f [ u ] [ 3 ] ,此时, v v 有三种选择:不安装、安装A机器、安装B机器,则此状态转移到min(f[v][0],f[v][1],f[v][2])

所以可列出状态转移方程:
f[u][0]=min(f[v][1],f[v][2])f[u][1]=min(f[v][0],f[v][1],f[v][2])+min(C1,f[v][2]min(f[v][0],f[v][1],f[v][2]))f[u][2]=min(f[v][0],f[v][1],f[v][2],f[v][3])+C2f[u][3]=min(f[v][0],f[v][1],f[v][2]) f [ u ] [ 0 ] = ∑ min ( f [ v ] [ 1 ] , f [ v ] [ 2 ] ) f [ u ] [ 1 ] = ∑ min ( f [ v ] [ 0 ] , f [ v ] [ 1 ] , f [ v ] [ 2 ] ) + min ( C 1 , f [ v ] [ 2 ] − min ( f [ v ] [ 0 ] , f [ v ] [ 1 ] , f [ v ] [ 2 ] ) ) f [ u ] [ 2 ] = ∑ min ( f [ v ] [ 0 ] , f [ v ] [ 1 ] , f [ v ] [ 2 ] , f [ v ] [ 3 ] ) + C 2 f [ u ] [ 3 ] = ∑ min ( f [ v ] [ 0 ] , f [ v ] [ 1 ] , f [ v ] [ 2 ] )

其中, v v u的儿子。

边界条件为: f[u][0]=f[u][1]=0,f[u][1]=C1,f[u][2]=C2 f [ u ] [ 0 ] = f [ u ] [ 1 ] = 0 , f [ u ] [ 1 ] = C 1 , f [ u ] [ 2 ] = C 2 。其中, u u 是叶子节点。

答案即为min(f[1][0],f[1][1],f[1][2])

正解代码

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;

const int Maxn=10000;
const int INF=0x7f7f7f7f;

struct Edge {
    int to;
    Edge *nxt;
}e[Maxn*2+5];
Edge *ecnt,*G[Maxn+5];
void AddEdge(int u,int v) {
    Edge *p=++ecnt;
    p->to=v;
    p->nxt=G[u],G[u]=p;
}
int N,C1,C2;
int f[Maxn+5][4];

void DFS(int u,int fa) {
    f[u][0]=f[u][1]=f[u][3]=0;
    f[u][2]=C2;
    int t=INF;
    for(Edge *p=G[u];p!=NULL;p=p->nxt) {
        int v=p->to;
        if(v==fa)continue;
        DFS(v,u);
        f[u][0]+=min(f[v][1],f[v][2]);
        int tmp=min(f[v][0],min(f[v][1],f[v][2]));
        f[u][1]+=tmp;
        f[u][2]+=min(tmp,f[v][3]);
        f[u][3]+=tmp;
        t=min(t,f[v][2]-tmp);
    }
    f[u][1]+=min(C1,t);
}

int main() {
    #ifdef LOACL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d %d %d",&N,&C1,&C2)!=EOF&&N) {
        memset(f,0,sizeof f);
        memset(G,0,sizeof G);
        ecnt=&e[0];
        for(int i=1;i<N;i++) {
            int u,v;
            scanf("%d %d",&u,&v);
            AddEdge(u,v);
            AddEdge(v,u);
        }
        DFS(1,-1);
        printf("%d\n",min(min(f[1][0],f[1][1]),f[1][2]));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
阅读下面材料,在空格处填入适当的内容(1个单词)或使用括号中单词的正确形式。 If including inter-state sections, those not protecting the northern border of China, the oldest _____56____(exist) section of the Great Wall of China was the Qi State "Great Wall". It stretches for over 500 kilometers (300 miles) from the Yellow River at Jinan eastwards to the East China Sea, almost ___57____(divide) Shandong Province in half. The "Great Wall" of the Qi State was ___58____(initial) built around 650 BC, and expended during the Warring States Period (475–221 BC). Before the Qi State Wall was built, natural barriers, i.e. rivers and mountain ranges, formed ____59_____ only defensible boundaries between territories as barriers ___60____ enemies. The State of Qi built its Great-Wall-esque military barrier along its southern border to prevent attacks from the State of Lu and the State of Chu. However, rapid development and ____61____(construct) have brought many new problems and challenges in protecting the wall. ____62____ is necessary to provide a solid legal guarantee for its conservation. To tackle the challenges, Shandong Province has passed a regulation protecting the structure____63____will take effect on Jan. 1. This year, Shandong has added 860 patrol posts in seven cities along the Qi wall, mainly recruiting farmers living nearby. Guo Jialian has been patrolling the section of the wall in Guangli village for three months. "I need to check ____64______ there is any damage to the wall that is caused by people digging earth from it. Awareness of protecting the Qi wall _____65_____ (enhance) in recent years. We all know the wall is a cultural relic," says Guo, adding that he frequently sees tourists coming to visit the ancient structure.
02-06

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值