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. Activiti源码:StandaloneInMemProcessEngineConfiguration与SpringProcessEngineConfiguration

    activiti-engine-5.22.0-sources.jar package org.activiti.engine.impl.cfg; StandaloneInMemProcessEngin ...

  2. Docker(五)-Dcoker容器

    简单的说,容器是独立运行的一个或一组应用,以及它们的运行态环境. 如果把镜像看成面向对象中的 类 的话,那么容器就是 类 的实例化 对象. 容器 启动容器 启动容器有两种方式,一种是基于镜像新建一个容 ...

  3. 关于“Scrum敏捷项目管理”

    此次关于“Scrum”的名词解析,主要目的是为我们的“OneZero”团队确定项目开发的模式. http://www.cnblogs.com/taven/archive/2010/10/17/1853 ...

  4. js & parseFloat & toFixed

    js & parseFloat & toFixed https://repl.it/languages/javascript https://repl.it/repls/MintyBa ...

  5. BZOJ5287 HNOI2018毒瘤(虚树+树形dp)

    显然的做法是暴力枚举非树边所连接两点的选或不选,大力dp.考场上写的是最暴力的O(3n-mn),成功比大众分少10分.容斥或者注意到某些枚举是不必要的就能让底数变成2.但暴力的极限也就到此为止. 每次 ...

  6. fgt2eth Script

    fgt2eth Script explanation_on_how_to_packet_capture_for_only_certain_TCP_flags_v2.txt Packet capture ...

  7. 洛谷 P3975 [TJOI2015]弦论 解题报告

    P3975 [TJOI2015]弦论 题目描述 为了提高智商,ZJY开始学习弦论.这一天,她在<String theory>中看到了这样一道问题:对于一个给定的长度为\(n\)的字符串,求 ...

  8. Oracle数据库--PL/SQL存储过程和函数的建立和调用

    1.存储过程建立的格式: create or replace procedure My_Procedure is begin --执行部分(函数内容); end; / 例子:(以hr表为例) crea ...

  9. [APIO2018] New Home 新家

    扫描线+线段树+二分答案+set+STL 就是把区间数颜色做得很好 时间看成线段,扫描线 对于某一个询问位置x 二分答案转化,看区间内有没有k种颜色.. 一个区间数颜色的套路是,prei上一个该颜色出 ...

  10. Pair_2测试与优化

    # Pair_2测试与优化 211606316李震 21160305胡彤 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分 ...