UVALive 6039 Let's Go Green (贪心)

来源:互联网 发布:淘宝瑜伽服模特是谁 编辑:程序博客网 时间:2024/06/05 09:43
 

6039 - Let's Go Green

Time limit: 3.000 seconds 


There are a lot of cities in Tree Land kingdom and each city is connected to other cities with roads such that there is exists exactly one path to go from one city to any other city. Each road in Tree Land connects exactly two different cities.

The king of this kingdom wants to encourage his citizen to have a good sense of natural environmental responsibility. One of his agenda is to promote bicycle, a wheeled human-powered transportation, as the main vehicle for his citizen to travel between places, rather than motor-cycle or auto-mobile which might produce a bad pollution to the environment.

In order to determine the progress of his campaign, the king asked the chancellor to monitor the number of bicycles which pass each road in the kingdom. The chancellor executed the king�s order immediately and reported the result to his king, i.e., for each road in the kingdom he reported the number of bicycles passing those roads in one full day. The king was happy, but it didn�t take a long time for him to realize that this report could be misleading. The report only says the number of bicycles passing each road and doesn�t say anything about the total number of bicycles being used in that day. It also doesn�t contain any information on the direction of reported bicycles.

For example, consider a city structure shown in the figure (left) below. There are 2 bicycles passing road connecting city A and B, 1 on road connecting B and C, 2 on C - D and 1 on C - E. The total number of bicycles recorded is 2 + 1 + 2 + 1 = 6, but it doesn�t mean the actual number of bicycles used is 6. There could be cases where a bicycle is used to travel between city X and Z passing some other cities, such that this one bicycle is recorded several times, once on each road between X and Z. The figure on the right describes one possibility where there are only 3 bicycles, i.e., one is used to travel between A and B, one is used to travel between A and D passing B and C, and one is used to travel between D and E passing C.

Assume that no bicycles are used to pass any road more than once. If a road detects P passing bicycles, then those are P different bicycles.

Now your task is to help the king to determine the minimum number of bicycles used on the report.

Input

The first line of input contains an integer T (T ≤ 10) denoting the number of cases. Each case begins with an integer N (2 ≤ N ≤ 100,000) denoting the number of cities in the kingdom numbered from 1..N. The next N-1 lines each contains three integers A, B and C (1 ≤ A, B ≤ N; A ≠ B; 0 ≤ C ≤ 100) denoting that there are C bicycles recorded passing road connecting A and B. Note that this N-1 lines describe the complete city structures in the kingdom.

Output

For each case, output "Case #X: Y", where X is case number starts from 1 and Y is the minimum number of bicycles used in the respective case.

Sample Input

251 2 22 3 13 4 23 5 171 2 12 3 22 4 12 5 35 6 25 7 3

Output for Sample Input

Case #1: 3Case #2: 5

题意:给n个点n-1条路,每条路给定要通过单车的数量,假设每一量单车都不能通过同一个点2次,问最少要多少步单车才能完成任务

题解:观察到每个点的独立性,于是对每一个点进行贪心计算,将每条路线变为2条有向路径,由于不知道单车会从那一边到哪一边,但是正向和反向结果是一样的,所以方向也可以忽略掉,现在只需要求每一个点所有边可以所有边合并后所需单车的数量......简单点.....就是对每一个点和与其相连的边,求过这个点的最少单车量且满足所有路的单车数,当某一条边的单车数大于其他边单车数的和的时候,明显需要的最少单车数为最大单车数那条边的量,其余情况的话,都可以使其22配对的,若有剩余就要+1,即所有边数(part+1)/2就是这个点过单车的结果,然后将每一个点放入原图,这时候所有边都重复了一次,所以所有点的单车数减去所有边的单车数就是结果了。(ps:至于为什么第二种情况必定可以22配对,本人暂时无法证明,但代码可AC)

AC代码:

#include<stdio.h>#include<stdlib.h>int p[100005],temp[100005],cou;struct point{    int data,next,wei;}edge[200005];void add(int x,int y,int z){    edge[cou].next=p[x];    edge[cou].data=y;    edge[cou].wei=z;    p[x]=cou++;}int cmp(const void *a,const void *b){    return *(int *)a<*(int *)b;}int main(){    int sum,res,part,i,j,t,n,x,y,z,cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(i=0;i<=n;i++) p[i]=-1;        for(sum=cou=i=0;i<n-1;i++)        {            scanf("%d%d%d",&x,&y,&z);            sum=sum+z;            add(x,y,z);            add(y,x,z);        }        for(res=0,i=1;i<=n;i++)        {            cou=part=0;            for(j=p[i];j!=-1;j=edge[j].next)            {                temp[cou]=edge[j].wei;                part+=temp[cou++];            }            qsort(temp,cou,sizeof(int),cmp);            if(2*temp[0]>=part) res+=temp[0];            else res+=(part+1)/2;        }        printf("Case #%d: %d\n",cas++,res-sum);    }    return 0;}

原创粉丝点击