zoukankan      html  css  js  c++  java
  • hdu 3452:Bonsai(最小割)

    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
    ", 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
    ");
    			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
    ", FF(m, n+1));
    	}
    	return 0;
    }
    
  • 相关阅读:
    “#ifdef __cplusplus extern "C" { #endif”的定义
    【原创】分布式之redis复习精讲
    python爬虫入门(六) Scrapy框架之原理介绍
    PYTHON面试
    14.Ubuntu基本命令
    python爬虫入门(五)Selenium模拟用户操作
    python爬虫入门(四)利用多线程爬虫
    python爬虫入门(三)XPATH和BeautifulSoup4
    python爬虫入门(一)urllib和urllib2
    7.Ajax
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852587.html
Copyright © 2011-2022 走看看