【题意】

  给出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 (苗条树,进化版)的更多相关文章

  1. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  2. 布拉格捷克理工大学研究团队:Prisma进化版

    原文链接  还记得 Prisma 吗?就是能把拍摄的照片转化为各种名画风格的修图软件,神经网络的深度学习后,想要波普还是梵高风的图片都不在话下. 现在,它的进化版本来了.这回是布拉格捷克理工大学的研究 ...

  3. 【BZOJ4080】【WF2014】Sensor Network [随机化]

    Sensor Network Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 魔法炮来到了帝都 ...

  4. 从零開始学Swift之Hello World进化版

    上节课,也就是昨晚啦,我们学习到从零開始学Swift之Hello World.那一节仅仅有一句代码,大家会认为不够过瘾. 那么这节课,就给大家来多点瘾货吧! 先上图! //var 代表变量的类型, s ...

  5. B树的进化版----B+树

    C++为什么叫C plus plus?这是由于C++相当于继承C的语法后,增加了各方面的能力,所扩展出的一种新语法.在软件领域中 plus 有增加的味道.在这里B +树也一样,是B树的增强版.在学习B ...

  6. HDU 4287 Intelligent IME(字典树数组版)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  8. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

  9. POJ 3659 Cell Phone Network(树的最小支配集)(贪心)

    Cell Phone Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6781   Accepted: 242 ...

随机推荐

  1. linux部署tomcat

    安装说明 安装环境:CentOS-6.5安装方式:源码安装 软件:apache-tomcat-6.0.45.tar.gz下载地址:http://tomcat.apache.org/download-6 ...

  2. Nginx高性能服务器安装、配置、运维 (3) —— Nginx配置详解

    四.Nginx 配置详解 YUM方式安装的Nginx默认配置文件放在/etc/nginx目录下,使用Vim编辑/etc/nginx/nginx.conf: ---------------------- ...

  3. 【转】小议Bug敏感度---Bug敏感度的故事(一)

    在测试圈中,相信大家对“Bug敏感度”这一词并不陌生,但是Bug敏感度具体是指什么呢,本文对此关键词进行解读的基础上,对其与软件质量的关系,影响的关键因素,如何提高测试人员的bug敏感度进行分享.(- ...

  4. Working with BeforeProperties and AfterProperties on SPItemEventReceiver

    As many of you know, event receivers are a great way to hook into various SharePoint events.  These ...

  5. js中对象的创建

    json方式,构造函数方式,Object方式,属性的删除和对象的销毁 <html> <head> <title>js中的对象的创建</title> &l ...

  6. 双程动态规划 nyoj61

    题目大意: 在矩阵m*n中,从(1,1)点到(m,n)点,再从(m,n)点到(1,1)点,所走路线经过的同学最大好心值, 要求每个点只能走一遍. 分析: ①我们可以把它只看成两个人同时从(1,1)点, ...

  7. ionic(一) build你的第一个android apk

    1.ionic start myApp tabs    >>创建一个app 2.cd myApp  >>进入myApp文件 3.ionic platform add andro ...

  8. Sql2008的行列转换之行转列

    今天在工作的时候遇到了行列转换的问题,记得去年有一段时间经常写,但是许久不用已经记不太得了.好记性不如烂笔头,忙完之后赶紧记录一下. 关键字:PIVOT(行转列),UNPIVOT(列转行) 先说说 P ...

  9. silverlight圆球滚动

    经大神启发后,才知道设置几个变量尤其是bool类型的方向,之后就是简单的判断了. // 当用户导航到此页面时执行. protected override void OnNavigatedTo(Navi ...

  10. Java输出日历

    源码链接:http://pan.baidu.com/s/1o6xeybK