Algoogle

Algorithm for Programming Contest

AOJ 1280 Slim Span

Category: AOJ Tag: spanning-tree

Slim Span

問題概要


解法


重さの下限を順に試す. それ以上の重さの辺を使った最小全域木をそれぞれ考える.
その中で辺の重さが最大と最小の差が小さくなるものを選ぶ.

コード


(1280.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) repi(i,0,a)
#define all(u) (u).begin(),(u).end()
#define pb push_back
#define INF 1e9
using namespace std;
struct edge{
    int from, to, w;
    edge(int from, int to, int w):from(from),to(to),w(w){}
    bool operator < (const edge &e)const{
        return w < e.w;
    }
};

int n, m;
int parent[128];

int root(int x){
    if(parent[x] == x) return x;
    else return parent[x] = root(parent[x]);
}

int main(){
    while(cin >> n >> m, n || m){
        vector<edge> E;
        rep(i,m){
            int a, b, w; cin >> a >> b >> w;
            a--; b--;
            E.pb(edge(a,b,w));
        }
        int ans = INF;
        sort(all(E));
        rep(i,m){
            int k = 0;
            rep(j,n) parent[j] = j;
            repi(j,i,m){
                int p = root(E[j].from), q = root(E[j].to);
                if(p != q){
                    parent[q] = p;
                    k++;
                    if(k == n-1){
                        ans = min(ans, E[j].w - E[i].w);
                        break;
                    }
                }
            }
        }
        cout << (ans==INF? -1: ans) << endl;
    }
    return 0;
}

Comments