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

Computer HDU - 2196

时间:2019-01-23 19:06:52      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:and   math   graph   color   ane   sam   mat   技术   about   

Computer HDU - 2196 

 

A school bought the first computer some time ago(so this computer‘s id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information. 
技术分享图片

 



Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4. 

InputInput file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.OutputFor each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).Sample Input

5
1 1
2 1
3 1
1 1

Sample Output

3
2
3
4
4

题意:一棵树,问某一个点能够走的不重复点的最长的路径是多少;
思路:先随便找一个点跑一遍树的直径,然后直径的两头各跑一遍dfs
技术分享图片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 40010;

struct Edge {
    int u,v,next,len;
}edge[maxn];

int n;
int tot;
int head[maxn];
bool vis[maxn];
void addedge(int u,int v,int w)
{
    edge[tot].u = u;
    edge[tot].v = v;
    edge[tot].len = w;
    edge[tot].next = head[u];
    head[u] = tot++;
}
int dfs(int start,int d[])
{
    for(int i = 1;i <= n;i++)
        d[i] = INF;
    memset(vis,false,sizeof vis);
    d[start] = 0;
    vis[start] = true;
    queue<int>que;
    que.push(start);
    int maxx = 0;
    int pos;
    while(!que.empty())
    {
        int p = que.front();
        que.pop();
        vis[p] = false;;
        if(maxx < d[p])
        {
            maxx = d[p];
            pos = p;
        }
        for(int i=head[p];i!=-1;i=edge[i].next)
        {
            int v = edge[i].v;
            if(d[v] > d[p] + edge[i].len)
            {
                d[v] = d[p] + edge[i].len;
                if(!vis[v])
                {
                    que.push(v);
                    vis[v] = true;
                }
            }
        }
    }
    return pos;
}
int main()
{
    while(~scanf("%d",&n))
    {
        int d1[maxn],d2[maxn];
        memset(head,-1,sizeof head);
        tot = 0;
        for(int u = 2;u <= n;u++)
        {
            int v,w;
            scanf("%d %d",&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        int st = dfs(1,d1);
        int en = dfs(st,d1);
        dfs(en,d2);
        for(int i=1;i<=n;i++)
            printf("%d\n",max(d1[i],d2[i]));
    }
}
View Code

 

Computer HDU - 2196

标签:and   math   graph   color   ane   sam   mat   技术   about   

原文地址:https://www.cnblogs.com/smallhester/p/10310732.html

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