UVA 534 - Frogger(kruskal扩展)

UVA 534 - Frogger

题目链接

题意:给定一些点,现在要求一条路径从第一个点能跳到第二个点,并且这个路径上的最大距离是最小的

思路:利用kruskal算法,每次加最小权值的边进去,判断一下能否联通两点,如果可以了,当前权值就是答案复杂度为O(n^2log(n))

但是其实这题用floyd搞搞O(n^3)也能过啦。。不过效率就没上面那个方法优了

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 205;

struct Point {
	int x, y;
	void read() {
		scanf("%d%d", &x, &y);
	}
} p[N];

double dis(Point a, Point b) {
	int dx = a.x - b.x;
	int dy = a.y - b.y;
	return sqrt(dx * dx + dy * dy);
}

struct Edge {
	int u, v;
	double d;
	Edge() {}
	Edge(int u, int v) {
		this->u = u;
		this->v = v;
		d = dis(p[u], p[v]);
	}
	bool operator < (const Edge& c) const {
		return d < c.d;
	}
} E[N * N];

int n, en, parent[N];

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

int main() {
	int cas = 0;
	while (~scanf("%d", &n) && n) {
		en = 0;
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			p[i].read();
			for (int j = 0; j < i; j++)
				E[en++] = Edge(i, j);
		}
		sort(E, E + en);
		for (int i = 0; i < en; i++) {
			int pa = find(E[i].u);
			int pb = find(E[i].v);
			if (pa != pb)
				parent[pa] = pb;
			if (find(0) == find(1)) {
				printf("Scenario #%d\nFrog Distance = %.3lf\n\n", ++cas, E[i].d);
				break;
			}
		}
	}
	return 0;
}
时间: 2024-10-27 10:13:08

UVA 534 - Frogger(kruskal扩展)的相关文章

最小瓶颈路 Uva 534 Frogger

说明:关于Uva的题目,可以在vjudge上做的,不用到Uva(那个极其慢的)网站去做. 最小瓶颈路:找u到v的一条路径满足最大边权值尽量小 先求最小生成树,然后u到v的路径在树上是唯一的,答案就是这条路径. Uva 534 Frogger Time Limit: 3000MS 64bit IO Format: %lld & %llu Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly h

UVA 534 Frogger 【最小瓶颈树】

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=475 题意:求点1到2所有路径上最大边的最小值. 解法:Kruskal按边值排序.直到1,2联通. 代码: #include <stdio.h> #include <iostream> #include <algorithm> #include <s

uva 3592 (MST, kruskal)

题意:平面上有若干个点,求最小生成树.有最多8个套餐,每个套餐有一个价格和若干个点,一旦购买套餐内的点就会相互连通. 思路:由于套餐不是很多,所以枚举一下即可,然后最小生成树就行了. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-

Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)

题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0.然后进行一遍prim(2)Kruskal算法+并查集 代码: //prim写法 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <

UVA 10090 Marbles(扩展欧几里得)

Marbles Input: standard input Output: standard output I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The boxes are of two types: Type 1: each box costs c1 Taka and can hold exactly n1 marbles Type 2:

uva 10397 Connect the Campus kruskal || prim

uva上的题目总是要难一些的...总是会拐弯抹角,这道题目给出有的建筑之间已经建好了光缆,让求最小生成树,我还是比较喜欢用kruskal算法,但是这道题目肯定用prim比较快,prim复杂度是n^2,kruskal复杂度eloge. 用kruskal时需要预先用并查集处理一下已经建好的光缆,让他们属于同一个祖先: 用prim算法的时候需要把他们的边置为0,这样算sum的时候就不会加上了. 代码:(kruskal) #include<iostream> #include<cstdio>

UVA - 12232 Exclusive-OR (并查集扩展偏离向量)

Description You are not given n non-negative integersX0,X1,..., Xn-1 less than220, but they do exist, and their values never change. I'll gradually provide you some facts about them, and ask you some questions. There are two kinds of facts, plus one

UVA 11383 - Golden Tiger Claw(二分图完美匹配扩展)

UVA 11383 - Golden Tiger Claw 题目链接 题意:给定每列和每行的和,给定一个矩阵,要求每个格子(x, y)的值小于row(i) + col(j),求一种方案,并且所有行列之和的和最小 思路:A二分图完美匹配的扩展,行列建二分图,权值为矩阵相应位置的值,做一次KM算法后,所有顶标之和就是最小的 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algor

poj 2253 Frogger【最小生成树变形】【kruskal】

Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30427   Accepted: 9806 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her,