题目的意思是给你一个棵树,每天边上有一个权值,现在要想根节点和每个叶子节点完全隔离开来,删除一些边,求最少需要删除的边权值综合是多少?

直接建模,以根节点为汇点,每个叶子节点连接虚拟源点流量无穷,树上的节点按原样建模就可以了。

最后跑一遍最大流等于最小割,完美解决。

召唤代码君:

#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 20010
#define inf 1999999999
using namespace std; int first[maxn],to[maxn],c[maxn],next[maxn],N;
int d[maxn],a[maxn],can[maxn],tag[maxn],TAG=520;
int s,t,n,m,ans;
int Q[maxn],bot,top; void _init()
{
N=-1,ans=0,s=0,t=m;
for (int i=0; i<=n; i++) first[i]=-1,a[i]=0;
} void edge(int U,int V,int W)
{
N++;
to[N]=V,c[N]=W,next[N]=first[U],first[U]=N;
} void _input()
{
int U,V,W;
for (int i=1; i<n; i++)
{
scanf("%d%d%d",&U,&V,&W);
edge(U,V,W),edge(V,U,W);
a[U]++,a[V]++;
}
for (int i=1; i<=n; i++)
if (a[i]==1 && i!=t) edge(s,i,inf),edge(i,s,0);
} bool bfs()
{
TAG++;
Q[bot=top=1]=t,d[t]=1,tag[t]=TAG;
while (bot<=top)
{
int cur=Q[bot++];
for (int i=first[cur]; i!=-1; i=next[i])
{
if (c[i^1]<=0 || tag[to[i]]==TAG) continue;
d[to[i]]=d[cur]+1,Q[++top]=to[i],tag[to[i]]=TAG;
//if (to[i]==s) return true;
}
}
return tag[s]==TAG;
return false;
} int dfs(int cur,int num)
{
if (cur==t) return num;
int tmp=num,k;
for (int i=first[cur]; i!=-1; i=next[i])
{
if (c[i]<=0 || tag[to[i]]!=TAG || d[to[i]]!=d[cur]-1 || can[to[i]]==TAG) continue;
k=dfs(to[i],min(num,c[i]));
if (k) c[i]-=k,c[i^1]+=k,num-=k;
if (num==0) break;
}
if (num) can[cur]=TAG;
return tmp-num;
} int main()
{
while (scanf("%d%d",&n,&m)&&(n|m))
{
_init();
_input();
while (bfs()) ans+=dfs(s,inf);
printf("%d\n",ans);
}
return 0;
}

  

HDU3452_Bonsai的更多相关文章

随机推荐

  1. SSIS 数据流的执行树和数据管道

    数据流组件的设计愿景是快速处理海量的数据,为了实现该目标,SSIS数据源引擎需要创建执行树和数据管道这两个数据结构,而用户为了快速处理数据流,必须知道各个转换组件的阻塞性,充分利用流式处理流程,利用更 ...

  2. 【JUC源码解析】ForkJoinPool

    简介 ForkJoin 框架,另一种风格的线程池(相比于ThreadPoolExecutor),采用分治算法,工作密取策略,极大地提高了并行性.对于那种大任务分割小任务的场景(分治)尤其有用. 框架图 ...

  3. Eclipse实用插件

    Eclipse实用插件 安装:Help - Eclipse Marketplace 查看图片:QuickImage 主题:Darkest Dark 代码风格:https://blog.csdn.net ...

  4. Jmeter+ant+jenkins接口自动化测试 平台搭建(三)

    四.报告优化 Jmeter 默认生成报告不是很详细,因此我们需要进行优化.这里我们使用新的报告模板:默认的报告模板是 jmeter-results-detail-report_21.xsl 先上效果图 ...

  5. elementUI实现前端分页

    按照他的文档来写分页,最主要的是el-table里面展示的数据怎么处理 <el-table :data="AllCommodityList.slice((currentPage-1)* ...

  6. skipfish介绍

    skipfish 开发语言:C语言 命令行扫描器 主动扫描web安全评估工具 谷歌开发 已经不再进行维护 重点关注web代码 通过两种方式进项扫描:1.字典枚举 2.递归爬网 优点:速度快.支持多路单 ...

  7. 报错android.view.InflateException: Binary XML file line #11: Attempt to invoke virtual method 'boolean

    出现这种问题,打开Android monitor的调试信息发现是 android.view.InflateException: Binary XML file line #11: Attempt to ...

  8. Spark Streaming流式处理

    Spark Streaming介绍 Spark Streaming概述 Spark Streaming makes it easy to build scalable fault-tolerant s ...

  9. DockerCon2017前瞻 - Docker企业版体验

    DockerCon 2017将于四月17号在美国Austin召开.在去年DockerCon上,Docker公司一系列的发布吹响了进军企业市场的号角.今天,容器技术已经愈发成熟,被越来越多的企业所关注和 ...

  10. route命令详情

    基础命令学习目录首页 原文链接:https://www.cnblogs.com/lpfuture/p/5857738.html 考试题一:linux下如何添加路由(百度面试题) 以上是原题,老男孩老师 ...