HDU_4081

这个题目大体的思路是这个样子的,首先求一个最小生成树,然后求出或者顺便求出最小生成树上任意两点间路径上的最大边权,之后枚举magic road,把magic road两个端点间的之前记录的最大边边权减去后作为B,把magic road两个端点上的人口和作为A,更新一下A/B即可。

#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#define MAXD 1010#define MAXM 1000010int N, x[MAXD], y[MAXD], people[MAXD], a[MAXM], b[MAXM], M, e, r[MAXM];int first[MAXD], next[2 * MAXD], v[2 * MAXD], vis[MAXD], p[MAXD];double w[MAXM], maxlen[MAXD][MAXD], total, len[2 * MAXD];int cmp(const void *_p, const void *_q){int *p = (int *)_p;int *q = (int *)_q;return w[*p] < w[*q] ? -1 : 1;}int find(int x){return p[x] == x ? x : (p[x] = find(p[x]));}double distance(int i, int j){return sqrt((double)(x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));}void init(){int i, j;    scanf("%d", &N);for(i = 0; i < N; i ++)        scanf("%d%d%d", &x[i], &y[i], &people[i]);    M = 0;for(i = 0; i < N; i ++)for(j = i + 1; j < N; j ++)        {            a[M] = i;            b[M] = j;            w[M] = distance(i, j);            M ++;        }}void add(int x, int y, double z){    v[e] = y;    len[e] = z;    next[e] = first[x];    first[x] = e;    e ++;}void dfs(int fa, int cur, double max){int i;    vis[cur] = 1;for(i = first[cur]; i != -1; i = next[i])if(!vis[v[i]])        {if(len[i] > max)            {                maxlen[fa][v[i]] = len[i];                dfs(fa, v[i], len[i]);            }else            {                maxlen[fa][v[i]] = max;                dfs(fa, v[i], max);            }        }}void prepare(){int i, j, k, tx, ty;for(i = 0; i < M; i ++)        r[i] = i;    qsort(r, M, sizeof(r[0]), cmp);for(i = 0; i < N; i ++)        p[i] = i;    e = 0;    memset(first, -1, sizeof(first));    total = 0;for(i = 0; i < M; i ++)    {        k = r[i];        tx = find(a[k]);        ty = find(b[k]);if(tx != ty)        {            total += w[k];            p[tx] = ty;            add(a[k], b[k], w[k]);            add(b[k], a[k], w[k]);        }    }for(i = 0; i < N; i ++)    {        memset(vis, 0, sizeof(vis));        dfs(i, i, 0);    }}void solve(){int i, j;double temp, res = 0;for(i = 0; i < N; i ++)for(j = i + 1; j < N; j ++)        {            temp = (double)(people[i] + people[j]) / (total - maxlen[i][j]);if(temp > res)                res = temp;        }    printf("%.2f\n", res);}int main(){int t;    scanf("%d", &t);while(t --)    {        init();        prepare();        solve();    }return 0;}

HDU 4081 Qin Shi Huang's National Road System相关推荐

  1. HDU - 4081 Qin Shi Huang‘s National Road System(次小生成树)

    点击打开题目链接 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  2. 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 M ...

  3. HDU 4081 Qin Shi Huang's National Road System (次小生成树算法)

    转载自http://blog.csdn.net/shuangde800 D_Double 题目: Problem Description During the Warring States Perio ...

  4. HDU - 4081 Qin Shi Huang's National Road System(次小生成树)

    题目大意:有N个城市,现在要修建一些道路使得这些城市能互相连通,修建一条道路的权值为这两个城市之间的欧几里德距离. 现在你可以选择一条路作为特殊道路,这条道路的权值为0 假设修建道路的总权值为B,用特 ...

  5. HDU 4081 Qin Shi Huang‘s National Road System(枚举+倍增优化)

    题目链接 题目链接:给你n个点,每个点都有权值,可以免费连接两个点,然后再选择n-2条边构成一个生成树,使得免费连接的两个点的权值和/n-2条边的长度和比值最大.输出这个最大值. 分析:很容易可以想到 ...

  6. Qin Shi Huang's National Road System HDU - 4081

    Qin Shi Huang's National Road System (HDU - 4081) 次小生成树的思想 题目描述 During the Warring States Period of ...

  7. HDU4081:Qin Shi Huang's National Road System (任意两点间的最小瓶颈路)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  8. hdu-4081 Qin Shi Huang's National Road System(次小生成树)

    题目链接:点击打开链接 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  9. Qin Shi Huang's National Road System( 次小生成树 )

    Qin Shi Huang's National Road System( 次小生成树 ) During the Warring States Period of ancient China(476 ...

最新文章

  1. mysql四维数组_MySQL如何实现数组功能
  2. java sql 返回 json_java访问oracle 返回json 实践(一)
  3. 学习IOS开问题篇--视图的模型控件属性写在私有分类中的原因
  4. 复制公钥到多个服务器脚本
  5. Cpp 11 / 万能引用、引用折叠和完美转发
  6. 8个前沿的 HTML5 CSS3 效果【附源码下载】
  7. mysql 双从性能_MySQL双主一致性架构优化
  8. php curl跨域cookie_php使用curl带cookie访问一直失败求助
  9. PHP通过OpenSSL生成证书、密钥并且加密解密数据,以及公钥,私钥和数字签名的理解...
  10. java求两个数组的并集、交集、差集
  11. python求平均值函数_Python两个练习题,写出私信有奖
  12. 干货 | attention超全综述
  13. bi报表工具应该具备哪些功能
  14. 无数个骂娘的故事告诉你,千万别做技术合伙人
  15. 一步一步制作最简单的动画
  16. Nexus的权限管理及分配
  17. 陌生人邀请我加入CS:GO游戏,我一接受就被盗号了
  18. 如何使用COM-Hunter检测持久化COM劫持漏洞
  19. Redis网站热搜关键词加载实践,建议收藏
  20. 【react native】Flatlist实现上拉滚动加载

热门文章

  1. python时间str转datetime_深入了解Python中的小知识
  2. python输入数据的维度_python – Keras LSTM输入维度设置
  3. python getcwd 与dirname_Python中获取路径os.getcwd()和os.path.dirname(os.path.realpath(__file__))的区别和对比...
  4. python显示界面后1秒自动隐藏_使用Python+Qt时解决QTreeWidget中的内容超出边界后自动隐藏的问题...
  5. python里default_新手对python default不是很理解它有什么用途
  6. mysql update 顺序_MySQL的Update语句Set顺序问题
  7. R语言 朴素贝叶斯分类预测
  8. 教你玩转vue-router命令视图
  9. 扫地机器人湿地_仅仅是打湿地板而已?定价高了,石头扫地机器人的拖地却仍很一般...
  10. linux用户管理命令 2