zoukankan      html  css  js  c++  java
  • uva 1494

    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.

    epsfbox{p5713.eps}

    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.

    Input 

    The first line contains an integer t meaning that there are t test cases (t$ le$10).

    For each test case:

    The first line is an integer n meaning that there are n cities (2 < n$ le$1000).

    Then n lines follow. Each line contains three integers X, Y and P (0$ le$X, Y$ le$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.

    Output 

    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.

    Sample Input 

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

    Sample Output 

    65.00
    70.00

    方法是枚举每对端点的边,求出不用该边的最小生成树,然后ans = MAX(ans, A/B),怎么快速求最小生成树权B呢?类似于次小生成树,由回路性质,先求出当前最小生成树,然后(n^2)dfs记录各点u,v间经过边的最大权值,
    这样可保证得到的A/B最小,因为对于所有的(u,v)求出了对应的最小的B,看书后的AC代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<set>
    #include<queue>
    #include<string>
    #include<cmath>
    #include<fstream>
    #include<iomanip>
    #include<climits>
    #include<cfloat>
    
    using namespace std;
    
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define ULL unsigned long long
    #define LL long long
    #define MAX(x,y) ((x) > (y) ? (x) : (y))
    #define MIN(x,y) ((x) < (y) ? (x) : (y))
    
    #define MAXN 1111
    #define MAXM 1111111
    int u[MAXM],v[MAXM];
    int p[MAXN];
    int n,x[MAXN],y[MAXN];
    int e,fa[MAXN],r[MAXM];
    double maxVal[MAXN][MAXN];
    double w[MAXM];
    
    bool cmp(const int a, const int b){
        return w[a]<w[b];
    }
    
    int finds(int x){
        if(fa[x] == x) return x;
        else return fa[x]=finds(fa[x]);
    }
    vector<int> g[MAXN],nodes;
    vector<double> c[MAXN];
    
    double mst(){
        int i,j;
        for(e=i=0; i<n; i++)
            for(j=i+1; j<n; j++){
                u[e]=i;     v[e]=j;
                w[e]=sqrt((x[i]-x[j])*(x[i]-x[j])+
                        (y[i]-y[j])*(y[i]-y[j]));
                e++;
            }
        for(i=0; i<e; i++) r[i]=i;
        for(i=0; i<n; i++){
            fa[i]=i;
            g[i].clear();   c[i].clear();
        }
        sort(r,r+e,cmp);
        double ans=0;
        for(j=i=0; i<e; i++){
            int tx=finds(u[r[i]]), ty=finds(v[r[i]]);
            if(tx!=ty){
                g[tx].push_back(ty);        g[ty].push_back(tx);
                c[tx].push_back(w[r[i]]);   c[ty].push_back(w[r[i]]);
                ans+=w[r[i]];
                fa[tx]=ty;
                if(++j==n-1) return ans;
            }
        }
        return -1.0;
    }
    
    typedef vector<int>::iterator itr;
                            //无根树转有根树,记录路径上最大边权
    void dfs(int u_, int v_, double w_){
        for(int i=0; i<nodes.size(); i++)
            maxVal[u_][nodes[i]]=maxVal[nodes[i]][u_] = MAX(maxVal[nodes[i]][v_], w_);
        nodes.push_back(u_);
        for(int i=0; i<g[u_].size(); i++)
            if(g[u_][i] != v_) dfs(g[u_][i], u_, c[u_][i]);
    }
    
    int main(){
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int t;
        scanf(" %d",&t);
        while(t--){
            int i,j;
            scanf(" %d",&n);
            for(i=0; i<n; i++){
                scanf(" %d %d %d",&x[i],&y[i],&p[i]);
            }
            double mv=mst(),ans=0;
            memset(maxVal, 0, sizeof(maxVal));
            nodes.clear();
            dfs(0, -1, 0);
    
            for(i=0; i<n; i++)
                for(j=i+1; j<n; j++)
                    ans=MAX(ans, (p[i]+p[j])/(mv-maxVal[i][j]));
            printf("%.2f
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    FOC中的Clarke变换和Park变换详解(动图+推导+仿真+附件代码)
    有感FOC算法学习与实现总结
    永磁同步电机 spmsm 和 ipmsm 的区别总结
    Jekyll 解决Jekyll server本地预览文章not found的问题
    STM32 TIM 多通道互补PWM波形输出配置快速入门
    STM32 TIM1高级定时器配置快速入门
    STM32 ADC多通道规则采样和注入采样
    STM32 时钟树配置快速入门
    STM32 TIM 编码器模式采集编码器信号
    STM32 标准库V3.5启动文件startup_stm32f10xxx.s分析
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3267064.html
Copyright © 2011-2022 走看看