您的位置:首页 > 其它

最小生成树Kruskal算法【模板】 与 并查集 例题:简单 poj 2485 Highways

2018-02-01 16:29 716 查看

Kruskal算法

每次选取权重最小的边,如果这条边是安全的,那么就把它加入到生成树中,直到选取n-1条表是算法结束(若找不到,则说明不存在最小生成树MST)。

所谓安全边,指的是这条边连接的两个结点,原本是处于不同的树上的。(否则就形成了回路)

很显然,Kruscal算法中用到了并查集,判断某一条边是否连接了两棵不同的树。

模板代码:

(并查集部分省略)

重载符号的使用见博客

struct Edge
{
int u, v, w;// 注意这里要存完整的两个端点
friend bool operator < (Edge a, Edge b)//也常用优先队列
{
return a.w < b.w;//要注意重载符号的使用!!!
}
} e[MAXN * MAXN];//注意这里要开MAXN * MAXN,因为每一个节点到其他节点都可能存在边

int edgeCount = 0;//记录边的条数
sort(e, e + edgeCount);
int times = 0;//记录选取边的条数
for(int i = 0; i < edgeCount; i++)
{
if(merge(e[i].u, e[i].v))
{
//按题目要求操作,如求最长最短的路,或求路的总长
if(++times == n - 1)//选取n-1条边后终止
break;
}
}


例题:简单 poj 2485 Highways

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They’re planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

Input

The first line of input is an integer T, which tells how many test cases followed.

The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

Sample Input

1

3
0 990 692
990 0 179
692 179 0


Sample Output

692


Hint

Huge input,scanf is recommended.

题意:

有n个城市,每一行给的数据是第i座城市到 1 ~ n座城市的距离,要在城市间建公路,使各个城市相通,求最短路径中建的最长的公路的长度。

思路:

最小生成树模板题,直接套。

AC代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAXN = 550;
int n, fa[MAXN];
struct Edge
{
int u, v, w;
friend bool operator < (Edge a, Edge b)
{
return a.w < b.w;
}
} e[MAXN * MAXN];

void init()
{
for(int i = 0; i <= n; i++)
{
fa[i] = i;
}
}

int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}

bool merge(int x, int y)
{
int fx = find(x), fy = find(y);
if(fx != fy)
{
fa[fx] = fy;
return true;
}
return false;
}

int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
init();
int edgeCount = 0;//记录边的条数
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
int a;
scanf("%d", &a);
if(i != j)
{
e[edgeCount].u = i;
e[edgeCount].v = j;
e[edgeCount].w = a;
edgeCount++;
}
}
}
sort(e, e + edgeCount);
int longroad = -1;
int times = 0;//记录选取边的条数
for(int i = 0; i < edgeCount; i++)
{
if(merge(e[i].u, e[i].v))
{
longroad = max(longroad, e[i].w);
if(++times == n - 1)
break;
}
}
printf("%d\n", longroad);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息