【Uvalive4960】 Sensor network (苗条树,进化版)
【题意】
给出N个点,M条边,问这N个点形成的生成树的最大权值边-最小权值边的最小值
Input
The input consists of several test cases, separated by single blank lines. Each test case begins with a
line containing the integer n (2 ≤ n ≤ 350), the number of doors in the building. The following line
contains another integer, m (n − 1 ≤ m ≤ n(n − 1)/2), the number of sensors in the network. The
test case finishes with m lines containing a description of each of the m sensors. The i-th of those lines
contains three integers, a (0 ≤ a ≤ n−1), b (0 ≤ b ≤ n−1) and w (1 ≤ w ≤ 2
15), in that order. Integers
a and b represent the pair of doors controlled by the i-th sensor, and w its recommended voltage. You
can safely assume that there are no two sensors controlling the same two doors.
The input will finish with a line containing ‘0’.
Output
For each case, your program should output a line containing the minimum margin of an admissible
subset of the sensors.
Sample Input
3
3
0 1 220
1 2 120
2 0 160
4
5
2 3 80
1 3 80
0 1 180
2 1 200
3 0 140
0
Sample Output
40
60
【分析】
是Uva1395的进化版。
数据范围大了一点点,不过之前的方法也是够慢的,m^2。
总结来说,生成树问题其实就是有一棵最小生成树,然后你sm乱枚举一些东西,然后在最小生成树上搞啊搞。。【难道不是么??..
最喜欢就是加一条边弄一个环然后在环上面又删一条边,就是没add边的时候两个点之间唯一路径上的边的权值求最值。
这题就是这样啦。
先排序,加边,考虑一下加下去的边是否会形成环,如果形成环的话,就把环内的最小边去掉(因为最长边就是现在ADD的那条边,如果要苗条就让边最小尽量大),然后重新求这棵新的生成树的最小边。等到生成树形成的时候,因为添加进去的新边的权值肯定是最大值的,所以只要只减去之前维护一个的最小值就可以了。
然后,求路径最小边还有找最短边都是O(n)暴力的。
总时间复杂度:O(nm)
之前的方法复杂度是O(m^2),嗯,很多恶心题就是完全图,如果是完全图的话,这样的方法无疑是更优的。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define Maxn 410
#define INF 0xfffffff int n,m; struct node
{
int x,y,c;
}t[Maxn*Maxn]; bool cmp(node x,node y) {return x.c<y.c;}
int mymin(int x,int y) {return x<y?x:y;} int fa[Maxn]; bool vis[Maxn];
int get_lca(int now)
{
int x=t[now].x,y=t[now].y,ans=;
for(int i=;i<=n;i++) vis[i]=;
while(x!=fa[x]) vis[x]=,x=fa[x]; vis[x]=;
while(y!=fa[y])
{
if(vis[y]) {ans=y;break;}
y=fa[y];
}
if(ans==&&vis[y]) ans=y;
// while(x!=fa[x]) vis[x]=0,x=fa[x]; vis[x]=0;
return ans;
} int dis[Maxn],mn,cnt;
void ffind(int now)
{
int x=t[now].x,y=t[now].y;
// if(fa[x]!=fa[y]) return;
int lca=get_lca(now);
if(lca==) return;
int k=INF,kv;
while(x!=lca)
{
if(dis[x]<k)
{
k=dis[x];
kv=x;
}
x=fa[x];
}
while(y!=lca)
{
if(dis[y]<k)
{
k=dis[y];
kv=y;
}
y=fa[y];
}
fa[kv]=kv,dis[kv]=;
mn=INF;
for(int i=;i<=n;i++) if(fa[i]!=i)
{
mn=mymin(mn,dis[i]);
}
cnt--;
} void add(int now)
{
int x=t[now].x,y=t[now].y,c=t[now].c;
if(fa[x]==fa[y]) return;
if(fa[x]==x) fa[x]=y,dis[x]=c;
else if(fa[y]==y) fa[y]=x,dis[y]=c;
else
{
int xx=x,u=fa[x],dd=dis[x];
while(x!=u)
{
int nu=fa[u],nd=dis[u];
fa[u]=x;
dis[u]=dd;
x=u;u=nu;dd=nd;
}
fa[xx]=y;dis[xx]=c;
}
mn=mymin(mn,c);
cnt++;
} int main()
{
while()
{
scanf("%d",&n);
if(n==) break;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].c);
t[i].x++;t[i].y++;
}
sort(t+,t++m,cmp);
cnt=;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) fa[i]=i;
mn=INF;
memset(dis,,sizeof(dis));
int ans=INF;
for(int i=;i<=m;i++)
{
ffind(i);
add(i);
if(cnt==n-) ans=mymin(ans,t[i].c-mn);
}
printf("%d\n",ans);
}
return ;
}
WC这题代码把LA3887 A了!!!!
2016-11-02 09:52:33
--------------------------------------------------------------------------
突然发现这题是删边模版?
【Uvalive4960】 Sensor network (苗条树,进化版)的更多相关文章
- 【转】线段树完全版~by NotOnlySuccess
线段树完全版 ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...
- 布拉格捷克理工大学研究团队:Prisma进化版
原文链接 还记得 Prisma 吗?就是能把拍摄的照片转化为各种名画风格的修图软件,神经网络的深度学习后,想要波普还是梵高风的图片都不在话下. 现在,它的进化版本来了.这回是布拉格捷克理工大学的研究 ...
- 【BZOJ4080】【WF2014】Sensor Network [随机化]
Sensor Network Time Limit: 2 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 魔法炮来到了帝都 ...
- 从零開始学Swift之Hello World进化版
上节课,也就是昨晚啦,我们学习到从零開始学Swift之Hello World.那一节仅仅有一句代码,大家会认为不够过瘾. 那么这节课,就给大家来多点瘾货吧! 先上图! //var 代表变量的类型, s ...
- B树的进化版----B+树
C++为什么叫C plus plus?这是由于C++相当于继承C的语法后,增加了各方面的能力,所扩展出的一种新语法.在软件领域中 plus 有增加的味道.在这里B +树也一样,是B树的增强版.在学习B ...
- HDU 4287 Intelligent IME(字典树数组版)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- 【转】 线段树完全版 ~by NotOnlySuccess
载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...
- POJ 3659 Cell Phone Network(树的最小支配集)(贪心)
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6781 Accepted: 242 ...
随机推荐
- Redis与Memcached对比
Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富,有字符串.链表.集合和有序集合.支持在服务器端计算集合的并,交和补集等.还支持多 ...
- C#扫盲之:带你掌握C#的扩展方法、以及探讨扩展方法的本质、注意事项
1.为什么需要扩展方法 .NET3.5给我们提供了扩展方法的概念,它的功能是在不修改要添加类型的原有结构时,允许你为类或结构添加新方法. 思考:那么究竟为什么需要扩展方法呢,为什么不直接修改原有类型呢 ...
- 一个react的完整项目展示
和一些人的关系像平行线,一辈子相守相望,见于眼底藏于心间.就怕耐不住寂寞,冲动而成了相交线,在一个点尽情拥抱,从此便离得越来越远,再也不见.遇到这样的人,因为不想做恋人只能一时,所以才选择做朋友能一世 ...
- 20160501--struts2入门3
一.自定义拦截器 要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口: public class PermissionInterc ...
- MVC小系列(二十)【给Action提供HttpStatusCodeResult】
主要用到: HttpStatusCodeResult 和HttpStatusCode 的http返回状态 比如: /// <summary> /// 使用异步模式 /// </sum ...
- TMemIniFile 与TIniFile 区别
在uses 申明 Inifiles MyStream:TMemIniFile; MyStream:=TMemIniFile.Create('c:\proxy.ini'); memo1.Text:=My ...
- Codevs 3233 古道
3233 古道 时间限制: 1 s 空间限制: 8000 KB 题目等级:**白银 Silver** [传送门](http://codevs.cn/problem/3233/) 题目描述 Descri ...
- IOS 学习笔记 2015-03-27 我理解的OC-代理模式
案例1 KCButton.h // // KCButton.h // Protocol&Block&Category // // Created by Kenshin Cui on 1 ...
- (转)IOS内存管理 retain release
obj-c本质就是"改进过的c语言",大家都知道c语言是没有垃圾回收(GC)机制的(注:虽然obj-c2.0后来增加了GC功能,但是在iphone上不能用,因此对于iOS平台的程序 ...
- ECMA5.1中Object.seal()和Object.freeze()的区别
1 Object.seal(O)的调用 When the seal function is called, the following steps are taken: If Type(O) i ...