可以转化成最小割的求解,题目其实就是要求把点分成两个集合,增加一个超级汇点,一部分的点在根节点所在集合内,一部分节点在超级汇点所在的集合内,这两就分开了,又要求费用最小,那么就是最小割。

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+;
const int INF=0x7FFFFFFF; struct Edge { int from,to,cap,flow; };
vector<Edge>edges;
vector<int>G[maxn];
vector<int>ljb[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int flag[maxn];
int cost[maxn][maxn];
int n,m,s,t; //求出层次网络
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s]=;
vis[s]=;
while(!Q.empty())
{
int x=Q.front();
Q.pop();
for(int i=;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} //加边
void AddEdge(int from,int to,int cap)
{
Edge r;
r.from=from;r.to=to;r.cap=cap;r.flow=;
edges.push_back(r);
Edge d;
d.from=to;d.to=from;d.cap=;d.flow=;
edges.push_back(d);
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} //每个阶段来一次DFS增广
int DFS(int x,int a)
{
if(x==t||a==) return a;
int flow=,f;
for(int i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
} //多个阶段,多次建立层次网络。
int Maxflow(int ss,int tt)
{
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow+=DFS(ss,INF);
}
return flow;
} void dfs(int x,int fa)
{
if(fa!=-)
AddEdge(fa,x,cost[fa][x]);
int FD=;
for(int i=;i<ljb[x].size();i++)
{
if(!flag[ljb[x][i]])
{
FD=;
flag[ljb[x][i]]=;
dfs(ljb[x][i],x);
}
}
if(FD==) AddEdge(x,t,INF);
} int main()
{
while(~scanf("%d%d",&n,&s))
{
if(n==&&s==) break; t=n+;
edges.clear();
for(int i=;i<maxn;i++)
{
G[i].clear();
ljb[i].clear();
} for(int i=;i<=n-;i++)
{
int uu,vv,cc;
scanf("%d%d%d",&uu,&vv,&cc);
ljb[uu].push_back(vv);
ljb[vv].push_back(uu);
cost[uu][vv]=cc;
cost[vv][uu]=cc;
}
memset(flag,,sizeof(flag));
flag[s]=;
dfs(s,-); int ans=Maxflow(s,t);
if(ans==INF) printf("0\n");
else printf("%d\n",ans);
}
return ;
}

HDU 3452 Bonsai的更多相关文章

  1. HDU 3452 Bonsai(网络流之最小割)

    题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #inc ...

  2. HDU 3452 Bonsai(树形dp)

    Bonsai Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. 设计模式-GoF23

    书呢,是2012年双11买的. 没有面向对象程序经验的人,果然还是看不懂的.

  2. oc UIAlertController封装

    #define SHOWALERT(MESSAGE) \ UIAlertController *alertController = [UIAlertController alertController ...

  3. FD.io vpp 框架转发图

    在ip4-icmp-input 与 ip4-udp-input后可以注册后续的处理函数,ip4-icmp-input根据 icmp的报文类型选择相应的处理函数,而ip4-udp-input根据端口选择 ...

  4. HDU 1517 A Multiplication Game 博弈

    题目大意:从1开始Stan与Ollie经行博弈,stan先手,每次将当前数乘上(2~9)间的任意数,最后一次操作后大于等于n的人获胜. 题目思路: 1-9 stan 胜 10-18 ollie胜 19 ...

  5. scala优点以及eclipse上安装scala插件

    可拓展 (面向对象,函数式编程) 静态类型化 (可检验,安全重构) 兼容JAVA (类库调用,互操作) 支持并发控制 (强计算能力,自定义其他控制结构) 语法简洁 (代码行短,类型推断,抽象控制) 插 ...

  6. C++线程安全的单例模式

    1.在GCC4.0之后的环境下: #include <iostream> using namespace std;template <typename T>class Sing ...

  7. Ubuntu 软件 安装 下载 及更新

    1  软件安装 sudo apt-get install 2 软件搜索 sudo   apt-cache search 3  系统已经安装了什么软件 dpkg  -l 是否确切安装了某软件 dpkg ...

  8. ListView上下线添加

    <com.jclick.swipelistview.byzswipemenulistview.InScrollviewSwipeMenuListView android:id="@+i ...

  9. cursor:pointer 什么意思?

    cursor规则是设定网页浏览时用户鼠标指针的样式,也就是鼠标的图形形状cursor:pointer设定鼠标的形状为一只伸出食指的手,这也是绝大多数浏览器里面鼠标停留在网页链接上方时候的样式另外可以选 ...

  10. jq获取上级、同级、下级元素

    下面介绍JQUERY的父,子,兄弟节点查找方法 jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$(&qu ...