UVa1395 && POJ 3522 Slim Span

UVa

POJ

Description

求苗条度最小的生成树

苗条度指该生成树的最大边 - 最小边

Algorithm

Kruskal变形

先sort

然后枚举最小边 构建最小生成树

Hint

UVa JAVA的RE 不知道为何= =

POJ的JAVA才1.5 λ表达式是JAVA 1.8才有的 然后本人才疏学浅 除了λ表达式以外不会写自定义sort比较 所以CE = =

C++就过了

会了C++ 的INF写法

#include <climits>

这样就有

INT_MAX这个常量了

Code

#include <cstdio>
#include <climits>
#include <algorithm>
using namespace std;
struct Edge
{
  int u, v, w;
  Edge(){}
  Edge(int u, int v, int w) : u(u), v(v), w(w) {}
};
const int MAX_M = 109 * 108 / 2;
const int MAX_N = 100 + 9;
int n;
bool comp(Edge a, Edge b)
{
  if (a.w < b.w) return true; else return false;
}
struct UnionFind
{
  int n;
  int par[MAX_N];
  int rank[MAX_N];
  UnionFind(int nn)
  {
    n = nn;
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = i;
    }
  }
  int find(int x)
  {
    if (par[x] == x) return x;
    else return par[x] = find(par[x]);
  }
  void unite(int x, int y)
  {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  bool same(int x, int y) {
    return find(x) == find(y);
  }
  void print()
  {
    for (int i = 0; i < n; i++) {
      printf("%d ", par[i]);
    }
  }
  int total()
  {
    int s = 0;
    for (int i = 0; i < n; i++) {
      if (par[i] == i) s++;
    }
    return s;
  }
};
void solve()
{
  int m;
  scanf("%d", &m);
  Edge edges[MAX_M];
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    edges[i] = Edge(u - 1, v - 1, w);
  }
  sort(edges, edges + m, comp);
  UnionFind unionFind(n);
  const int INF = INT_MAX / 2;
  int ans = INF;
  for (int i = 0; i < m; i++) {
    UnionFind unionFind = UnionFind(n);
    int e = 0;
    for (int j = i; j < m; j++) {
      if (!unionFind.same(edges[j].u, edges[j].v)) {
        unionFind.unite(edges[j].u, edges[j].v);
        e++;
        if (e == n - 1) {
          ans = min(edges[j].w - edges[i].w, ans);
          break;
        }
      }
    }
  }
  if (ans == INF) ans = -1;
  printf("%d\n", ans);
}
int main()
{
//  freopen("input.txt", "r", stdin);
  for (;;) {
    scanf("%d", &n);
    if (n == 0) break;
    solve();
  }
}
时间: 2024-08-08 18:19:37

UVa1395 && POJ 3522 Slim Span的相关文章

poj 3522 Slim Span 最大边减最小边最小的生成树

枚举最小边进行kruskal. #include <cstdio> #include <algorithm> using namespace std; #define maxn 120 #define maxm 10000 struct edge { int u,v,w; }e[maxm]; int p[maxn],n,m; int find(int x) { if(x==p[x]) return x; return p[x]=find(p[x]); } void link(int

POJ 3522 Slim Span (Kruskal +枚举 边权差最小的生成树)

Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6685 Accepted: 3544 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V is a

POJ 3522 ——Slim Span——————【最小生成树、最大边与最小边最小】

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7102   Accepted: 3761 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V 

POJ 3522 Slim Span【枚举+克鲁斯卡尔求最小生成树】

Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7365 Accepted: 3909 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V is a

POJ 3522 Slim Span

Given an undirected weighted graph G , you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E) , where V is a set of vertices {v1, v2,..., vn} and E is a set of undirected edges {e1, e2,..., em} . Each edge e

POJ 3522 Slim Span 最小生成树,暴力 难度:0

kruskal思想,排序后暴力枚举从任意边开始能够组成的最小生成树 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 101; const int maxe = maxn * maxn / 2; struct edge{ int f,t,c; bool operator <(edge e2)const { return c<e2.c; } }e[maxe]; int

POJ 3522 Slim Span(最小生成树)

题意:给定一个n个点m条边的无向图,找一颗苗条度(最大边减最小边)最小的生成树. 思路:假设苗条度最小的这棵树的最小边为a,若要使苗条度最小,答案一定是以a为最小边的一颗最小生成树,所以可以考虑枚举最小边,计算出苗条度并更新答案. #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm

POJ 3522 Slim Span (并查集 + 枚举 + kruskal)

链接:点击打开链接 题目好长, 而且还有图片,所以就不复制粘贴过来了,这道题的大意是: 一棵树T(连通无环子图)将用n-1条边连接原图的所有的n个顶点,生成的生成树的最大权值边与最小权值边的差(称"苗条值")尽量小,找出这个最小的苗条值: 思路: 用kruskal枚举: 首先对每条边的权值从小到大进行排序: 枚举每条边为最小边生成最小生成树,并计算这样的生成树的苗条值,枚举玩所有的情况就可以求出苗条值: 代码解析如下: #include <iostream> #includ

uva 1395 - Slim Span poj 3522 Slim Span(最小生成树算法)

最近学习了一下 最小生成树 算法. 所谓最小生成树算法,就是给出一个连通图g[ maxn ][ maxn  ], 找出这个连通图的边权和最小的生成图(树). 可以实现这个目的的算法,我叫它最小生成树算法.kruskal算法就是我学到的一种实现这种功能的算法. 对于kruskal算法的描述以及简单的证明在刘汝佳第二版上已经说得够明白 本题就是求 最小生成树 里面的 最大边权和最小边权 相差最小的最小生成树. #include<cstdio> #include<cstring> #in