二分+最小生成树【bzoj2654】: tree
2024-09-01 23:47:03
2654: tree
给你一个无向带权连通图,每条边是黑色或白色。让你求一棵最小权的恰好有need条白色边的生成树。
题目保证有解。
二分答案,然后跑最小生成树判断。
注意优先跑白色边。
code:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int wx=500017;
inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
}
int fa[wx];
int n,m,k,tot,ans;
struct node{
int x,y,d,flag;
friend bool operator < (const node & a,const node & b){
return a.d<b.d;
}
}t[wx*2];
int find(int x){
if(x==fa[x])return x;
return fa[x]=find(fa[x]);
}
bool ok(int now){
for(int i=1;i<=n;i++)fa[i]=i;
int sum=0,tmp=0;
for(int i=1;i<=m;i++){
if(t[i].flag)continue;
int fx=find(t[i].x); int fy=find(t[i].y);
if(fx==fy)continue;
if(t[i].flag==0&&tmp>=k)continue;
sum+=t[i].d; fa[fx]=fy;
if(t[i].flag==0)tmp++;
}
for(int i=1;i<=m;i++){
int fx=find(t[i].x); int fy=find(t[i].y);
if(fx==fy)continue;
if(t[i].flag==0&&tmp>=k)continue;
sum+=t[i].d; fa[fx]=fy;
if(t[i].flag==0)tmp++;
}
return sum<=now&&tmp>=k;
}
int main(){
n=read(); m=read(); k=read();
for(int i=1;i<=m;i++){
int x,y,z,c;
x=read(); y=read(); z=read(); c=read();
x++; y++;
t[i].x=x; t[i].y=y; t[i].d=z; t[i].flag=c;
tot+=z;
}
sort(t+1,t+1+m);
int l=0,r=tot;
while(l<=r){
int mid=l+r>>1;
if(ok(mid))ans=mid,r=mid-1;
else l=mid+1;
}
printf("%d\n",ans);
return 0;
}
最新文章
- RxJava 和 RxAndroid 三(生命周期控制和内存优化)
- myeclipe eclipse 常遇问题:Some projects cannot be imported 、java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver、The file connot be validate
- CentOS 6.4下安装Oracle 11gR2
- TTL电平和CMOS电平总结
- 算法之旅,直奔<;algorithm>;之十四 fill_n
- PHP 访问类中的静态属性
- linux之grep实例讲解
- python学习笔记之一:列表与元组
- Object-C 新手教程
- HTML5表单提示placeholder属性兼容IE
- Python3 编程第一步
- Touch Handling in Cocos2D 3.x(一)
- 使用 Google
- makefile笔记7 - makefile函数
- mpvue开发小程序记录
- vue + vue-router+vuex+elementUI开发环境搭建
- ActiveMQ 集群和主从
- iPhone X Web 设计
- POJ3436(KB11-A 最大流)
- 题解 P2580 【于是他错误的点名开始了】
热门文章
- [转]eclipse 设置默认编码为Utf-8
- javascript——对象的基础知识
- 问题:PLS-00204: 函数或伪列 &#39;EXISTS&#39; 只能在 SQL 语句中使用;结果:PL/SQL中不能用exists函数?
- thread常用方法
- Lucene打分公式的数学推导
- [cerc2017J]Justified Jungle
- C# 把一个文件夹下所有文件删除
- JS Number类型数字位数及IEEE754标准
- oracle 监听服务OracleOraDb11g_home1TNSListener打开后立马停止错误
- p2234&;bzoj1588 营业额统计