hdu 4081 Qin Shi Huang's National Road System (次小生成樹的變形)

題目:Qin Shi Huang's National Road System

Qin Shi Huang's National Road System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2049    Accepted Submission(s): 746


Problem Description
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.
 

Input
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.
 

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
 

Source
 

Recommend
lcy

題意:秦始皇修路要把所有的城市都連通,每個城市有相應的人口數,每條路有相應的修路費。

          現在可以選一條magic路,修路費變爲0,以A代表magic 路兩端的人口數和。B代表總路費。

          選一條路作爲magic路,使A/B最大。         

分析:不能用貪心,因爲A與B相互制約。

          要使A/B最大,那麼B應該最小。故先求出n個點的最小生成樹。再枚舉

 每一條邊,假設最小生成樹的值是B, 而枚舉的那條邊長度是edge[i][j],  如果這一條邊已經

是屬於最小生成樹上的,那麼最終式子的值是A/(B-edge[i][j])。如果這一條不屬於最小生成

樹上的, 那麼添加上這條邊,就會有n條邊,那麼就會使得有了一個,爲了使得它還是一

個生成樹,就要刪掉環上的一條邊。 爲了讓生成樹儘量少,那麼就要刪掉除了加入的那條邊

以外,權值最大的那條路徑。 假設刪除的那個邊的權值是Max[i][j], 那麼就是A/(B-Max[i][j]).


即:如果把這條邊當作magic road的話,那麼這條邊以及連接u v 的mst的邊就組成了一個環了

當前這條邊的權值是最大的,要使剩下的路的花費最小,那麼肯定要把u v間的最長的一條邊給刪

去就行了,也就是找環中的第二大邊了。


代碼:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;

struct node
{
    double x,y;
    double peo;
}city[1010];
int vis[1010],mark[1010][1010],pre[1010];
double maps[1010][1010],dis[1010],maxedge[1010][1010];
int n;
double sum,ans;

double cal(int i,int j)
{
    double xx=(city[i].x-city[j].x)*(city[i].x-city[j].x);
    double yy=(city[i].y-city[j].y)*(city[i].y-city[j].y);
    return sqrt(xx+yy);
}
void prim()
{
    int i,j,v;
    double minc;
    sum=0;
    memset(maxedge,0,sizeof(maxedge));
    memset(pre,0,sizeof(pre));
    //dis[1]=INF;
    for(i=2;i<=n;i++)
    {
        dis[i]=maps[1][i];
        pre[i]=1;
    }
    vis[1]=1;
    for(i=1;i<n;i++)
    {
        minc=INF;
        v=1;
        for(j=1;j<=n;j++)
        {
            if(!vis[j] && dis[j]<minc)
            {
                 minc=dis[j];
                 v=j;
            }
        }
        sum+=minc;
        mark[pre[v]][v]=mark[v][pre[v]]=1;
        vis[v]=1;
        for(j=1;j<=n;j++)
        {
            if(vis[j] && j!=v)
            {
                maxedge[j][v]=maxedge[v][j]=max(dis[v],maxedge[pre[v]][j]);
            }
            if(!vis[j] && maps[v][j]<dis[j])
            {
                dis[j]=maps[v][j];
                pre[j]=v;
            }
        }
    }
}
int main()
{
    int T,i,j;
    scanf("%d",&T);
    while(T--)
    {
        memset(mark,0,sizeof(mark));
        memset(vis,0,sizeof(vis));
        memset(dis,0,sizeof(dis));
        memset(city,0,sizeof(city));
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf",&city[i].x,&city[i].y,&city[i].peo);
            for(j=1;j<i;j++)
                maps[i][j]=maps[j][i]=cal(i,j);
        }
        prim();
        ans=-1;
        for(i=1;i<=n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                if(!mark[i][j])
                    ans=max(ans,(city[i].peo+city[j].peo)/(sum-maxedge[i][j]));
                else
                    ans=max(ans,(city[i].peo+city[j].peo)/(sum-maps[i][j]));
            }
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}

感想:maxedge[j][v]=maxedge[v][j]=max(dis[v],maxedge[pre[v]][j]);

這一句開始寫成maxedge[j][v]=maxedge[v][j]=max(dis[v],maps[pre[v]][j]);

wa了好多好多次。。。。T_T....


發佈了96 篇原創文章 · 獲贊 13 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章