prim,把每个墙看成一个节点,从起点用prim求最小生成树,直到覆盖到终点为止,输出最小生成树中的最大边

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std; #define MAX_WALL_NUM 1005
#define INF 0x3f3f3f3f struct Wall
{
int s, e, pos;
bool h; //direction: is horizental
}wall[MAX_WALL_NUM]; int wall_num;
double graph[MAX_WALL_NUM][MAX_WALL_NUM]; void input()
{
for (int i = ; i < wall_num; i++)
{
int x, y, l;
scanf("%d%d%d", &x, &y, &l);
if (l < )
{
wall[i].h = false;
swap(x, y);
}else
wall[i].h = true;
wall[i].s = x;
wall[i].e = x + abs(l);
wall[i].pos = y;
}
} bool overlap(Wall a, Wall b)
{
if (a.s > b.e || a.e < b.s)
return false;
return true;
} double cal(Wall a, Wall b)
{
if (a.h == b.h)
{
int dist1 = abs(a.pos - b.pos);
if (overlap(a, b))
return dist1;
else
{
int dist2 = min(abs(a.s - b.e), abs(a.e - b.s));
return sqrt(dist1 * dist1 + dist2 * dist2);
}
}
if (a.s <= b.pos && a.e >= b.pos && b.s <= a.pos && b.e >= a.pos)
return ;
if (a.s <= b.pos && a.e >= b.pos)
return min(abs(b.s - a.pos), abs(b.e - a.pos));
if (b.s <= a.pos && b.e >= a.pos)
return min(abs(a.s - b.pos), abs(a.e - b.pos));
int dist1 = min(abs(a.s - b.pos), abs(a.e - b.pos));
int dist2 = min(abs(b.s - a.pos), abs(b.e - a.pos));
return sqrt(dist1 * dist1 + dist2 * dist2);
} void calculate_graph()
{
for (int i = ; i < wall_num; i++)
{
for (int j = i + ; j < wall_num; j++)
{
graph[i][j] = graph[j][i] = cal(wall[i], wall[j]);
}
}
} double prim()
{
bool vis[MAX_WALL_NUM];
double dist[MAX_WALL_NUM];
memset(vis, , sizeof(vis));
fill(dist, dist + wall_num, INF);
dist[] = ;
double ret = ;
while (!vis[])
{
double min_dist = INF;
int temp = -;
for (int i = ; i < wall_num; i++)
if (!vis[i] && dist[i] < min_dist)
{
temp = i;
min_dist = dist[i];
}
if (temp == -)
return -;
vis[temp] = true;
dist[temp] = ;
ret = max(ret, min_dist);
for (int i = ; i < wall_num; i++)
dist[i] = min(dist[i], dist[temp] + graph[temp][i]);
}
return ret;
} int main()
{
while (scanf("%d", &wall_num), wall_num)
{
input();
calculate_graph();
printf("%.2f\n", prim());
}
return ;
}

poj1292的更多相关文章

随机推荐

  1. emoji & click copy

    emoji & click copy document.execCommand("copy"); https://clipboardjs.com/ https://www. ...

  2. python中 除了if else def class 有作用域 其余没有作用域

    python中 除了if else def class 有作用域 其余没有作用域

  3. CentOS7下安装Scrapy

    更新yum[root@localhost ~]# yum -y update1安装gcc及扩展包[root@localhost ~]# yum install gcc libffi-devel pyt ...

  4. CodeForces - 707C

    C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. MT【175】刚刚凑巧

    已知$\Delta ABC$满足$\sin^2A+\sin^2B+\sin^2C=2\sqrt{3}\sin A\sin B\sin C,a=2$,求$A$ 提示:利用正弦定理:$a^2+b^2+c^ ...

  6. NOI2018旅游记

    这居然是我第一次参加非NOIP的NOI系列赛事,有点小期待啊 前几天的UNR我暴露出了许多问题,而且翻了好多分,不过令人震惊的是假设Day1不停电(导致已经写好的T3没交上去)我居然有rk10,虽然并 ...

  7. 【Treap 例题】神秘岛(island)

    神秘岛(island) 题目描述: 除了敲代码和撩妹,旅行是cxw123 的第三爱好.他来到了澳大利亚东北部的大宝礁,在这里,有一个隔绝人世的神秘岛,这个岛不同于附近其他的珊瑚岛,它的生长速度极快,甚 ...

  8. 前端学习 -- Css -- 浮动

    块元素在文档流中默认垂直排列,所以这个三个div自上至下依次排开,如果希望块元素在页面中水平排列,可以使块元素脱离文档流. 使用float来使元素浮动,从而脱离文档流 可选值: none,默认值,元素 ...

  9. hdu 3022 Sum of Digits

    http://acm.hdu.edu.cn/showproblem.php?pid=3022 题意: 最多不超过10000组数据,每组数据给定两个数n,m,求一个最小的数,使得该数每一位之和等于n,每 ...

  10. info replication

    主Redis设置值:redis-cli -h 192.168.18.121 -p 63800 -a tinywan123456 登陆从1:redis-cli -h 192.168.18.121 -p ...