zoukankan      html  css  js  c++  java
  • Computer HDU

    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
    ",max(d1[i],d2[i]));
        }
    }
    View Code
  • 相关阅读:
    Windows下搭建JSP开发环境
    ssh 学习笔记
    18 11 27 长连接 短链接
    18 11 26 用多进程 多线程 携程 实现 http 服务器的创建
    18 11 24 简单的http服务器
    关于 某个智慧树课堂的 机器与机器交流方法
    18 11 23 正则学习
    尝试解决 : Microsoft Visual C++ 14.0 is required 的问题
    18 11 20 网络通信 ----多任务---- 携程 ----生成器
    18 11 20 网络通信 ----多任务---- 携程 ----迭代器
  • 原文地址:https://www.cnblogs.com/smallhester/p/10310732.html
Copyright © 2011-2022 走看看