UVa 1395 Slim Span

问题:给出一个n结点的图,求最大边与最小边差值最小的生成树

my code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
#include <map>
using namespace std;
#define N 102
#define INF 0x7fffffff

struct Edge {
    int u, v, w;
    bool operator < (const Edge &t) const {
        return w < t.w;
    }
}c[N*(N-)/];
int p[N];

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

void build(int l, int r, int &edge, int &maxl, int &minl)
{
    for(int i=l; i<=r; i++) {
        int pu = findRoot(c[i].u);
        int pv = findRoot(c[i].v);
        if(pu != pv) {
            p[pu] = pv;
            edge++;
            maxl = max(maxl, c[i].w);
            minl = min(minl, c[i].w);
        }
    }
}

int main()
{
    int n, m;
    int maxl, minl, edge, gap;
    while(cin>>n>>m, n || m) {
        ){
            cout<< - <<endl;
            continue;
        }
        gap = INF;
        ; i<m; i++)
            cin>>c[i].u>>c[i].v>>c[i].w;
        sort(c, c+m);
        ; i+n-<m; i++) {
            ; k<=n; k++) p[k] = k;
            maxl = -INF; minl = INF; edge = ;
            build(i, i+n-, edge, maxl, minl);
            ){
                gap = min(gap, maxl-minl);
                continue;
            }
            ; j<m; j++) {
                build(j, j, edge, maxl, minl);
                ){
                    gap = min(gap, maxl-minl);
                    break;
                }
            }
        }
         <<endl;
        else cout<< gap <<endl;
    }

    ;
}
上一篇:UVA - 1395 Slim Span (最小生成树Kruskal)


下一篇:Es6对象的扩展和Class类的基础知识笔记