zoukankan      html  css  js  c++  java
  • HDU 4679 Terrorist’s destroy

    Terrorist’s destroy

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 621    Accepted Submission(s): 184

    Problem Description
    There is a city which is built like a tree.A terrorist wants to destroy the city's roads. But now he is alone, he can only destroy one road, then the city will be divided into two cities. Impression of the city is a number defined as the distance between the farthest two houses (As it relates to the fare).When the terrorist destroyed a road, he needs to spend some energy, assuming that the number is a.At the same time,he will get a number b which is maximum of the Impression of two cities. The terrorist wants to know which road to destroy so that the product of a and b will be minimized.You should find the road's id.
    Note that the length of each road is one.
     
    Input
    The first line contains integer T(1<=T<=20), denote the number of the test cases.
    For each test cases,the first line contains a integer n(1 < n <= 100000);denote the number of the houses;
    Each of the following (n-1) lines contains third integers u,v,w, indicating there is a road between house u and houses v,and will cost terrorist w energy to destroy it.The id of these road is number from 1 to n-1.(1<=u<=n , 1<=v<=n , 1<=w<=10000)
     
    Output
    For each test case, output the case number first,and then output the id of the road which the terrorist should destroy.If the answer is not unique,output the smallest id.
     
    Sample Input
    2 5 4 5 1 1 5 1 2 1 1 3 5 1 5 1 4 1 1 3 1 5 1 1 2 5 1
     
    Sample Output
    Case #1: 2 Case #2: 3
     
    Source
     
    Recommend
    zhuyuanchen520
     
    各种bfs都是为了最后的枚举边,在枚举边的时候如果边不在直径上,就w*直径,在直径上借助先前预处理出来的当断裂时两城市的最大距离(dp加速)
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <queue>
    #define N 100100
    #define INF 0x7ffffff
    using namespace std;
    struct infor
    {
        int u,v,w,next,pt;
    }a[2*N],Map[2*N];
    int b[N],level[N],dep[N],pre[N],temp[N];
    int dp1[N],dp2[N],sum[N],Top,n;
    bool check[N],bir[N],inque[N];
    int main()
    {
        //freopen("data.in","r",stdin);
        void addeage(int x,int y,int pos);
        void ra_bfs(int &s,int &e);
        void DP(int *p);
        int t,tem=1;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            memset(b,-1,sizeof(b));
            Top=1;
            for(int i=1;i<=n-1;i++)
            {
                int x,y,w;
                scanf("%d %d %d",&x,&y,&w);
                Map[i].u = x;
                Map[i].v = y;
                Map[i].w = w;
                addeage(x,y,i);
                addeage(y,x,i);
            }
            //求直径
            int s=1,e;
            ra_bfs(s,e); //s->e;
            ra_bfs(e,s);//e->s
            Top=0;
            int point=s;
            memset(check,false,sizeof(check));
            while(true)
            {
                dep[Top++] = point;
                check[point] = true;
                int k = pre[point];
                bir[a[k].pt] = false;
                if(!pre[point])
                {
                    break;
                }
                point=a[k].u;
            }
            //倒求
            DP(dp2);
            //正求
            for(int i=0;i<=Top-1;i++)
            {
                temp[i] = dep[Top-1-i];
            }
            for(int i=0;i<=Top-1;i++)
            {
                dep[i] = temp[i];
            }
            DP(dp1);
            //枚举结果
            int Min=INF,key;
            for(int i=1;i<=n-1;i++)
            {
                int com;
                if(bir[i])
                {
                    com = (Top-1)*(Map[i].w);
                }else
                {
                    int x = Map[i].u;
                    int y = Map[i].v;
                    if(level[x]<level[y])
                    {
                        com = max(dp1[x],dp2[y])*(Map[i].w);
                    }else
                    {
                        com = max(dp1[y],dp2[x])*(Map[i].w);
                    }
                }
                if(com<Min)
                {
                    Min = com;
                    key = i;
                }
            }
            printf("Case #%d: %d
    ",tem++,key);
        }
        return 0;
    }
    void addeage(int x,int y,int pos)
    {
        a[Top].u = x;
        a[Top].v = y;
        a[Top].next = b[x];
        a[Top].pt = pos;
        b[x] = Top++;
        bir[pos] = true;
    }
    void ra_bfs(int &s,int &e)
    {
        level[s] = 1;
        queue<int>que;
        memset(inque,false,sizeof(inque));
        que.push(s);
        inque[s] = true;
        pre[s] = 0;
        while(!que.empty())
        {
            int x = que.front();
            que.pop();
            for(int i=b[x];i!=-1;i=a[i].next)
            {
                int y = a[i].v;
                if(!inque[y])
                {
                    pre[y] = i;
                    level[y] = level[x]+1;
                    que.push(y);
                    inque[y] = true;
                }
            }
        }
        int Max=0;
        for(int i=1;i<=n;i++)
        {
            if(level[i]>Max)
            {
                Max = level[i];
                e = i;
            }
        }
    }
    void DP(int *p)
    {
        p[dep[0]] = 0;
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=Top-1;i++)
        {
            int x = dep[i];
            queue<int>que;
            que.push(x);
            sum[x] = 1;
            int Max = 1;
            while(!que.empty())
            {
                int x = que.front();
                que.pop();
                for(int i=b[x];i!=-1;i=a[i].next)
                {
                    int y = a[i].v;
                    if(sum[y]==0&&!check[y])
                    {
                        sum[y] = sum[x] + 1;
                        Max = max(sum[y],Max);
                        que.push(y);
                    }
                }
            }
            p[x] = max(p[dep[i-1]],i+Max-1);
        }
    }
    

     
  • 相关阅读:
    【阅读笔记】《C程序员 从校园到职场》第四章 变量和函数
    【阅读笔记】《C程序员 从校园到职场》第三章 程序的样式(大括号)
    睡眠呼吸机-相关检测算法的实现原理
    电磁学5.场能密度、电容与介电常数
    电磁学4.高压击穿
    电磁学3.静电屏蔽
    电磁学2.静电势能和电势
    电磁学1.高斯定律
    BLDC开发笔记8.过流保护与电流采样要点
    BLDC开发笔记7.转速和扭矩的影响因素和开关管的热分析概述
  • 原文地址:https://www.cnblogs.com/riskyer/p/3263073.html
Copyright © 2011-2022 走看看