题目链接
https://acm.hdu.edu.cn/showproblem.php?pid=4253
题意
给出图,边分黑白两色,求使用k条黑边的最小生成树
思路
先贴论文http://www.doc88.com/p-916950232776.html
二分+生成树。
为每条黑边加权x,然后求出使用最少low条黑边和最多high条黑边的最小生成树。可以证明low到high是一个连续区间,这个low和high可以通过更改排序策略求出(同边权时优先黑/白边)。假如k落在这个区间中,那么使用k条黑边的生成树和这棵生成树是同一颗,那么答案就是cost(加权后生成树)-k*x。同时观察可以出性质,high和low显然x增时递减且low_x=high_x+1。那么我们直接求一下high或者low,二分判断即可。
注意到每次加权x后,黑边和白边的相对位置不变,所以可以提前排序用类似双指针的算法实现O(m)的排序所有边,可以降低时间复杂度。
代码
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
//#define int long long
//#define double long double
using namespace std;
typedef long long ll;
const int maxn=400505;
const int inf=0x3f3f3f3f;
int n,m,k;
struct Edge{
int u,v,w;
int who;
}edge[maxn];
int fa[maxn];
void init(){
for(int i=0;i<=n;i++) fa[i]=i;
}
int find(int x){
return x==fa[x]?fa[x]:fa[x]=find(fa[x]);
}
bool unite(int x,int y){
x=find(x),y=find(y);
if(x==y) return 0;
fa[x]=y;
return 1;
}
bool cmp1(Edge a,Edge b){//最多
if(a.w<b.w)return 1;
else if(a.w>b.w)return 0;
else{
return a.who<b.who;
}
}
int ans;
int cost;
int check(int x){
for(int i=1;i<=m;i++){
if(!edge[i].who)
edge[i].w+=x;
}
int high=0,low=0;
cost=0;
sort(edge+1,edge+1+m,cmp1);
init();
for(int i=1;i<=m;i++){
if(unite(edge[i].u,edge[i].v)){
cost+=edge[i].w;
if(!edge[i].who)
high++;
}
}
for(int i=1;i<=m;i++){
if(!edge[i].who)
edge[i].w-=x;
}
if(high>=k){
return 1;
}
return 0;
}
void solve(){
for(int i=1;i<=m;i++){
cin>>edge[i].u>>edge[i].v>>edge[i].w>>edge[i].who;
}
int l=-105,r=105,mid;
int need;
ans=inf;
while(l<r){
mid=(l+r)>>1;
int flag=check(mid);
if(flag==1){
l=mid+1;
ans=cost-k*mid;
}
else
r=mid;
}
cout<<ans<<endl;
}
signed main(){
IOS
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
int tn=1;
//cin>>tn;
while(cin>>n>>m>>k){
cout<<"Case "<<tn++<<": ";
solve();
}
}
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
//#define int long long
//#define double long double
using namespace std;
typedef long long ll;
const int maxn=400505;
const int inf=0x3f3f3f3f;
int n,m,k;
struct Edge{
int u,v,w;
int who;
}edge[maxn];
int fa[maxn];
void init(){
for(int i=0;i<=n;i++) fa[i]=i;
}
int find(int x){
return x==fa[x]?fa[x]:fa[x]=find(fa[x]);
}
bool unite(int x,int y){
x=find(x),y=find(y);
if(x==y) return 0;
fa[x]=y;
return 1;
}
bool cmp1(Edge a,Edge b){//最多
if(a.w<b.w)return 1;
else if(a.w>b.w)return 0;
else{
return a.who>b.who;
}
}
int ans;
int cost;
int check(int x){
for(int i=1;i<=m;i++){
if(!edge[i].who)
edge[i].w+=x;
}
int low=0;
cost=0;
sort(edge+1,edge+1+m,cmp1);
init();
for(int i=1;i<=m;i++){
if(unite(edge[i].u,edge[i].v)){
cost+=edge[i].w;
if(!edge[i].who)
low++;
}
}
for(int i=1;i<=m;i++){
if(!edge[i].who)
edge[i].w-=x;
}
if(low<=k){
return 1;
}
return 0;
}
void solve(){
for(int i=1;i<=m;i++){
cin>>edge[i].u>>edge[i].v>>edge[i].w>>edge[i].who;
}
int l=-105,r=105,mid;
int need;
ans=inf;
while(l<r){
mid=(l+r)>>1;
int flag=check(mid);
if(flag==1){
r=mid;
ans=cost-k*mid;
}
else
l=mid+1;
}
cout<<ans<<endl;
}
signed main(){
IOS
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
int tn=1;
//cin>>tn;
while(cin>>n>>m>>k){
cout<<"Case "<<tn++<<": ";
solve();
}
}