Qin Shi Huang's National Road System UVA - 1494(次小生成树)
秦始皇统一中国之后要在全国修公路连接各个城市,皇帝只想修成最小生成树(距离最小,不考虑人力),一个道士说自己可以不花人力物力修一条路,经过两方妥协,选择max(两个城市人口/(生成树长度-这条路的长度(连接两个城市的路中,权值最大的那段路)))的路让他变,求这个比值最大值。
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int n;
int pre[maxn], vis[maxn];
double graph[maxn][maxn], maxd[maxn][maxn], d[maxn];
struct node
{
int x, y, p;
}Node[maxn]; double prime(int s)
{
double sum = ;
mem(vis, );
mem(maxd, );
int temp;
rap(i, , n) d[i] = graph[s][i], pre[i] = s;
d[s] = ;
vis[s] = ;
rap(i, , n-)
{
double mincost = INF;
rap(j, , n)
if(!vis[j] && mincost > d[j])
{
temp = j;
mincost = d[j];
}
rap(j, , n)
if(vis[j] && j != temp) maxd[temp][j] = maxd[j][temp] = max(mincost, maxd[pre[temp]][j]);
sum += mincost;
vis[temp] = ;
// used[temp][pre[temp]] = used[pre[temp]][temp] = 1;
rap(j, , n)
{
if(!vis[j] && d[j] > graph[temp][j])
d[j] = graph[temp][j], pre[j] = temp;
}
}
return sum;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
//求次小生成树还是得需要这段代码,用来找到不在生成树里的边
// scanf("%d", &n);
// for(int i=1; i<=n; i++)
// for(int j=1; j<=n; j++)
// if(i == j) graph[i][j] = 0;
// else graph[i][j] = graph[j][i] = INF;
rap(i, , n)
{
scanf("%d%d%d", &Node[i].x, &Node[i].y, &Node[i].p);
}
rap(i, , n)
rap(j, i+, n)
graph[i][j] = graph[j][i] = sqrt((double)(Node[i].x - Node[j].x)*(Node[i].x - Node[j].x) + (double)(Node[i].y - Node[j].y)*(Node[i].y - Node[j].y));
double dis = prime();
double sum = -;
rap(i, , n)
rap(j, i+, n)
if(pre[i] == j || pre[j] == i) //对于当前结点i和j 如果i和j之间有直接边(在生成树中的) 则dis减直接边
sum = max(sum, 1.0*(Node[i].p + Node[j].p)/(dis - graph[i][j]));
else //没有直接边 则减i和j之间那条路中权值最大的那段路
sum = max(sum, 1.0*(Node[i].p + Node[j].p)/(dis - maxd[i][j]));
printf("%.2f\n", sum); } return ;
}
Qin Shi Huang's National Road System UVA - 1494(次小生成树)的更多相关文章
- 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: 32768/3 ...
- UValive 5713 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 (次小生成树的变形)
题目:Qin Shi Huang's National Road System Qin Shi Huang's National Road System Time Limit: 2000/1000 M ...
- 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: 32768/3 ...
- Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)
Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...
- [hdu P4081] Qin Shi Huang’s National Road System
[hdu P4081] Qin Shi Huang’s National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 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——————【次小生成树、prim】
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
随机推荐
- 1116: [POI2008]CLO
1116: [POI2008]CLO https://lydsy.com/JudgeOnline/problem.php?id=1116 分析: 单独考虑每个联通块的情况. 设这个联通块里有n个点,那 ...
- SRM 698 div1 RepeatString
250pts RepeatString 题意:问最少修改多少次将一个字符串修改为AA的形式.可以插入一个字符,删除一个字符,修改字符. 思路:枚举分界点,然后dp一下. /* * @Author: m ...
- SaltStack入门篇(五)之salt-ssh的使用以及LAMP状态设计部署
1.salt-ssh的使用 官方文档:https://docs.saltstack.com/en/2016.11/topics/ssh/index.html ()安装salt-ssh [root@li ...
- 查询系统日期和时间(mysql)
select current_date --不带时间select sysdate() 或 SELECT NOW(); --带时间
- golang 单元测试
单元测试是质量保证十分重要的一环,好的单元测试不仅能及时地发现问题,更能够方便地调试,提高生产效率.所以很多人认为写单元测试是需要额外的时间,会降低生产效率,是对单元测试最大的偏见和误解. go 语言 ...
- XAF-如何改变列表点击时的默认行为
在 Windows 窗体应用程序中,按下回车或双击列表会打开默认的详细视图. 在 ASP.NET Web 应用程序中,单击对象时执行此操作. 这一行为是由 ListViewProcessCurrent ...
- Python函数标注
Python函数标注 是关于用户自定义函数中使用的类型的完全可选元数据信息. 函数标注 以Python字典的形式存放在函数的 __annotations__ 属性中,并且不会影响函数的任何其他部分. ...
- Vue2 v-bind:href 中如何使用过滤器
<a class="topic_title" v-bind:href="info.id|getTitleHref" v-bind:title=" ...
- 测试类异常Manual close is not allowed over a Spring managed SqlSession
在用Spring 和mybatis整合的 写测试类的时候报出解决办法:在全局配置文件 class="org.mybatis.spring.SqlSessionTemplate" ...
- CSP201312-3:最大的矩形
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...