hdu4081 Qin Shi Huang's National Road System PRIM+dp

8 篇文章 0 订阅

代码1:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
const int N = 1000 + 10;
const double INF = 1.0*1e8;
struct point
{
    double x,y;
    double val;
}p[N];
double get_dis(point a,point b)
{
    return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
}
vector<int> ed[N];
double map[N][N];
int n,pre[N];
double dis[N];
double sum;
void prim()
{
    int now=1;
    for(int i=1;i<=n;i++) ed[i].clear();
    for(int i=1;i<=n;i++) dis[i] = 1.0*INF;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++) if(now!=j && dis[j]>0 && map[now][j]>=0 && dis[j]>map[now][j]){
            dis[j] = map[now][j];
            pre[j] = now;
        }
        dis[now] = -1.0;
        int min_p=-1;
        double min_dis=1.0*INF;
        for(int j=1;j<=n;j++) if(dis[j]>0){
            if(dis[j] < min_dis)
            {
                min_p = j;
                min_dis = dis[j];
            }
        }
        if(min_p < 0) break;
        sum += min_dis;
        ed[min_p].push_back(pre[min_p]);
        ed[pre[min_p]].push_back(min_p);
        now = min_p;
    }
}
double dfs(int x,int fa)
{
    double max_val=p[x].val;
    for(int i=0;i<ed[x].size();i++)
    {
        int j = ed[x][i];
        if(j!=fa)
        {
            max_val = max( max_val,dfs(j,x) );
        }
    }
    return max_val;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        sum = 0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].val);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j)
                    map[i][j] = get_dis(p[i],p[j]);
                else
                    map[i][j] = 0.0;
        prim();
        double ans = 0;
        for(int i=1;i<=n;i++)
            for(int j=0;j<ed[i].size();j++)
            {
                int k = ed[i][j];
                double s1 = dfs(i,k);
                double s2 = dfs(k,i);
                ans = max(ans, (s1+s2)/(sum - map[i][k]));
            }
        ans += 1e-8;
        printf("%.2lf\n",ans);
    }
    return 0;
}

代码2:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int N = 1000 + 10;
const double INF = 1.0*1e8;
struct point
{
    double x,y;
    double val;
}p[N];
double get_dis(point a,point b)
{
    return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
}
vector<int> ed[N];
double map[N][N];
int n,pre[N],vis[N];
double dis[N],maxcost[N][N];
double sum;
void prim()
{
    int now=1;
    for(int i=1;i<=n;i++) ed[i].clear();
    for(int i=1;i<=n;i++) dis[i] = 1.0*INF;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++) if(now!=j && dis[j]>0 && map[now][j]>=0 && dis[j]>map[now][j]){
            dis[j] = map[now][j];
            pre[j] = now;
        }
        dis[now] = -1.0;
        int min_p=-1;
        double min_dis=1.0*INF;
        for(int j=1;j<=n;j++) if(dis[j]>0){
            if(dis[j] < min_dis)
            {
                min_p = j;
                min_dis = dis[j];
            }
        }
        if(min_p < 0) break;
        sum += min_dis;
        ed[min_p].push_back(pre[min_p]);
        ed[pre[min_p]].push_back(min_p);
        now = min_p;
    }
}
void dfs(int s,int x)
{
    for(int i=0;i<ed[x].size();i++){
        int v = ed[x][i];
        //printf("%d->%d\n",x,v);
        if(vis[v]) continue;
        vis[v] = 1;
        maxcost[s][v] = max(maxcost[s][v] , max(map[x][v],maxcost[s][x]));
        dfs(s,v);
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        sum = 0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].val);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j)
                    map[i][j] = get_dis(p[i],p[j]);
                else
                    map[i][j] = 0.0;
        prim();
        double ans = 0;
        //cout<<"hehe\n";
        for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) maxcost[i][j] = 0;
        for(int i=1;i<=n;i++){
            memset(vis,0,sizeof(vis));
            vis[i] = 1;
            maxcost[i][i] = 0;
            dfs(i,i);
        }
        //printf("orz\n");
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
            {
                ans = max(ans, (p[i].val+p[j].val)/(sum-maxcost[i][j])  );
            }
        ans += 1e-8;
        printf("%.2lf\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值