UESTC_Frozen Rose-Heads CDOJ 791

The winter is coming and all the experts are warning that it will be the coldest one in the last hundred years. Freddy needs to make sure that his garden does not sustain any damage. One of the most important tasks is to make sure that no water remains in his large watering system.

All the water comes from a central node and is distributed by pipes to neighboring nodes and soon. Each node is either a sprinkler (rose head) with no outgoing pipe or an internal node with one or more outgoing pipes leading to some other nodes. Every node has exactly one incoming pipe, except for the central node which takes the water directly from a well and has no incoming pipe. Every pipe has a valve that stops all the water going through the pipe. The valves are of different quality and age, so some may be harder to close than others.

Freddy knows his valves well and has assigned a value to each pipe representing the amount of effort needed to close the corresponding valve. He asks you to help him count the minimum effort needed to close some valves so that no water goes to the sprinklers.

Input

The input contains several test cases. Each test case starts with a line with two integers, the number of nodes n (2n1,000), and the number of the central node c (1cn). Each of the next n1 lines represents one pipe and contains three integers, uv (1u,vn) and w(1w1,000), where u and v are the nodes connected by a pipe and w is the effort needed to close the valve on that pipe. You may assume that every node is reachable from the central node.

Output

For each test case, output a single line containing the minimum sum of efforts of valves to be closed, such that the central node gets separated from all sprinklers.

 

解题报告

基础树形DP。。没啥好讲的

 

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <set>
using namespace std;
const int maxn = 1000 + 25;
const int INF = 1 << 25;
set<int>E[maxn];
int n,c,cost[maxn][maxn],pre[maxn],dp[maxn];


void Delete_n(int cur,int fat)
{
    pre[cur] = fat;
    for(set<int>::iterator it = E[cur].begin() ; it != E[cur].end() ; ++ it)
     {
         int tar = *it;
         if (*it != fat)
          Delete_n(tar,cur);
     }
    if (fat != -1)
      E[cur].erase(fat);
}

int slove(int cur)
{
    if (dp[cur] != -1)
     return dp[cur];
    int & ans = dp[cur] = INF;
    if (!E[cur].size())
     return ans = cost[cur][pre[cur]];
    int res = 0;
    for(set<int>::iterator it = E[cur].begin() ; it != E[cur].end() ; ++ it)
     {
          res += slove(*it);
     }
    if (cur == c)
     ans = res;
    else
     ans = min(res,cost[cur][pre[cur]]);
    return ans;
}

int main(int argc,char * argv[])
{  
  while(scanf("%d%d",&n,&c) == 2)
   {
         c--;
         memset(dp,-1,sizeof(dp));
         for(int i = 0 ; i < n-1 ; ++ i)
           {
               int u,v,w;
               scanf("%d%d%d",&u,&v,&w);
               E[u-1].insert(v-1);
               E[v-1].insert(u-1);
               cost[u-1][v-1] = cost[v-1][u-1] = w;
           }
          Delete_n(c,-1);
          slove(c);
          cout << dp[c] << endl;
          for(int i = 0 ; i < n ; ++ i)
           E[i].clear();
   }
  return 0;
}

 

转载于:https://www.cnblogs.com/Xiper/p/4465685.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值