hdu 4118 Holiday's Accommodation 树形dp

本文探讨了一种优化旅游计划的方法,目标是在不重复目的地的情况下,最大化所有参与者的总旅行距离。通过分析城市间的道路连接,利用树形动态规划算法找到最佳方案。

Holiday's Accommodation

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

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

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 <= 105), representing the number of cities.
Then the followingN-1 lines each contains three integersX, Y,Z(1 <= X, Y <= N, 1 <= Z <= 106), 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

HINT

 

题意

有一颗树,每棵树上都有一个人,然后想让你安排一个旅游计划,让这每一个人都去其他地方,让所有人走过的距离和最大

每一个地方不能有两个人

题解:

首先分析一下,我们对每一个边进行分析,每个边的左边有n个节点,右边有m个节点,那么必然ans+=min(n,m)*边权

仔细想想,就很清楚,假设左边节点比右边少,那么我让左边的节点都到右边去,一定最优。

显然,一个简单的树形dp,跑一发就好了。

注意,这道题我反正dfs爆栈了,我加了#pragma comment(linker, "/STACK:102400000,102400000")

才A掉

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************
#pragma comment(linker, "/STACK:102400000,102400000")
struct node
{
    int x,y;
};
struct edge
{
    int x,y,z;
};
int n,m;
vector<node> e[maxn];
edge ee[maxn];
int dp[maxn];
int vis[maxn];
void dfs(int x)
{
    dp[x]+=1;
    for(int i=0;i<e[x].size();i++)
    {
        node v=e[x][i];
        if(dp[v.x]==0)
        {   
            dfs(v.x);
            dp[x]+=dp[v.x];
        }
    }
}
int main()
{
    //freopen("test.txt","r",stdin);
    int t=read();
    for(int cas=1;cas<=t;cas++)
    {
        memset(dp,0,sizeof(dp));
        n=read();
        for(int i=1;i<=n;i++)
            e[i].clear();
        for(int i=1;i<n;i++)
        {
            ee[i].x=read(),ee[i].y=read(),ee[i].z=read();
            e[ee[i].x].push_back(node{ee[i].y,ee[i].z});
            e[ee[i].y].push_back(node{ee[i].x,ee[i].z});
        }
        ll ans=0;
        dfs(1);
        for(int i=1;i<n;i++)
        {
            int kiss=min(dp[ee[i].x],dp[ee[i].y]);
            ans+=min(kiss,n-kiss)*ee[i].z*2;
        }
        printf("Case #%d: %lld\n",cas,ans);
    }
}

 

转载于:https://www.cnblogs.com/qscqesze/p/4526079.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值