码迷,mamicode.com
首页 > 其他好文 > 详细

(最小生成树) hdu 4424

时间:2015-05-27 22:37:45      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

Conquer a New Region

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1382    Accepted Submission(s): 455


Problem Description
The wheel of the history rolling forward, our king conquered a new region in a distant continent.
There are N towns (numbered from 1 to N) in this region connected by several roads. It‘s confirmed that there is exact one route between any two towns. Traffic is important while controlled colonies are far away from the local country. We define the capacity C(i, j) of a road indicating it is allowed to transport at most C(i, j) goods between town i and town j if there is a road between them. And for a route between i and j, we define a value S(i, j) indicating the maximum traffic capacity between i and j which is equal to the minimum capacity of the roads on the route. 
Our king wants to select a center town to restore his war-resources in which the total traffic capacities from the center to the other N - 1 towns is maximized. Now, you, the best programmer in the kingdom, should help our king to select this center.
 

 

Input
There are multiple test cases.
The first line of each case contains an integer N. (1 <= N <= 200,000)
The next N - 1 lines each contains three integers a, b, c indicating there is a road between town a and town b whose capacity is c. (1 <= a, b <= N, 1 <= c <= 100,000)
 

 

Output
For each test case, output an integer indicating the total traffic capacity of the chosen center town.
 

 

Sample Input
4 1 2 2 2 4 1 2 3 1 4 1 2 1 2 4 1 2 3 1
 

 

Sample Output
4 3
 

 

Source
 
 
题意:
给你一颗树,树中每条边有一权值c,对于从i节点到j节点的路径,其权值定义为该路径上边权值的最小值,于是问找到一个中心点,使得从该点到其他节点的路径权值和最大,求该最大值。
 
思路:
想到并查集这个数据结构,先对边从大到小排序,然后枚举边,合并点,将点A合并到B里面要满足以B的根节点为中心的权值比以A的根节点为中心的权值要大。
 
妈的。。。注意long long 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
int n,fa[2000010],sum[2000010];
long long dist[2000010];
struct node
{
    int x,y,w;
}e[2000010];
int find(int x)
{
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
bool cmp(node a,node b)
{
    return a.w>b.w;
}
void Union(int x,int y,int w)
{
    int fx,fy;
    fx=find(x),fy=find(y);
    if(fx!=fy)
    {
        long long x1=(long long)(dist[fx]+(long long)sum[fy]*w);
        long long x2=(long long)(dist[fy]+(long long)sum[fx]*w);
        if(x1>x2)
        {
            fa[fy]=fx;
            sum[fx]+=sum[fy];
            dist[fx]=x1;
        }
        else
        {
            fa[fx]=fy;
            sum[fy]+=sum[fx];
            dist[fy]=x2;
        }
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            fa[i]=i,sum[i]=1,dist[i]=0;
        for(int i=1;i<n;i++)
            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
        sort(e+1,e+n,cmp);
        for(int i=1;i<n;i++)
            Union(e[i].x,e[i].y,e[i].w);
        cout<<dist[find(1)]<<endl;
    }
    return 0;
}

  

(最小生成树) hdu 4424

标签:

原文地址:http://www.cnblogs.com/water-full/p/4534421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!