UVA1395

// UVa1395 Slim Span
// Rujia Liu
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

 + ;
;
int n;

int pa[maxn];
int findset(int x) { return pa[x] != x ? pa[x] = findset(pa[x]) : x; } 

struct Edge {
  int u, v, d;
  Edge(int u, int v, int d):u(u),v(v),d(d) {}
  bool operator < (const Edge& rhs) const {
    return d < rhs.d;
  }
};

vector<Edge> e;

int solve() {
  int m = e.size();
  sort(e.begin(), e.end());
  int ans = INF;
  ; L < m; L++) {  //枚举从第L条边开始kruskal
    ; i <= n; i++) pa[i] = i;
    int cnt = n; // number of sets
    for(int R = L; R < m; R++) {  //枚举终点,遇到终止条件break
      int u = findset(e[R].u), v = findset(e[R].v);
      if(u != v) {
        pa[u] = v;
        ) { ans = min(ans, e[R].d-e[L].d); break; }  //计算“苗条度”
      }
    }
  }
  ;
  return ans;
}

int main() {
  int m, u, v, d;
   && n) {
    e.clear();
    ; i < m; i++) {
      scanf("%d%d%d", &u, &v, &d);
      e.push_back(Edge(u, v, d));
    }
    printf("%d\n", solve());
  }
  ;
}
上一篇:公众号对接绑定视频教程<推荐>【申明:来源于网络】


下一篇:【POJ】2385 Apple Catching(dp)