HDU 3721 Building Roads

来源:互联网 发布:中兴通讯知乎 编辑:程序博客网 时间:2024/05/17 23:30

题目链接:HDU3721


Building Roads

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 645    Accepted Submission(s): 228


Problem Description
There is a magic planet in the space. There is a magical country on the planet. There are N cities in the country. The country is magical because there are exactly N-1 magic roads between the N cities, and from each city, it is possible to visit any other city. But after the huge expansion of the country, the road system seems to be messy. The moderator decided to rebuild the road system.

As a worker, I cannot do too much things. I could just move one road to another position connecting arbitrary 2 cities using my magic, keeping its length unchanged. Of course, afterwards all the N cities have to be still connected. I wonder how to move in order to make the farthest distance between any two cities minimum. Could you solve it for me?
 

Input
The first line of the input is one integer T (T ≤ 10), and then T test cases follow.
Each test case begins with a line contains only one integer N (N ≤ 2500), means there are N magic cities. The cities are numbered from 0 to N-1.
Following N-1 lines, each line has 3 integers a, b and c, means there is a magic road between a and b with distance c. (0 <= a, b < N, 0 < c <= 1000)
 

Output
For each test case, output the case number and the corresponding minimum farthest distance. See sample for detailed format.
 

Sample Input
240 1 21 2 22 3 250 1 11 2 22 3 33 4 4
 

Sample Output
Case 1: 4Case 2: 7
 

Source
2010 Asia Tianjin Regional Contest
 

Recommend
zhouzeyong
 


树的直径的应用。


#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>using namespace std;vector<pair<int,int> > gra[2600];int zhif,zhi;int dx,dy;int dep[2600];int depf[2600];int p[2600];int pf[2600];int dfs(int s,int pp,int d){    dep[s]=d;    if(d>zhi) zhi=d,zhif=s;    int ma=d,f=-1,t;    for(int i=0;i<gra[s].size();i++)        if(gra[s][i].first!=pp)        {            if(s==dx && gra[s][i].first==dy) continue;            if(s==dy && gra[s][i].first==dx) continue;            t=dfs(gra[s][i].first,s,d+gra[s][i].second);            if(t>ma) ma=t,f=gra[s][i].first;        }    p[s]=f;    return ma;}int main(){    int t,ti=1;    scanf("%d",&t);    while(t--)    {        int n,x,y,w,a,b;        scanf("%d",&n);        for(int i=0;i<n;i++)            gra[i].clear();        printf("Case %d: ",ti++);        for(int i=1;i<n;i++)        {            scanf("%d%d%d",&x,&y,&w);            gra[x].push_back(make_pair(y,w));            gra[y].push_back(make_pair(x,w));        }        if(n==1) printf("0\n");        else        {            int ans=0x3fffffff;            zhif=0;dx=dy=-1;zhi=-1;            dfs(0,-1,0);            x=zhif;            zhi=-1;            dfs(zhif,-1,0);            for(int i=0;i<n;i++)                pf[i]=p[i],depf[i]=dep[i];            y=zhif;            while(x!=y)            {                dx=x;dy=pf[x];zhi=-1;                dfs(x,-1,0);                int tx=zhif,now=0,sum,fa=0x3fffffff,faa,ff,ffa;                zhi=-1;                sum=dfs(tx,-1,0);                ff=sum;                fa=sum;                while(tx!=zhif)                {                    now+=dep[p[tx]]-dep[tx];                    sum-=dep[p[tx]]-dep[tx];                    fa=min(max(sum,now),fa);                    tx=p[tx];                }                zhi=-1;                dfs(y,-1,0);                tx=zhif,now=0;zhi=-1;                sum=dfs(tx,-1,0);                faa=sum;ffa=sum;                while(tx!=zhif)                {                    now+=dep[p[tx]]-dep[tx];                    sum-=dep[p[tx]]-dep[tx];                    faa=min(max(sum,now),faa);                    tx=p[tx];                }                ans=min(ans,max(fa+depf[dy]-depf[dx]+faa,max(ffa,ff)));                x=pf[x];            }            printf("%d\n",ans);        }    }    return 0;}


原创粉丝点击