【poj3522】 Slim Span

http://poj.org/problem?id=3522 (题目链接)

题意

  求最小生成树的最大边与最小边差最小。

Solution

  排序后滑动窗口维护

代码

// poj3522
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;

const int maxn=5010;
struct edge {int u,v,w;}e[maxn];
int n,m,fa[maxn];

bool cmp(edge a,edge b) {
	return a.w<b.w;
}
int find(int x) {
	return fa[x]==x ? x : fa[x]=find(fa[x]);
}
int main() {
	while (scanf("%d%d",&n,&m)!=EOF && (n || m)) {
		for (int i=1;i<=m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		sort(e+1,e+1+m,cmp);
		int ans=inf;
		for (int l=1;l<=m;l++) {
			int cnt=0,flag=0;
			for (int i=1;i<=n;i++) fa[i]=i;
			for (int i=l;i<=m;i++) {
				int r1=find(e[i].u),r2=find(e[i].v);
				if (r1!=r2) cnt++,fa[r1]=r2;
				if (cnt==n-1) {flag=1;ans=min(ans,e[i].w-e[l].w);break;}
			}
			if (!flag) break;
		}
		if (ans==inf) printf("-1\n");
		else printf("%d\n",ans);
	}
	return 0;
}

  

时间: 2024-10-13 11:38:43

【poj3522】 Slim Span的相关文章

【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);

【UVA】1395-Slim Span

数学实在练不下去了,只能来水几个图论了,真想像D神一样来句:这道题很简单,直接AC就可以了. 大体思路:按照边的权值排序,枚举区间,利用并查集判断是否构成通路. 14042663 1395 Slim Span Accepted C++ 0.265 2014-08-15 02:11:53 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<v

【UVA 1395】 Slim Span (苗条树)

[题意] 求一颗生成树,满足最大边和最小边之差最小 InputThe input consists of multiple datasets, followed by a line containing two zeros separated by a space.Each dataset has the following format.n ma1 b1 w1...am bm wmEvery input item in a dataset is a non-negative integer.

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

POJ3522 Slim Span

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3959 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: 8633   Accepted: 4608 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 

【leetcode】901. Online Stock Span

题目如下: 解题思路:和[leetcode]84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素. 代码如下: class StockSpanner(object): def __init__(self): self.cl = [] self.vl = [] def next(self, price): """ :type price: int :rtype: int """

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,.