HDU 4118 Holiday's Accommodation(树形DP)

/*
#pragma comment(linker, "/STACK:10240000000000,10240000000000")
*/
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
const int MAXN=200010;
struct Node
{
    int to,next;
    int len;
}edge[MAXN*2];

int head[MAXN];
int tol;
int num[MAXN];//从这个点以下的结点数
long long ans;
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
    memset(num,0,sizeof(num));
}
void add(int a,int b,int len)
{
    edge[tol].to=b;
    edge[tol].len=len;
    edge[tol].next=head[a];
    head[a]=tol++;

    edge[tol].to=a;
    edge[tol].len=len;
    edge[tol].next=head[b];
    head[b]=tol++;
}
int n;
//递归形式会超出内存
/*
void dfs(int u,int pre)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==pre)continue;
        dfs(v,u);
        num[u]+=num[v];
        ans+=(long long)edge[i].len*min(num[v],n-num[v]);
    }
    num[u]++;
}
*/

int sta[MAXN];
bool vis[MAXN];
void dfs(int u)
{
    memset(vis,false,sizeof(vis));
    int top=0;
    sta[top++]=u;
    vis[u]=true;
    while(top>0)
    {
        bool flag=true;
        int t=sta[top-1];
        for(int i=head[t];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(vis[v])continue;
            flag=false;
            sta[top++]=v;
            vis[v]=true;
        }
        if(!flag)continue;
        top--;
        for(int i=head[t];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(num[v]!=0)
            {
                num[t]+=num[v];
                ans+=(long long)edge[i].len*min(num[v],n-num[v]);
            }
        }
        num[t]++;
    }
}

int main()
{
    int T;
    int iCase=0;
    int u,v,w;
    scanf("%d",&T);
    while(T--)
    {
        iCase++;
        scanf("%d",&n);
        init();
        ans=0;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        dfs(1);
        printf("Case #%d: %I64d\n",iCase,ans*2);
    }
    return 0;
}

 

Holiday’s Accommodation

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

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
2011 Asia ChengDu Regional Contest  

 

Recommend chenyongfu     刚好形成一棵树。 可以知道每条边要走的次数刚好的是这条边两端的点数的最小值的两倍。 所以简单树形DP就可以解决。   但是普通的dfs会爆内存。。。可以手动扩大内存,在HDU可以AC。   所以要用非递归的dfs,用一个栈来实现。 貌似现场赛这道题没有出现爆内存的情况吧,都是1A的。。。   看代码:非递归实现,树形DP;  

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/10/05/2712222.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞