Hdu 4081 Qin Shi Huang's National Road System

Source: Internet
Author: User

Page 1 of the White Paper. Question: Qin Shihuang wants to build a road project to connect all cities. Then, the Taoist Xu Fu can help him build a path without labor. Two variables are given: Let the population at the beginning and end of the road divide A by the sum of labor in the road other than this B Qin Shihuang wants to make this road repair location, making A/B the biggest idea: use the Minimum Spanning Tree to first generate a full-pass graph and then use DFS to calculate the path of the maximum weight on the unique path between each city and then enumerate each city, SUM-maxcost [U] [V] calculated using the Minimum Spanning Tree

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <vector>#define INF 0x3f3f3f3f#define MAX(a,b) a>b?a:busing namespace std;struct point{    int x,y,p;}P[1005];int set[1005];struct line{    int s,e;    double w;    bool operator < (const line &cmp)const    {        return w<cmp.w;    }}L[1005*1005];double maxcost[1005][1005];double map[1005][1005];vector<int>minmap[1005];bool vis[1005];int find(int x){    while(set[x]!=x)    x=set[x];    return x;}double dis(point tx,point ty){    return sqrt((double)(tx.x-ty.x)*(tx.x-ty.x)+(double)(tx.y-ty.y)*(tx.y-ty.y));}void dfs(int st,int pos){    for(int i=0;i<minmap[pos].size();i++)    {        if(!vis[minmap[pos][i]])        {            vis[minmap[pos][i]]=true;            maxcost[st][minmap[pos][i]]=MAX(maxcost[st][pos],map[pos][minmap[pos][i]]);           // printf("maxcost[%d][%d]=%.4lf\n",st,minmap[pos][i],maxcost[st][minmap[pos][i]]);            dfs(st,minmap[pos][i]);        }    }}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n;        memset(maxcost,0,sizeof(maxcost));        scanf("%d",&n);        int top=0;        for(int i=1;i<=n;i++)        {            scanf("%d%d%d",&P[i].x,&P[i].y,&P[i].p);            for(int j=1;j<i;j++)            {                L[top].s=i;                L[top].e=j;                L[top].w=dis(P[i],P[j]);                map[i][j]=map[j][i]=L[top++].w;            }            minmap[i].clear();            set[i]=i;        }        sort(L,L+top);        double sum=0;        for(int i=0;i<top;i++)        {            int xx=find(L[i].s);            int yy=find(L[i].e);            if(xx!=yy)            {                set[xx]=yy;                sum+=L[i].w;                minmap[L[i].s].push_back(L[i].e);                minmap[L[i].e].push_back(L[i].s);            }        }        //printf("sum = %.4lf\n",sum);        for(int i=1;i<=n;i++)        {            memset(vis,false,sizeof(vis));            dfs(i,i);            maxcost[i][i]=0;        }        double ans=0;        for(int i=1;i<n;i++)        {            for(int j=i+1;j<=n;j++)            {                int tmp=P[i].p+P[j].p;                double tt=1.0*tmp/(sum-maxcost[i][j]);                if(tt>ans)ans=tt;            }        }        printf("%.2lf\n",ans);    }    return 0;}

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.