hdu4081(最小生成树变形,prim)

Qin Shi Huang's National Road System

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


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

题意:有n个点,现在要求出一个生成树,每个点都有权值,现在可以让生成树里面的一条边的花费为0,不过要让这条边连接的两点的权值尽可能的大,让A/B的结果为最大,A表示这条边连接的两点的权值之和,B表示生成树所有边的权值之和减去A的这条边。

思路:遍历所有的边,不过遍历是建立在先建出最小生成树的基础上,也就是说先建出最小生成树,如果两个点的边在最小生成树上就直接ans=max(ans,(a[i].z+a[j].z)/(sum-tu[i][j]));如果不在最小生成树上,那么假设我们有这条边,那么必然在已经建好的最小生成树上有一条边要去掉,我们取最优解去掉连接该边成环之后的最大权值边(不包括添加的这条边),那么ans=max(ans,(a[i].z+a[j].z)/(sum-len[i][j])), len[i][j]数组存的就是连接i和j使最小生成树成环之后的环上的除ij之外的最大权值边。


注意:这题特别无奈的是刚开始用的kruskal算法,百度了一下kruskal与prim的时间差别,稠密prim比kcuskal快的不止一下,稀疏图kruskal更快。结果刚开始死活tle,心有不甘,若有人有优化办法望告知博主。


下面是tle代码(kruskal):

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1010;
double tu[N][N],len[N][N];
bool vis[N][N];
vector <int>g[N];
int n,f[N];
struct data
{
    int x,y,p;
}a[N];
struct hh
{
    int a,b;
    double w;
}p[N*N/2];
inline double dis(int i,int j)
{
    return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
}
bool cmp(hh a,hh b)
{
    return a.w<b.w;
}
int fa(int x)
{
    if(x!=f[x])
    return f[x]=fa(f[x]);
    return f[x];
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(tu,0,sizeof(tu));
         memset(len,0,sizeof(len));
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].p),f[i]=i;
        int bj=0;
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
            tu[i][j]=tu[j][i]=dis(i,j),p[bj].a=i,p[bj].b=j,p[bj++].w=tu[i][j];
        int k=0;
        sort(p,p+bj,cmp);
        double sum=0;
        for(int i=0;i<n;i++)
        g[i].push_back(i);
        for(int i=0;i<bj;i++)
        {
            if(k==n-1)break;
            int x1=fa(p[i].a),x2=fa(p[i].b);
            if(x1!=x2)
            {
                vis[p[i].a][p[i].b]=vis[p[i].b][p[i].a]=1;
                sum+=tu[p[i].a][p[i].b];
                int l=g[x1].size(),ll=g[x2].size();
                for(int j=0;j<l;j++)
                    for(int k=0;k<ll;k++)
                        len[g[x1][j]][g[x2][k]]=len[g[x2][k]][g[x1][j]]=tu[p[i].a][p[i].b];
                f[x1]=x2;
                int l1=g[x1].size();
                for(int j=0;j<l1;j++)
                    g[x2].push_back(g[x1][j]);
                g[x1].clear();
            }
        }
        double ans=-1;
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                if(vis[i][j])
                    ans=max(ans,(double)(a[i].p+a[j].p)/(sum-tu[i][j]));
            else
                    ans=max(ans,(double)(a[i].p+a[j].p)/(sum-len[i][j]));
       printf("%.2f\n",ans);
    }
    return 0;
}




ac代码(prim):

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1010;
const int maxn=1010;
const double inf=1e14;

struct note
{
    int x,y,z;
}a[maxn];

double tu[maxn][maxn],dis[maxn];
int pre[maxn],n;

int flag[maxn][maxn],vis[maxn];
double len[maxn][maxn];

double prim(int u)
{
    double sum=0;
    memset(flag,0,sizeof(flag));
    memset(vis,0,sizeof(vis));
    memset(len,0,sizeof(len));
    for(int i=1; i<=n; i++)
    {
        dis[i]=tu[u][i];
        pre[i]=u;
    }
    vis[u]=1;
    for(int i=1; i<n; i++)
    {
        double minn=inf;
        int v=-1;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]<minn)
            {
                v=j;
                minn=dis[j];
            }
        }
        if(v!=-1)
        {
            sum+=dis[v];
            flag[v][pre[v]]=flag[pre[v]][v]=1;
            vis[v]=1;
            for(int k=1; k<=n; k++)
            {
                if(vis[k]&&k!=v)
                {
                    len[v][k]=len[k][v]=max(len[k][pre[v]],dis[v]);
                }
                if(!vis[k]&&tu[v][k]<dis[k])
                {
                    dis[k]=tu[v][k];
                    pre[k]=v;
                }
            }
        }
    }
    return sum;
}

double lenth(int i,int j)
{
    return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
        for(int i=1; i<= n; i++)
            for(int j=i+1; j<=n; j++)
                tu[i][j]=tu[j][i]=lenth(i,j);
        double sum=prim(1);
        double ans=-1;
        for(int i=1; i<=n; i++)
            for(int j=i+1; j<=n; j++)
                if(flag[i][j])
                    ans=max(ans,(a[i].z+a[j].z)/(sum-tu[i][j]));
                else
                    ans=max(ans,(a[i].z+a[j].z)/(sum-len[i][j]));

        printf("%.2lf\n",ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/martinue/p/5490437.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值