HDU3452_Bonsai
题目的意思是给你一个棵树,每天边上有一个权值,现在要想根节点和每个叶子节点完全隔离开来,删除一些边,求最少需要删除的边权值综合是多少?
直接建模,以根节点为汇点,每个叶子节点连接虚拟源点流量无穷,树上的节点按原样建模就可以了。
最后跑一遍最大流等于最小割,完美解决。
召唤代码君:
#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的更多相关文章
随机推荐
- python中偏函数的应用
一.什么是偏函数? (1)在Python的functools模块众多的功能中,其中有一个就是偏函数,我们称之为 partial function 模块的概念我们下一篇在细讲. (2)我们都听过偏将军吧 ...
- 微信小程序——手把手教你写一个微信小程序
前言 微信小程序年前的跳一跳确实是火了一把,然后呢一直没有时间去实践项目,一直想搞但是工作上不需要所以,嗯嗯嗯嗯嗯emmmmm..... 需求 小程序语音识别,全景图片观看,登录授权,获取个人基本信息 ...
- svn检出项目后,serverlet包 报错
因为缺少一个包 servlet-api.jar 没引.
- WebGL------osg框架学习二
今天我们继续来学习osg.js框架.上一篇我们介绍了DrawActor对象绘制操作类和Drawable可绘制对象类,我们大致知道了osg对Drawable可绘制对象的绘制流程管理.今天我们要继续介绍S ...
- java高并发之锁的使用以及原理浅析
锁像synchronized同步块一样,是一种线程同步机制.让自Java 5开始,java.util.concurrent.locks包提供了另一种方式实现线程同步机制——Lock.那么问题来了既然都 ...
- java基础---类加载和对象创建过程
类中可以存在的成员: class A{ 静态成员变量: 非静态成员变量: 静态函数: 非静态函数: 构造函数 A(..){...} 静态代码块 static{...} 构造代码块 {...} } 类加 ...
- 最优方向法(MOD)
算法描述 求解模型: \[\min\sum\limits_i\|x_i\|_0 \quad \mathrm{s.t.} \; \|Y-DX\|^2_F \leq \varepsilon\] 或 \[\ ...
- 【Docker】第三篇 Docker容器管理
一.Docker容器概述: 简单理解容器是镜像的一个实例. 镜像是静态的只读文件,而容器的运行需要可写文件层. 二.创建容器 [root@web130 ~]# docker create -it ub ...
- ubuntu下import matplotlib错误解决办法
环境:ubuntu16.04,python2.7,tensorflow1.4.0 问题: ImportError: No moudule named _tkinter, please install ...
- 使用SSD目标检测c++接口编译问题解决记录
本来SSD做测试的Python接口用起来也是比较方便的,但是如果部署集成的话,肯定要用c++环境,于是动手鼓捣了一下. 编译用的cmake,写的CMakeList.txt,期间碰到一些小问题,简单记录 ...