zoukankan      html  css  js  c++  java
  • hdu 4081 Qin Shi Huang's National Road System

    During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China ---- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally conquered all six other kingdoms and became the first emperor of a unified China in 221 BC. That was Qin dynasty ---- the first imperial dynasty of China(not to be confused with the Qing Dynasty, the last dynasty of China). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the first emperor" in Chinese. 

    Qin Shi Huang undertook gigantic projects, including the first version of the Great Wall of China, the now famous city-sized mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a story about the road system: 
    There were n cities in China and Qin Shi Huang wanted them all be connected by n-1 roads, in order that he could go to every city from the capital city Xianyang. 
    Although Qin Shi Huang was a tyrant, he wanted the total length of all roads to be minimum,so that the road system may not cost too many people's life. A daoshi (some kind of monk) named Xu Fu told Qin Shi Huang that he could build a road by magic and that magic road would cost no money and no labor. But Xu Fu could only build ONE magic road for Qin Shi Huang. So Qin Shi Huang had to decide where to build the magic road. Qin Shi Huang wanted the total length of all none magic roads to be as small as possible, but Xu Fu wanted the magic road to benefit as many people as possible ---- So Qin Shi Huang decided that the value of A/B (the ratio of A to B) must be the maximum, which A is the total population of the two cites connected by the magic road, and B is the total length of none magic roads. 
    Would you help Qin Shi Huang? 
    A city can be considered as a point, and a road can be considered as a line segment connecting two points.
     

    输入

    The first line contains an integer t meaning that there are t test cases(t <= 10). 
    For each test case: 
    The first line is an integer n meaning that there are n cities(2 < n <= 1000). 
    Then n lines follow. Each line contains three integers X, Y and P ( 0 <= X, Y <= 1000, 0 < P < 100000). (X, Y) is the coordinate of a city and P is the population of that city. 
    It is guaranteed that each city has a distinct location.
     

    输出

    For each test case, print a line indicating the above mentioned maximum ratio A/B. The result should be rounded to 2 digits after decimal point.
     

    样例输入

    2
    4
    1 1 20
    1 2 30
    200 2 80
    200 1 100
    3
    1 1 20
    1 2 30
    2 2 40
     

    样例输出

    65.00
    70.00
    题意: 秦始皇建一些路要求代价最小 这些路中有一条特殊的路 为了使更多的人走这条路 就使得(A/B)的值最大 A 是2个城市的总人数  B是最小生成树中除了这2个城市的距离外的距离之和
     
    求出最小生成树  在这基础上求次小生成树
     
     为了使A/B最大,就应该是B越小,故可以先求出n个点的最小生成树。因此,可以枚举每一条边,假设最小生成树的值是B, 而枚举的那条边长度是edge[i][j],  如果这一条边已经是属于最小生成树上的,那么最终式子的值是A/(B-edge[i][j])。如果这一条不属于最小生成树上的, 那么添加上这条边,就会有n条边,那么就会使得有了一个,为了使得它还是一个生成树,就要删掉环上的一棵树。 为了让生成树尽量少,那么就要删掉除了加入的那条边以外,权值最大的那条路径。 假设删除的那个边的权值是Max[i][j], 那么就是A/(B-Max[i][j]).
    #include <cstdio>
    #include <queue>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int oo = 1e9+17;
    const int N = 1088;
    int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    struct da
    {
        int x, y, people;
    } as[N];
    double maps[N][N], dis[N], Max[N][N];
    int vis[N], n, pre[N], used[N][N];
    double getdist(int i, int j)
    {
        double a = (as[i].x-as[j].x)*1.0;
        double b = (as[i].y-as[j].y)*1.0;
        return sqrt(a*a + b*b);
    }
    void prime(double &answer)
    {
        int i, j, index;
        double mini, ans=0;
        for(i = 1; i <= n; i++)
        {
            dis[i] = maps[1][i];
            pre[i] = 1;
        }
        memset(used, 0, sizeof(used));
        memset(vis, 0, sizeof(vis));
        memset(Max, 0, sizeof(Max));
        vis[1] = 1;
        for(i = 1; i < n; i++)
        {
            mini = oo;
            index = 0;
            for(j = 1; j <= n; j++)
            {
                if(!vis[j] && dis[j] < mini)
                {
                    mini = dis[j];
                    index = j;
                }
            }
            vis[index] = 1;
            ans += mini;
            used[pre[index]][index] = used[index][pre[index]] = 1;
            for(j = 1; j <= n; j++)
            {
                if(vis[j] && index != j)
                    Max[index][j] = Max[j][index] = max(dis[index], Max[pre[index]][j]);
                if(!vis[j] && dis[j] > maps[index][j])
                {
                    dis[j] = maps[index][j];
                    pre[j] = index;
                }
            }
        }
        double op;
    
        for(i = 1; i <= n; i++)
        {
            for(j = 1+i; j <= n; j++)
            {
    
                if(used[i][j])
                {
                    op = (as[i].people + as[j].people)*1.0/(ans - maps[i][j]);
                    answer = max(answer, op);
                }
                else
                {
                    op = (as[i].people + as[j].people)*1.0/(ans - Max[i][j]);
                    answer = max(answer, op);
                }
    
            }
        }
    }
    int main()
    {
        int i, j, T;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d", &n);
            for(i = 1; i <= n; i++)
                scanf("%d %d %d", &as[i].x, &as[i].y, &as[i].people);
            for(i = 1; i <= n; i++)
            {
                maps[i][i] = 0;
                for(j = i+1; j <= n; j++)
                {
                    maps[j][i] = maps[i][j] = getdist(i, j);
                }
            }
            double answer = -1;
            prime(answer);
            printf("%.2lf
    ", answer);
        }
        return 0;
    }
    

      

  • 相关阅读:
    esper(4-5)- Context 条件
    esper(4-4)-OverLapping Context
    esper(4-3)-Non-Overlapping Context
    esper(4-2)-Category Context
    esper(4-1)-简单context
    esper(3)-窗口&聚合分组
    esper(2)-事件类型
    java常用代码
    idea(3)-jetty配置
    BP(反向传播)算法原理及推导
  • 原文地址:https://www.cnblogs.com/PersistFaith/p/4850366.html
Copyright © 2011-2022 走看看