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. MySQL与Spring事务隔离级别

    https://zhuanlan.zhihu.com/p/27887568 能画第一张表,根据表描述.

  2. Dubbo和Spring Cloud微服务架构比较

    Dubbo 出生于阿里系,是阿里巴巴服务化治理的核心框架,并被广泛应用于中国各互联网公司:只需要通过 Spring 配置的方式即可完成服务化,对于应用无入侵,设计的目的还是服务于自身的业务为主. 微服 ...

  3. Spring之注入复杂类型属性

    注入类: package helloworld; import java.util.List; import java.util.Map; import java.util.Properties; p ...

  4. 洛谷P3348 [ZJOI2016]大森林(LCT,虚点,树上差分)

    洛谷题目传送门 思路分析 最简单粗暴的想法,肯定是大力LCT,每个树都来一遍link之类的操作啦(T飞就不说了) 考虑如何优化算法.如果没有1操作,肯定每个树都长一样.有了1操作,就来仔细分析一下对不 ...

  5. 【题解】 bzoj4004: [JLOI2015]装备购买 (线性基)

    bzoj4004,戳我戳我 Solution: 裸的线性基,这没啥好说的,我们说说有意思的地方(就是我老是wa的地方) Attention: 这题在\(luogu\),上貌似不卡精度,\(bzoj\) ...

  6. redirect和forward的区别

    异同 1.从地址栏显示来说 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,所以 ...

  7. webpack提取库

    有时候我们不想bundle.js过于大,想把一些库独立成文件单独拿出来 module.exports = { entry: { // bundle是我们要打包的项目文件的导出名字, app是入口js文 ...

  8. Hadoop生态圈-Flume的组件之sink处理器

    Hadoop生态圈-Flume的组件之sink处理器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一. 二.

  9. Hadoop生态圈-flume日志收集工具完全分布式部署

    Hadoop生态圈-flume日志收集工具完全分布式部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   目前为止,Hadoop的一个主流应用就是对于大规模web日志的分析和处理 ...

  10. Python模块调用方式详解

    Python模块调用方式详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其 ...