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. C语言和go语言之间的交互 - C语言中使用go语言,使用的go语言又使用了c语言

    一.go语言中使用C语言 go代码中使用C代码,在go语言的函数块中,以注释的方式写入C代码,然后紧跟import “C” 即可在go代码中使用C函数 代码示例: go代码:testC.go 1 pa ...

  2. [转帖] 外部访问k8s 里面pod的方式方法

    https://jimmysong.io/posts/accessing-kubernetes-pods-from-outside-of-the-cluster/ 从外部访问Kubernetes中的P ...

  3. ITSS相关的名词解释

    1.ITSM(IT Service Management)IT服务管理.从宏观的角度可以理解为一个领域或行业,人中观的角度可以理解为一种IT管理的方法论,从微观的角度可以理解为是一套协同动作的流程.从 ...

  4. FuelPHP 系列(二) ------ route 路由

    FuelPHP 中,默认可以通过 /controller_name/function_name 这种方式来访问,也可以通过自定义路由来访问. 路由配置在 /fuel/app/config/routes ...

  5. 一本通1628X-factor Chain

    1628:X-factor Chain 时间限制: 1000 ms         内存限制: 524288 KB [题目描述] 原题来自 POJ 3421 输入正整数 x,求 x 的大于 1 的因子 ...

  6. Codeforces 494C - Helping People

    题意 有一个长度为 \(n\) 的数列 \(a\),有 \(m\) 个 操作,每个操作是给 \(a[l_i,r_i]\) 中的数都加一,一个操作有 \(p_i\) 的概率执行(否则不执行).一个性质是 ...

  7. SpringBoot设置事务隔离等级

    "If you're gonna play the game, boy, ya gotta learn to play it right" Spring Boot 使用事务非常简单 ...

  8. poj 1511 Invitation Cards(最短路中等题)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

  9. WebService 检测到有潜在危险的 Request.Form 值

    在web.config 的 <system.web> <pages validateRequest="false" /> <httpRuntime r ...

  10. 【bzoj3994】 SDOI2015—约数个数和

    http://www.lydsy.com/JudgeOnline/problem.php?id=3994 (题目链接) 题意 多组询问,给出${n,m}$,求${\sum_{i=1}^n\sum_{j ...