HDU 4081 Qin Shi Huang's National Road System
步骤是先求最小生成树,然后选两个不同的点,遍历所有的这样的点,选出两点人口比较大,而且连通两点的边的最大边比较大的情况。
因此要对i,j点连接起来的边进行遍历。
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 1010
#define max(a,b) ((a)>(b)?(a):(b))
#define INF 999999999 double map[N][N],lowcost[N],cost[N][N];
int vis[N];/*-1表示该点已经加入,非0表示i和vis[i]的值表示的点构成一条短边*/
int n;
typedef struct
{
double x,y,w;
} Point;
Point point[N]; double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} void input(void)
{
int i,j;
scanf("%d",&n);
for(i=; i<n; i++)
scanf("%lf%lf%lf",&point[i].x,&point[i].y,&point[i].w);
for(i=; i<n-; i++)
for(j=i+; j<n; j++)
map[i][j]=map[j][i]=dis(point[i],point[j]);
} double prim(void)
{
memset(cost,,sizeof(cost));
memset(vis,,sizeof(vis));
double ans=;
int e=,i,j;/*e=0表示首先将点0加入集合U*/
vis[]=-;/*e用来记录新加入的顶点*/
for(i=; i<n; i++)
lowcost[i]=1.0*INF;
for(i=; i<n-; i++)
{
double micost=1.0*INF;
int miedge=-;
for(j=; j<n; j++)
if(vis[j]!=-)
{
double temp=map[e][j];
if(temp<lowcost[j])
{
lowcost[j]=temp;
vis[j]=e;
}
if(lowcost[j]<micost)
micost=lowcost[miedge=j];
}
ans+=micost;
cost[miedge][vis[miedge]]=cost[vis[miedge]][miedge]=map[vis[miedge]][miedge];
for(j=; j<n; j++)
if(vis[j]==-)
cost[j][miedge]=cost[miedge][j]=max(cost[miedge][vis[miedge]],cost[j][vis[miedge]]);
vis[e=miedge]=-;
}
return ans;
} void solve(void)
{
int i,j;
double mst=prim();
/* printf("%f\n",mst);*/
double ans=-;
for(i=; i<n-; i++)
for(j=i+; j<n; j++)
{
double t=(point[i].w+point[j].w)/(mst-cost[i][j]);
ans=max(ans,t);
}
printf("%.2f\n",ans);
} int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
input();
solve();
}
return ;
}
HDU 4081 Qin Shi Huang's National Road System的更多相关文章
- HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: ...
- 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 ...
- 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 ...
- 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——————【次小生成树、prim】
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 树的基本性质 or 次小生成树思想 难度:1
During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in Ch ...
- HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...
- hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...
- HDU 4081 Qin Shi Huang's National Road System [次小生成树]
题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...
- hdu 4081 Qin Shi Huang's National Road System(最小生成树+dp)2011 Asia Beijing Regional Contest
同样是看别人题解才明白的 题目大意—— 话说秦始皇统一六国之后,打算修路.他要用n-1条路,将n个城市连接起来,并且使这n-1条路的距离之和最短.最小生成树是不是?不对,还有呢.接着,一个自称徐福的游 ...
随机推荐
- break continue return 区别
break语句: break语句会使运行的程序立刻退出包含在最内层的循环或者退出一个switch语句.由于它是用来退出循环或者switch语句,所以只有当它出现在这些语句时,这种形式的break语句才 ...
- Redis 集群常见问题
Redis集群相关问题 1:远程连接问题 远程连接保护模式下,需要做一些配置.
- c# winform实现网页上用户自动登陆,模拟网站登录
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- java- 枚举的常见用法
用法一:常量 public enum MyColor{Red,Black,Blue} public enum Color { RED, GREEN, BLANK, YELLOW } enum为枚举类的 ...
- c#汉字与编码之间的转换(输出十六进制)
/******************************************************************/ /*********************** ****** ...
- 类库探源——System.Drawing.Bitmap
一.System.Drawing.Bitmap Bitmap 类: 封装GDI+ 位图,此位图由图形图像及其属性的像素数据组成.Bitmap 是用于处理由像素定义的图像的对象 命名空间: System ...
- C++动态分配内存
动态分配(Dynamic Memory)内存是指在程序运行时(runtime)根据用户输入的需要来分配相应的内存空间. 1.内存分配操作符new 和 new[] Example: (1)给单个元素动态 ...
- Universal Naming Convention (UNC)
Quote from: http://compnetworking.about.com/od/windowsnetworking/g/unc-name.htm Definition: UNC is a ...
- Hack--兼容性测试
CSS hack由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效 ...
- 对BFC的理解
转载http://www.cnblogs.com/lhb25/p/inside-block-formatting-ontext.html 在解释 BFC 是什么之前,需要先介绍 Box.Formatt ...