hdu 3452:Bonsai(最小割)

72 篇文章 0 订阅
19 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=3452

Problem Description

After being assaulted in the parking lot by Mr. Miyagi following the "All Valley Karate Tournament", John Kreese has come to you for assistance. Help John in his quest for justice by chopping off all the leaves from Mr. Miyagi's bonsai tree!
You are given an undirected tree (i.e., a connected graph with no cycles), where each edge (i.e., branch) has a nonnegative weight (i.e., thickness). One vertex of the tree has been designated the root of the tree.The remaining vertices of the tree each have unique paths to the root; non-root vertices which are not the successors of any other vertex on a path to the root are known as leaves.Determine the minimum weight set of edges that must be removed so that none of the leaves in the original tree are connected by some path to the root.

 

 

Input

The input file will contain multiple test cases. Each test case will begin with a line containing a pair of integers n (where 1 <= n <= 1000) and r (where r ∈ {1,……, n}) indicating the number of vertices in the tree and the index of the root vertex, respectively. The next n-1 lines each contain three integers ui vi wi (where ui, vi ∈ {1,……, n} and 0 <= wi <= 1000) indicating that vertex ui is connected to vertex vi by an undirected edge with weight wi. The input file will not contain duplicate edges. The end-of-file is denoted by a single line containing "0 0".

 

 

Output

For each input test case, print a single integer indicating the minimum total weight of edges that must be deleted in order to ensure that there exists no path from one of the original leaves to the root.

 

 

Sample Input

15 15
1 2 1
2 3 2
2 5 3
5 6 7
4 6 5
6 7 4
5 15 6
15 10 11
10 13 5
13 14 4
12 13 3
9 10 8
8 9 2
9 11 3
0 0

Sample Output

16

题意分析:

给出一棵树,边的权值和根节点已知,求根节点和所有叶子节点都不连通的最小代价。

解题思路:

建立一个超级汇点,把叶子节点和汇点连接,注意是叶子节点而不是所有节点,求根节点到超级汇点的最大流。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define N 1020
int book[N], in[N];
int n, m, inf=0x3f3f3f;
struct edge{
	int v;
	int w;
	int rev;
};
vector<edge>e[N];
queue<int>q;
void add()
{
	int u, v, t;
	for(u=1; u<=n; u++)
	{
		if(in[u] == 1 && u!=m)
		{
			e[u].push_back(edge{n+1, inf, e[n+1].size()});
			e[n+1].push_back(edge{u, inf, e[u].size()});
		}
				
	}
}
int dfs(int s, int t, int f)
{
	book[s] = 1;
	int v, d;
	if (s == t)
		return f;
	for (v=0; v<e[s].size(); v++)
	{
		edge &g=e[s][v];
		if(g.w && !book[g.v])
		{
			book[g.v]=1;
			d = dfs(g.v, t, min(f, g.w));
			if(d>0)
			{
				g.w-=d;
				e[g.v][g.rev].w+=d; 
				return d;
			}
		}
	}
	return 0;
}
int FF(int s, int t)
{
	int d, sum=0;
	while(1)
	{
		memset(book ,0, sizeof(book));
		d = dfs(s, t, inf);
		if(d==0)
			break;
		sum+=d;
	}
	return sum;
}
int main()
{
	int u, v, w, i;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		if (n == 0 && m == 0)
			break;
		for(i=1; i<=n+1; i++)
			e[i].clear();
		memset(in, 0, sizeof(in));
		
		for(i = 1; i < n; i++)
		{
			scanf("%d%d%d", &u, &v, &w);
			in[u]++;
			in[v]++;
			e[u].push_back({v, w, e[v].size()});
			e[v].push_back({u, w, e[u].size()-1});
		}
		add();
		printf("%d\n", FF(m, n+1));
	}
	return 0;
}

最开始找根节点是用到bfs,一直错,最后发现n=1时要做特殊考虑。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define N 1020
int book[N];
int n, m, inf=0x3f3f3f;
struct edge{
	int v;
	int w;
	int rev;
};
vector<edge>e[N];
queue<int>q;
void add(int s)
{
	int u, v, t;
	memset(book, 0, sizeof(book));
	book[s]=1;
	q.push(s);
	while (!q.empty())
	{
		t = 0;
		u = q.front();
		q.pop();
		for(v=0; v<e[u].size(); v++)
		{
			edge g=e[u][v];
			if(book[g.v]==0)
			{
				t++;
				book[g.v]=1;
				q.push(g.v);
			}
		}
		if (t == 0)
		{
			e[u].push_back(edge{n+1, inf, e[n+1].size()});
			e[n+1].push_back(edge{u, inf, e[u].size()});
		}
	}
}
int dfs(int s, int t, int f)
{
	book[s] = 1;
	int v, d;
	if (s == t)
		return f;
	for (v=0; v<e[s].size(); v++)
	{
		edge &g=e[s][v];
		if(g.w && !book[g.v])
		{
			book[g.v]=1;
			d = dfs(g.v, t, min(f, g.w));
			if(d>0)
			{
				g.w-=d;
				e[g.v][g.rev].w+=d; 
				return d;
			}
		}
	}
	return 0;
}
int FF(int s, int t)
{
	int d, sum=0;
	while(1)
	{
		memset(book ,0, sizeof(book));
		d = dfs(s, t, inf);
		if(d==0)
			break;
		sum+=d;
	}
	return sum;
}
int main()
{
	int u, v, w, i;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		if (n == 0 && m == 0)
			break;
		if(n==1)
		{
			printf("0\n");
			continue;
		}
		for(i=1; i<=n+1; i++)
			e[i].clear();
		memset(e, 0, sizeof(e));
		for(i = 1; i < n; i++)
		{
			scanf("%d%d%d", &u, &v, &w);
			e[u].push_back({v, w, e[v].size()});
			e[v].push_back({u, w, e[u].size()-1});
		}
		add(m);
		printf("%d\n", FF(m, n+1));
	}
	return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值