poj slim span

#include <bits/stdc++.h>

using namespace std;
#define N 105
#define INF 0x7ffffff
struct Edge
{
    int u;
    int v;
    int w;
}edge[N*N/2];

int n, m, span, p[N], rank[N];

int cmp(Edge a, Edge b)
{
    return a.w<b.w;
}

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

void unionSet(int x, int y)
{
    if (rank[x] > rank[y]) p[y] = x;
    else {
        p[x] = y;
        if (rank[x] == rank[y]) rank[y]++;
    }
}

void kruskal()
{
    sort(edge+1, edge+m+1, cmp);

    for(int i=1; i<=m-n+2; i++)
      {
          int cnt = 0;
          int start = 0;
          for(int j=1; j<=n; j++) p[j] = j;
          memset(rank, 0, sizeof(rank));
          for(int k=i; k<=m; k++)
          {
              int x = find(edge[k].u);
              int y = find(edge[k].v);
              if(x != y)
              {
                  unionSet(x,y);
                  cnt++;
                  if(!start)
                   start = i;
                  if(cnt == n-1)
                  {
                      span = min(span, edge[k].w-edge[start].w);
                      break;
                  }
              }
          }
      }
      if(span == INF) cout<<-1<<endl;
      else cout<<span<<endl;
}

int main()
{
      while(~scanf("%d%d",&n, &m) && n+m)
    {
        for(int i=1; i<=m; i++)
        cin>>edge[i].u>>edge[i].v>>edge[i].w;
        span = INF;
        kruskal();
    }
    return 0;
}

时间: 2024-11-10 07:48:09

poj slim span的相关文章

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 最大边减最小边最小的生成树

枚举最小边进行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——————【最小生成树、最大边与最小边最小】

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

苗条生成树Slim Span 与舒适的路线——刘汝佳的最小极差生成树(至少我第一次看到这种方法是从刘汝佳那里学来的)

昨天做的Slim Span,今天做了舒适的路线,都是差不多的题目.舒适的路线因为一些小瑕疵调了一会答案储存与输出. Slim Span: 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdio> 4 #include<vector> 5 using namespace std; 6 const int N=128; 7 struct node{ 8 int x,y,v; 9 bool oper

UVA1395 Slim Span(kruskal算法)

Slim Span [PDF Link] 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,.

[2016-01-27][UVA][1395][D -?Slim Span]

[2016-01-27][UVA][1395][D - Slim Span] 时间:2016-01-21  11:06:30  星期四 题目编号:UVA 1395 题目大意:求所有生成树 最大边和最小边之差的最小值 分析: 想法是枚举所有边,但是分析之后发现可以不用枚举所有的树 已知krukal得到的最小生成树得到的最小生成树,最大边最小 也就是说,这个最大边,就是使得 最大边和最小边 差值最大的边 那么,只需要求每条边的对应的差值最小的最大边即可 方法: 从最小的边开始跑生成树,然后跑krus

【Kruskal】Slim Span

[Uva1395]Slim Span 题目略…… 试题分析:codevs1001舒适的路线上加一个判一下连通性就好,顺便把除改成减 代码: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);

UVALive-3887 Slim Span (kruskal)

题目大意:定义无向图生成树的最大边与最小边的差为苗条度,找出苗条度最小的生成树的苗条度. 题目分析:先将所有边按权值从小到大排序,在连续区间[L,R]中的边如果能构成一棵生成树,那么这棵树一定有最小的苗条度.枚举所有这样的区间. 代码如下: # include<iostream> # include<cstdio> # include<set> # include<queue> # include<cstring> # include<al