首页 > ACM题库 > HDU-杭电 > HDU 4118-Holiday’s Accommodation-动态规划-[解题报告]HOJ
2015
04-16

HDU 4118-Holiday’s Accommodation-动态规划-[解题报告]HOJ

Holiday’s Accommodation

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

Case #1: 18
Case #2: 62

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

题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值。

思路:对于每一个边,都可以把这个变量偷的点交换,这样这些点都要走这个边,就是这个边要走2*min(左端的点数,右端的点数)。但是会爆栈,可以人工开栈,也可用非递归的方法写dfs,同样可以过。

#pragma comment(linker, "/STACK:10240000000000,10240000000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define MAXN 101005
#define MAXM 400005
#define INF 1000000000

using namespace std;
struct EDGE
{
    int v, next;
    long long w;
}edge[MAXM];
int head[MAXN], e;
int n;
int num[MAXN];
long long ans;
void init()
{
    ans = 0;
    memset(head, -1, sizeof(head));
    e = 0;
    memset(num, 0, sizeof(num));
}
void add(int u, int v, long long w)
{
    edge[e].v = v;
    edge[e].w = w;
    edge[e].next = head[u];
    head[u] = e++;
}
void dfs(int u, int fa)
{
    num[u] = 1;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if(v != fa)
        {
            dfs(v, u);
            num[u] += num[v];
            long long tmp = min(num[v], n - num[v]);
            ans += tmp * edge[i].w * 2LL;
        }
    }
}
int main()
{
    int T, cas = 0, x, y, z;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        init();
        for(int i = 1; i < n; i++)
        {
            scanf("%d%d%d", &x, &y, &z);
            add(x, y, z);
            add(y, x, z);
        }
        dfs(1, 0);
        printf("Case #%d: %I64d\n", ++cas, ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/xcockroach/article/details/7957304


  1. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.