HDU4081 Qin Shi Huang's National Road System【prim最小生成树+枚举】
先求出最小生成树,然后枚举树上的边,对于每条边“分别”找出这条割边形成的两个块中点权最大的两个
1.因为结果是A/B。A的变化会引起B的变化,两个制约。无法直接贪心出最大的A/B。故要通过枚举
2.无论magic road要加在哪里。加的边是否是最小生成树上的边,都会产生环,我们都要选择一条边删掉
注意删掉的边必须是树的环上的边。为了使结果最大。即找出最大的边
3.能够枚举两点。找出边,也能够枚举边,找出点,我是用后者。感觉比較easy写也好理解
- #include <cstdio>
- #include <cstdlib>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <cstring>
- #include <cmath>
- using namespace std;
- const int INF=(1<<31)-1;
- int num;
- bool visit[1111];
- struct city{int x,y,popu;}point[1111];
- vector<int>G[1111];
- double getdis(city& a,city& b)
- {
- double dx=a.x-b.x;
- double dy=a.y-b.y;
- return sqrt(dx*dx+dy*dy);
- }
- double dis[1111][1111];
- double prim()
- {
- double sum=0;
- double dist[1111];
- fill(dist,dist+1111,INF*1.0);
- double minedge=INF;
- int now=1,min_p;
- int pre[1111];
- memset(pre,0,sizeof(pre));
- dist[now]=0;
- visit[1]=true;
- for(int t=1;t<num;t++)
- {
- for(int i=1;i<=num;i++)
- {
- if(i!=now && !visit[i] &&dist[i]>dis[now][i])
- {
- pre[i]=now;
- dist[i]=dis[now][i];
- }
- }
- minedge=INF;
- for(int i=1;i<=num;i++)
- {
- if(!visit[i]&&minedge>dist[i])
- {
- minedge=dist[i];
- min_p=i;
- }
- }
- G[pre[min_p]].push_back(min_p);
- G[min_p].push_back(pre[min_p]);
- sum+=dis[min_p][pre[min_p]];
- now=min_p;
- visit[now]=true;
- }
- return sum;
- }
- int dfs(int v,int fa)
- {
- int maxn=point[v].popu;
- for(int j=0;j<G[v].size();j++)
- {
- if(G[v][j]!=fa)
- {
- maxn=max(maxn,dfs(G[v][j],v));
- }
- }
- return maxn;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("G:/1.txt","r",stdin);
- freopen("G:/2.txt","w",stdout);
- #endif
- int T;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d",&num);
- for(int i=1;i<=num;i++)
- {
- scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].popu);
- }
- for(int i=1;i<=num;i++)
- {
- for(int j=1;j<=num;j++)
- {
- dis[i][j]=getdis(point[i],point[j]);
- }
- }
- double sum=prim();
- double ans=0;
- for(int i=1;i<=num;i++)
- {
- for(int j=0;j<G[i].size();j++)
- {
- int t1=dfs(i,G[i][j]);
- int t2=dfs(G[i][j],i);
- ans=max(ans,(t1+t2)*1.0/(sum-dis[i][G[i][j]]));
- }
- }
- //ans+=(1e-8);
- printf("%.2f\n",ans);
- for(int i=1;i<=num;i++)
- {
- G[i].clear();
- }
- memset(point,0,sizeof(city)*(num+1));
- memset(visit,0,sizeof(bool)*(num+1));
- }
- }
HDU4081 Qin Shi Huang's National Road System【prim最小生成树+枚举】的更多相关文章
- HDU 4081 Qin Shi Huang's National Road System 最小生成树
点击打开链接题目链接 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- HDU 4081 Qin Shi Huang's National Road System(最小生成树/次小生成树)
题目链接:传送门 题意: 有n坐城市,知道每坐城市的坐标和人口.如今要在全部城市之间修路,保证每一个城市都能相连,而且保证A/B 最大.全部路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i ...
- HDU4081 Qin Shi Huang's National Road System —— 次小生成树变形
题目链接:https://vjudge.net/problem/HDU-4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 ...
- HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏
Qin Shi Huang's National Road System ...
- 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 ...
- hdu-4081 Qin Shi Huang's National Road System(最小生成树+bfs)
题目链接: Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- HDU4081 Qin Shi Huang's National Road System(次小生成树)
枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...
- hdu4081 Qin Shi Huang's National Road System 次小生成树
先发发牢骚:图论500题上说这题是最小生成树+DFS,网上搜题解也有人这么做.但是其实就是次小生成树.次小生成树完全当模版题.其中有一个小细节没注意,导致我几个小时一直在找错.有了模版要会用模版,然后 ...
- HDU4081 Qin Shi Huang's National Road System
先求最小生成树 再遍历每一对顶点,如果该顶点之间的边属于最小生成树,则剪掉这对顶点在最小生成树里的最长路径 否则直接剪掉连接这对顶点的边~ 用prim算法求最小生成树最长路径的模板~ #include ...
随机推荐
- Python-搭建Nginx+Django环境
1.安装 flup 模块 下载:http://projects.unbit.it/downloads/uwsgi-latest.tar.gz 安装:python setup.py install 2. ...
- Cordova 快速入门记录
本篇文章由:http://xinpure.com/cordova-quick-start-recording/ 记一笔 Cordova 官网入门文档 Get Started Fast,言简意该.通俗易 ...
- mysql索引二
理解MySQL——索引与优化 写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优 的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储1 ...
- php连接sql2005
连接前配置系统: 1.检查文件 php5.2.5/ntwdblib.dll 默认下面有一个,不能连接再替换. 下载正确版本的 ntwdblib.dll (2000.80.194.0),地址: http ...
- Python 列表 list() 方法
描述 Python 列表 list() 方法用于将可迭代对象(字符串.列表.元祖.字典)转换为列表. 注:元组与列表是非常类似的,区别在于元组的元素值不能修改,元组是放在括号中,列表是放于方括号中. ...
- 日期常用操作类DateUtil
一.给定yyyy-MM-dd hh:mm:ss格式的字符串,返回Date. public Date convertStr2Date(String dateString) { try { SimpleD ...
- opensips编译安装时可能遇到的问题
错误一: ERROR: could not load the script in /usr/local//lib64/opensips/opensipsctl/opensipsdbctl.pgsql ...
- c#(winform)中自定义ListItem类方便ComboBox添加Item项
1.定义ListItem类 public class ListItem { private string _key = string.Empty; private string _value = st ...
- Node Redis 小试
Redis 是一个高性能的 key-value 数据库,为了保证效率,数据都是缓存在内存中,在执行频繁而又复杂的数据库查询条件时,可以使用 Redis 缓存一份查询结果,以提升应用性能. 背景 如果一 ...
- 多国语言解决方案gnu.gettext + poedit
1.工具简介 1.1.关于i18n i18n其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数是“国际化”的简称. i10n为资源本地化,全称为Locali ...