标签:最大限度 namespace pos 因此 this 无法 生成树 cin class
Flatopia岛国完全平坦。不幸的是,Flatopia的公共高速公路系统非常糟糕。弗拉托利亚政府意识到了这个问题,并且已经建造了一些连接一些最重要城镇的高速公路。但是,仍有一些城镇无法通过高速公路抵达。有必要建造更多的高速公路,以便能够在不离开高速公路系统的情况下在任何一对城镇之间行驶。
Flatopian城镇的编号从1到N,城镇i的位置由笛卡尔坐标(xi,yi)给出。每条高速公路连接两个城镇。所有高速公路(原始高速公路和要建造的高速公路)都遵循直线,因此它们的长度等于城镇之间的笛卡尔距离。所有高速公路都可以在两个方向上使用。高速公路可以自由地相互交叉,但司机只能在位于两条高速公路尽头的小镇的高速公路之间切换。
Flatopian政府希望最大限度地降低建设新高速公路的成本。但是,他们希望保证每个城镇都可以从其他城镇到达公路。由于Flatopia是如此平坦,高速公路的成本总是与其长度成正比。因此,最便宜的高速公路系统将是最小化总公路长度的系统。
最小生成树,已连通的路直接更新父节点,再挨个点连线,更新剩余点,使用n-1条边后直接结束即可。
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXM = 562500+10;
const int MAXN = 750+10;
struct Node
{
double _x,_y;
}node[MAXN];
struct Path
{
int _l,_r;
double _value;
bool operator < (const Path & that)const{
return this->_value < that._value;
}
}path[MAXM];
int Father[MAXN];
double a[MAXN];
int n, m;
int s, p;
int Get_F(int x)
{
return Father[x] = (Father[x] == x) ? x : Get_F(Father[x]);
}
void Init(int x)
{
for (int i = 1;i <= x;i++)
Father[i] = i;
}
double Get_Len(Node a,Node b)
{
return sqrt((a._x - b._x) * (a._x - b._x) + (a._y - b._y) * (a._y - b._y));
}
int main()
{
cin >> n;
Init(n);
for (int i = 1;i <= n;i++)
cin >> node[i]._x >> node[i]._y;
cin >> m;
int l, r;
int k = 0;
for (int i = 1;i <= m;i++)
{
cin >> l >> r;
int tl = Get_F(l);
int tr = Get_F(r);
if (tl != tr)
{
Father[tl] = tr;
k++;
}
}
if (k == n - 1)
{
cout << endl;
return 0;
}
int pos = 0;
for (int i = 1;i <= n;i++)
{
for (int j = i + 1;j <= n;j++)
{
path[++pos]._l = i;
path[pos]._r = j;
path[pos]._value = Get_Len(node[i], node[j]);
}
}
sort(path + 1,path + 1 + pos);
for (int i = 1;i <= pos;i++)
{
int tl = Get_F(path[i]._l);
int tr = Get_F(path[i]._r);
if (tl != tr)
{
Father[tl] = tr;
cout << path[i]._l << ‘ ‘ << path[i]._r << endl;
k++;
}
if (k == n - 1)
break;
}
return 0;
}
标签:最大限度 namespace pos 因此 this 无法 生成树 cin class
原文地址:https://www.cnblogs.com/YDDDD/p/10340277.html