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;
}

最新文章

  1. RxJava 和 RxAndroid 三(生命周期控制和内存优化)
  2. myeclipe eclipse 常遇问题:Some projects cannot be imported 、java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver、The file connot be validate
  3. CentOS 6.4下安装Oracle 11gR2
  4. TTL电平和CMOS电平总结
  5. 算法之旅,直奔&lt;algorithm&gt;之十四 fill_n
  6. PHP 访问类中的静态属性
  7. linux之grep实例讲解
  8. python学习笔记之一:列表与元组
  9. Object-C 新手教程
  10. HTML5表单提示placeholder属性兼容IE
  11. Python3 编程第一步
  12. Touch Handling in Cocos2D 3.x(一)
  13. 使用 Google
  14. makefile笔记7 - makefile函数
  15. mpvue开发小程序记录
  16. vue + vue-router+vuex+elementUI开发环境搭建
  17. ActiveMQ 集群和主从
  18. iPhone X Web 设计
  19. POJ3436(KB11-A 最大流)
  20. 题解 P2580 【于是他错误的点名开始了】

热门文章

  1. [转]eclipse 设置默认编码为Utf-8
  2. javascript——对象的基础知识
  3. 问题:PLS-00204: 函数或伪列 &#39;EXISTS&#39; 只能在 SQL 语句中使用;结果:PL/SQL中不能用exists函数?
  4. thread常用方法
  5. Lucene打分公式的数学推导
  6. [cerc2017J]Justified Jungle
  7. C# 把一个文件夹下所有文件删除
  8. JS Number类型数字位数及IEEE754标准
  9. oracle 监听服务OracleOraDb11g_home1TNSListener打开后立马停止错误
  10. p2234&amp;bzoj1588 营业额统计