Ultraviolet A 534-Frogger (Kruskal extension)

Source: Internet
Author: User
Ultraviolet A 534-Frogger

Question Link

Given some points, a path is required to jump from the first point to the second point, and the maximum distance on the path is the smallest.

Idea: Use the Kruskal algorithm to add the edge of the smallest weight value each time to determine whether the edge can be connected to two points. If yes, the current weight is that the answer complexity is O (n ^ 2log (n ))

But in fact, I can use Floyd to create O (N ^ 3 .. However, the efficiency is not as good as the above method.

Code:

#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;}


Ultraviolet A 534-Frogger (Kruskal extension)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.