HDU4118

Holiday's Accommodation

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 200000/200000 K (Java/Others)
Total Submission(s): 2135    Accepted Submission(s): 595


Problem Description
Nowadays, people have many ways to save money on accommodation when they are on vacation.
One of these ways is exchanging houses with other people.
Here is a group of N people who want to travel around the world. They live in different cities, so they can travel to some other people's city and use someone's house temporary. Now they want to make a plan that choose a destination for each person. There are 2 rules should be satisfied:
1. All the people should go to one of the other people's city.
2. Two of them never go to the same city, because they are not willing to share a house.
They want to maximize the sum of all people's travel distance. The travel distance of a person is the distance between the city he lives in and the city he travels to. These N cities have N - 1 highways connecting them. The travelers always choose the shortest path when traveling.
Given the highways' information, it is your job to find the best plan, that maximum the total travel distance of all people.
 

Input
The first line of input contains one integer T(1 <= T <= 10), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(2 <= N <= 10 5), representing the number of cities.
Then the followingN-1 lines each contains three integersX, Y,Z(1 <= X, Y <= N, 1 <= Z <= 10 6), means that there is a highway between city X and city Y , and length of that highway.
You can assume all the cities are connected and the highways are bi-directional.
 

Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y represents the largest total travel distance of all people.
 

Sample Input
  
  
2 4 1 2 3 2 3 2 4 3 2 6 1 2 3 2 3 4 2 4 1 4 5 8 5 6 5
 

Sample Output
  
  
Case #1: 18 Case #2: 62
 

Source
 

Recommend
chenyongfu   |   We have carefully selected several similar problems for you:   4119  4112  4114  4115  4117 
 

Statistic |  Submit |  Discuss |  Note

每条边被走过的最多次数为该边两边节点数的最小值,dfs一下就可以了,答案会超int,要用long long。


#include <iostream>
#include <algorithm>
#include <cstring>
#pragma comment(linker,"/STACK:102400000,1024000")
const int N=100009;
using namespace std;

struct Edge
{
    int a,v,num,next;
}edge[N<<1];
int node[N],n,k,d,t,se;

void addEdge(int a,int b,int v)
{
    //cout<<"**************"<<se<<" "<<a<<" "<<b<<endl;
    edge[se].a=b;
    edge[se].v=v;
    edge[se].num=-1;
    edge[se].next=node[a];
    node[a]=se++;
}

int dfs(int x,int pre)
{
    int ans=1;
    for(int s=node[x];s>=0;s=edge[s].next)
    {
        if(edge[s].a==pre) continue;
        edge[s].num=dfs(edge[s].a,x);
        ans+=edge[s].num;
    }
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    int T,cas=1;
    cin>>T;
    while(T--)
    {
        int i,j,a,b,c;
        se=0;
        cin>>n;
        memset(node,-1,sizeof(node));
        for(i=0;i<n-1;i++)
        {
            cin>>a>>b>>c;
            addEdge(a,b,c);
            addEdge(b,a,c);
        }
        c=dfs(1,0);
        if(c!=n && c!=0) exit(-1);
        long long int ans=0;
        for(i=0;i<se;i++) if(edge[i].num>0)
        {
            ans+=(long long int)min(n-edge[i].num,edge[i].num)*edge[i].v*2;
        }
        cout<<"Case #"<<cas++<<": "<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值