给你一个无向带权连通图,每条边是黑色或白色。让你求一棵最小权的恰好有need条白色边的生成树。
题目保证有解。

Input

第一行V,E,need分别表示点数,边数和需要的白色边数。
接下来E行,每行s,t,c,col表示这边的端点(点从0开始标号),边权,颜色(0白色1黑色)。

Output

一行表示所求生成树的边权和。
V<=50000,E<=100000,所有数据边权为[1,100]中的正整数。

Sample Input

2 2 1
0 1 1 1
0 1 2 0

Sample Output

2

Hint

原数据出错,现已更新 by liutian,但未重测---2016.6.24


  显然是MST,但是在Kruskal的过程中我们无法控制白边的数量。如果考虑修改边值,可以发现如果给白边的边权都加上一个delta,那么白边的数量随着delta的增大而减小。所以可以二分它去控制白边的数量。

Code

 /**
* bzoj
* Problem#2654
* Accepted
* Time:1892ms
* Memory:3052k
*/
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} typedef class union_found{
public:
int *f;
union_found():f(NULL) {}
union_found(int points) {
f = new int[(const int)(points + )];
clear(points);
}
int find(int x) {
if(f[x] != x) return f[x] = find(f[x]);
return f[x];
}
void unit(int fa, int so) {
int ffa = find(fa);
int fso = find(so);
f[fso] = ffa;
}
boolean connected(int a, int b) {
return find(a) == find(b);
}
void clear(int points) {
for(int i = ; i <= points; i++)
f[i] = i;
}
}union_found; int delta = ; typedef class Edge {
public:
int from;
int end;
int val;
boolean col; inline int getVal() const {
if(col) return val + delta;
return val;
}
}Edge; boolean operator < (const Edge& a, const Edge& b) {
return a.getVal() < b.getVal();
} int n, m, lim;
union_found uf;
Edge* edge; inline void init() {
readInteger(n);
readInteger(m);
readInteger(lim);
uf = union_found(n);
edge = new Edge[(const int)(m + )];
for(int i = ; i < m; i++) {
readInteger(edge[i].from);
readInteger(edge[i].end);
readInteger(edge[i].val);
readInteger(edge[i].col);
edge[i].col ^= ;
}
} int res = ;
int kruskal(int mid) {
delta = mid;
res = ;
uf.clear(n);
sort(edge, edge + m);
int fw = , fin = ;
for(int i = ; i < m; i++) {
if(!uf.connected(edge[i].from, edge[i].end)) {
uf.unit(edge[i].from, edge[i].end);
fw += edge[i].col, fin ++, res += edge[i].val;
}
}
return fw;
} inline void solve() {
int l = -, r = , c;
while(l <= r) {
int mid = (l + r) >> ;
if((c = kruskal(mid)) < lim) r = mid - ;
else if(c > lim) l = mid + ;
else break;
}
printf("%d\n", res);
} int main() {
init();
solve();
return ;
}

bzoj 2654 tree - 二分法 - 最小生成树的更多相关文章

  1. BZOJ 2654: tree(二分 最小生成树)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2901  Solved: 1196[Submit][Status][Discuss] Descript ...

  2. BZOJ 2654: tree( 二分 + MST )

    我们给白色的边增加权值 , 则选到的白色边就会变多 , 因此可以二分一下. 不过这道题有点小坑... ------------------------------------------------- ...

  3. BZOJ 2654: tree Kruskal+二分答案

    2654: tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1863  Solved: 736[Submit][Status][Discuss ...

  4. BZOJ 2654: tree

    Description \(n\) 个点, \(m\) 条边,边有权值和黑/白色,求含有 \(need\) 个白边的生成树. Sol 二分+Kruskal. 将每条白边都加上一个权值,然后跑最小生成树 ...

  5. [BZOJ 2654]tree(陈立杰)

    Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色 ...

  6. hdu 4253 Two Famous Companies BZOJ 2654 tree

    [题意]:给出n个点,m条边,边分为两种,一种是A公司的,一种是B公司的.边上有权值,问用n-1条边把n个点连起来的最小费用是多少,其中A公司的边刚好有k条.题目保证有解. 思路:我们发现,如果我们给 ...

  7. bzoj 2654 tree 二分+kruskal

    tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2739  Solved: 1126[Submit][Status][Discuss] Des ...

  8. BZOJ 2654 tree(二分答案+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2654 [题目大意] 给你一个无向带权连通图,每条边是黑色或白色. 让你求一棵最小权的恰 ...

  9. bzoj 2212 Tree Rotations

    bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...

随机推荐

  1. LAMP部署流水

    1.安装完成linux系统后,关闭防火墙: [root@localhost ~]# service iptables stop iptables: Setting chains to policy A ...

  2. 快速排序之python

    快速排序( Quick sort) 快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行递归排序,以达到整个序列有 ...

  3. LoadRunner-参数化(界面说明)

    1.Parameter type:参数类型 2. Select columm :选择列的方式,可以通过列编号或名称. By number:通过列编号  比如参数accounts位于第1列,passwo ...

  4. vueawesomeswiper自定义 导航点

    1,pagination的配置 pagination: { el: '.swiper-paginationfull', // type:'bullets', // bulletElement : 's ...

  5. mybatis之入门

    一.mybatis介绍 是apache旗下的一个开源的顶级ORM框架(做dao层的操作) 开始叫ibatis在2010年经过升级后发布到google code上就改名为mybatis 定位:1.是一个 ...

  6. Java学习之路-Burlap学习

    今天我们来学一下Burlap. Burlap是一种基于XML远程调用技术,但与其他基于XML的远程技术(例如SOAP或者XML-RPC)不同,Burlap的消息结构尽可能的简单,不需要额外的外部定义语 ...

  7. win8 metro 自己写摄像头拍照项目

    这个项目不是用的系统自带的CameraCaptureUI.是自己写的摄像头的调用,界面做的不好所以,不放了.可是能够实现拍照功能: 以下是using 程序命名空间: using Windows.Med ...

  8. SqlServer--bat批处理执行sql语句1-osql

    首先需要知道,此处使用的批处理命令是osql ,如果安装了SqlServer,目录类似: D:\Program Files\Microsoft SQL Server\100\Tools\Binn 脚本 ...

  9. Linux CentOS6.5下编译安装MySQL 5.6

    检查:卸载掉原有MySql 因为mysql数据库在Linux上实在是太流行了,所以目前下载的主流Linux系统版本基本上都集成了mysql数据库在里面,我们可以通过如下命令来查看我们的操作系统上是否已 ...

  10. EditPlus 4.3.2499 中文版已经发布(11月21日更新)

    新的版本修复了如下问题: 文本库的日期快捷方式“^@”失效. 列选模式下“减少缩进量”命令无法执行. 在某些情况下突出显示匹配括号导致程序崩溃.(这个问题是我发现的,电邮告诉作者后,一天之内就修复了) ...