[生成树][Uva1395][Slim Span]
2015-10-08 16:56
281 查看
代码:
#include <set> #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <map> using namespace std; struct node { int s,t,w; }A[10000]; int N,M; int father[200]; int tot=0; void init() { for(int i=0;i<200;i++) father[i]=i; tot=0; } int find(int x) { if(x!=father[x]) father[x]=find(father[x]); return father[x]; } bool cmp(node a,node b) { return a.w<b.w; } void input() { for(int i=1;i<=M;i++) { scanf("%d%d%d",&A[i].s,&A[i].t,&A[i].w); } sort(A+1,A+M+1,cmp); } void solve() { int ans=0x3f3f3f3f; for(int L=1;L<=M;L++) { init(); for(int R=L;R<=M;R++) { int xx=find(A[R].s),yy=find(A[R].t); if(xx!=yy) { tot++; father[xx]=yy; } if(tot==N-1) { ans=min(ans,A[R].w-A[L].w); break; } } } if(ans==0x3f3f3f3f) printf("%d\n",-1); else printf("%d\n",ans); } int main() { // freopen("a.in","r",stdin); while(cin>>N>>M&&(N||M)) { input(); solve(); } }
相关文章推荐
- python多线程编程基础
- 高通平台msm8909 LK 实现LCD 兼容
- 《招聘一个靠谱的 iOS》—参考答案(下)
- 奥卡姆剃刀
- DWR框架搭建与使用
- [生成树][Uva1395][Slim Span]
- oracle所有用户都被锁定之后解决办法
- mysql备份和还原
- iOS开源库和完整应用
- 基于用户投票的排名算法Reddit
- IOS程式语法之block的使用掌握
- 深入理解html5系列-文本标签
- Linux(9.28-10.4)学习笔记
- 创建与合并分支
- iOS开发----音频、视频的实现
- 风口方向杂谈
- 《Orange's 一个操作系统的实现》学习笔记(二) 实模式和保护模式
- 文件写操作
- XML-RPC 的 Apache 实现
- 大数据漫谈1:大数据到底是什么