cdoj 791 Frozen Rose-Heads

//本来想做白书上一题 结果发现又要二染色 又要dp的 想了两个小时没想通 然后做了个傻逼题安慰自己

解:不多说,就是递归到叶节点,然后回来的时候在解决子树和直接删边的代价中间取个最小值

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<queue>
 8 #include<vector>
 9 #include<map>
10 #include<stack>
11 #include<string>
12 
13 using namespace std;
14 
15 const int MAXN=1007;
16 const int MAXINT=2000000000;
17 
18 int n,c;
19 vector <int> G[MAXN];
20 int cost[MAXN][MAXN];
21 
22 int dfs(int now,int fa){
23     if (G[now].size()==1 && G[now][0]==fa){
24             return MAXINT;
25     }
26     int tmp=0;
27     for (int i=0;i<(int)G[now].size();i++){
28             if (G[now][i]==fa) continue;
29             tmp=tmp+min(dfs(G[now][i],now),cost[now][G[now][i]]);
30     }
31     return tmp;
32 }
33 
34 int main(){
35     while (scanf("%d%d",&n,&c)==2){
36             for (int i=1;i<=n;i++) G[i].clear();
37             for (int i=0;i<n-1;i++){
38                     int x,y,z;
39                     scanf("%d%d%d",&x,&y,&z);
40                     cost[x][y]=z;
41                     cost[y][x]=z;
42                     G[x].push_back(y);
43                     G[y].push_back(x);
44             }
45             printf("%d\n",dfs(c,-1));
46     }
47     return 0;
48 }
49 /*
50 3 1
51 2 1 5
52 1 3 4
53 7 7
54 7 6 10
55 7 5 10
56 6 4 1
57 6 3 1
58 5 2 1
59 5 1 2
60 
61 4 1
62 1 2 1
63 2 3 1
64 2 4 1
65 */
View Code

 

转载于:https://www.cnblogs.com/baby-mouse/p/4609255.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值