hdu4081 Qin Shi Huang's National Road System

20 篇文章 0 订阅
4 篇文章 0 订阅

次小生成树的变形                   传送门

求出最小生成树,然后DFS把最小生成树变为有根数,同时计算任意两个节点间的瓶颈路maxcost[u][v](即u,v路径上的最长边),方法如下:

当访问至一个新节点u时,枚举所有已访问过的节点x,则maxcost[x][u] = max( maxcost[x][v], w[u][v]),其中v是u的父节点。时间复杂度为O(n2)


那么此题便可以枚举免费的那条路,若这条路在最小生成树上则B 为mst - G[u][v],否则  B = mst - maxcost[u][v],而A为常数,问题解决



//#pragma warning (disable: 4786)
//#pragma comment (linker, "/STACK:16777216")
//HEAD
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//STL
#define SZ(V) (int)V.size()
#define PB push_back
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
//OUTPUT
#define WI(n) printf("%d\n", n)
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const int INF = 100000000;
const double eps = 1e-10;
const int maxn = 1010;

int N, fa[maxn];
struct node{
    double x, y, p;
}city[maxn];
double g[maxn][maxn], dis[maxn], mst, cost[maxn][maxn];
bool vis[maxn], used[maxn][maxn];

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

void prim()
{
    CLR(vis, 0), CLR(used, 0);
    mst = 0;
    REP(i, N)
        dis[i] = INF, fa[i] = 0;
    CLR(cost, 0);
    fa[0] = -1;
    dis[0] = 0;
    double mi; int pos;
    REP(i, N)
    {
        mi = INF;
        REP(j, N)
            if (!vis[j] && dis[j] < mi)
            {
                mi = dis[j];
                pos = j;
            }
        mst += mi;
        if (fa[pos] != -1)
        {
            used[fa[pos]][pos] = used[pos][fa[pos]]= 1;
            REP(j, N)
            if (vis[j])
            {
                cost[j][pos] = max(cost[j][fa[pos]], g[fa[pos]][pos]);
                cost[pos][j] = cost[j][pos];
            }

        }
        vis[pos] = 1;
        REP(j, N)
        {
            if (!vis[j] && dis[j] > g[pos][j])
            {
                dis[j] = g[pos][j];
                fa[j] = pos;
            }
        }
    }
}

void solve()
{
    REP(i, N)
        FF(j, i, N)
            g[i][j] = g[j][i] = Dist(i, j);
    prim();
    double ans = 0.0;
    double a, b;
    REP(i, N)
        FF(j, i + 1,  N)
        {
            a = city[i].p + city[j].p;
            if (used[i][j])
                b = mst - g[i][j];
            else
                b = mst - cost[i][j];
            ans = max(ans, a / b);
        }
        printf("%.2lf\n", ans);
}

int main()
{
    int t;
    RI(t);
    while (t--)
    {
        RI(N);
        REP(i, N)
            scanf("%lf%lf%lf", &city[i].x, &city[i].y ,&city[i].p);
        solve();
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值