bzoj2654(wqs二分+MST)

5 篇文章 0 订阅

直接二分白色边边权权值再做MST就可以了。。注意相同边权排序时保持黑白色边的一致性。。

 

/*
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y)/2
#define NM 50005
#define nm 100005 
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e16;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 
 
 



int n,m,s,ans,p,f[NM];
struct tmp{
    int x,y,v,f;
    bool operator<(const tmp&o)const{return v<o.v||(v==o.v&&f<o.f);}
}a[nm];

int find(int x){return f[x]==x?x:f[x]=find(f[x]);}

bool check(int t){
    int cnt=0;s=0;
    inc(i,1,m)if(!a[i].f)a[i].v+=t;
    inc(i,1,n)f[i]=i;
    sort(a+1,a+1+m);
    inc(i,1,m){
	int x=find(a[i].x),y=find(a[i].y);
	if(x==y)continue;
	f[x]=y;s+=a[i].v;
	if(!a[i].f)cnt++;
    }
    inc(i,1,m)if(!a[i].f)a[i].v-=t;
    return cnt>=p;
}

int main(){
    n=read();m=read();p=read();
    inc(i,1,m)a[i].x=read()+1,a[i].y=read()+1,a[i].v=read(),a[i].f=read();
    for(int x=-100,y=100;x<=y;)
	if(check(mid))ans=s-mid*p,x=mid+1;else y=mid-1;
    return 0*printf("%d\n",ans);
}

 

 

 

 

2654: tree

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 3781  Solved: 1631
[Submit][Status][Discuss]

Description

给你一个无向带权连通图,每条边是黑色或白色。让你求一棵最小权的恰好有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

 

Source

 

[Submit][Status][Discuss]



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值