zoukankan      html  css  js  c++  java
  • 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
    ",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
  • 相关阅读:
    C# 获取 PC 序列号
    C# 获取进程退出代码
    PHP mail() 函数
    PHP ezmlm_hash() 函数
    PHP Mail 函数
    PHP libxml_clear_errors() 函数
    PHP setrawcookie() 函数
    创建用户
    [FJOI2018]领导集团问题
    C# 获取进程退出代码
  • 原文地址:https://www.cnblogs.com/baby-mouse/p/4609255.html
Copyright © 2011-2022 走看看