UVA10672 POJ1909 ZOJ2374 Marbles on a treeBFS

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10672 POJ1909 ZOJ2374 Marbles on a treeBFS相关的知识,希望对你有一定的参考价值。

Marbles on a tree
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1890 Accepted: 885

Description

n boxes are placed on the vertices of a rooted tree, which are numbered from 1 to n, 1 <= n <= 10000. Each box is either empty or contains a number of marbles; the total number of marbles is n.
The task is to move the marbles such that each box contains exactly one marble. This is to be accomplished be a sequence of moves; each move consists of moving one marble to a box at an adjacent vertex. What is the minimum number of moves required to achieve the goal?

Input

The input contains a number of cases. Each case starts with the number n followed by n lines. Each line contains at least three numbers which are: v the number of a vertex, followed by the number of marbles originally placed at vertex v followed by a number d which is the number of children of v, followed by d numbers giving the identities of the children of v.
The input is terminated by a case where n = 0 and this case should not be processed.

Output

For each case in the input, output the smallest number of moves of marbles resulting in one marble at each vertex of the tree.

Sample Input

9
1 2 3 2 3 4
2 1 0
3 0 2 5 6
4 1 3 7 8 9
5 3 0
6 0 0
7 0 0
8 2 0
9 0 0
9
1 0 3 2 3 4
2 0 0
3 0 2 5 6
4 9 3 7 8 9
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
9
1 0 3 2 3 4
2 9 0
3 0 2 5 6
4 0 3 7 8 9
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
0

Sample Output

7
14
20

Source

Waterloo local 2004.06.12

问题链接UVA10672 POJ1909 ZOJ2374 Marbles on a tree
问题简述:(略)
问题分析:用BFS来解决,不解释。这个题分类上属于用拓扑排序来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10672 POJ1909 ZOJ2374 Marbles on a tree */

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

const int N = 10000;
int degree[N], marbles[N], p[N];

int main()

    memset(degree, 0, sizeof(degree));
    memset(marbles, 0, sizeof(marbles));
    memset(p, 0, sizeof(p));

    int n, v, m, d;
    while (~scanf("%d", &n) && n) 
        for (int i = 0; i < n; i++) 
            scanf("%d%d%d", &v, &m, &d);
            degree[v] = d;
            marbles[v] = m;
            for (int j = 0; j < d; j++) 
                int child;
                scanf("%d", &child);
                p[child] = v;
            
        

        queue<int> q;
        for (int i = 1; i <= n; i++)
            if (degree[i] == 0) q.push(i);

        int ans = 0;
        while (!q.empty()) 
            int u = q.front();
            q.pop();
            int parent = p[u];
            if (parent == 0) break;
            if (marbles[u] != 1) 
                marbles[parent] += marbles[u] - 1;
                ans += abs(marbles[u] - 1);
                marbles[u] = 1;
            

            if (--degree[parent] == 0) q.push(parent);
        

        printf("%d\\n", ans);
    

    return 0;

以上是关于UVA10672 POJ1909 ZOJ2374 Marbles on a treeBFS的主要内容,如果未能解决你的问题,请参考以下文章

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA542 POJ2261 ZOJ1950 France ‘98概率

UVA10670 POJ1907 ZOJ2372 Work Reduction贪心

UVA10081 POJ2537 ZOJ1883 Tight WordsDP

UVA10277 POJ2646 ZOJ1856 Boastin‘ Red Socks数学

POJ2689 ZOJ1842 UVA10140 Prime Distance筛选法