http://www.spoj.com/problems/COMPANYS/en/

题目要求恰好有k条0类边的最小生成树

每次给0类边的权值加或减某个值delta,直到最小生成树上恰好有k条边为0,此时得到最小生成树的权值-更改的值*k即为答案

但是直接这么做的话会超时,因为都是整数权值,所以只需要优先取0类边,二分整数delta即可

如果直接给所有0类边减100,让0类边优先选择,那么会导致整棵树不一定是最小的,应该优先选择的1类边没有选择

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=2e5+5;
typedef pair<int,int> P;
int first[maxn];
struct edge{
int f,t,nxt,c;
int tel;
}e[maxn],org[maxn];
int n,m,k,sum;
bool used[maxn]; void addedge(int f,int t,int c,int tel,int i){
org[i].nxt=first[f];
first[f]=i;
org[i].f=f;
org[i].t=t;
org[i].c=c;
org[i].tel=tel;
}
class Greater: public binary_function<P, P,bool>
{
public:
bool operator()(const P& a, const P& b)
{
if(a.first!=b.first)return a.first>b.first;
return e[a.second].tel<e[b.second].tel;
}
};
priority_queue <P,vector<P>,Greater> pque;
int prim(){
memset(used,false,sizeof(used));
used[0]=true;
int unum=1;
for(int p=first[0];p!=-1;p=e[p].nxt){
pque.push(P(e[p].c,p));
}
sum=0;
int ans=0;
while(unum<n&&!pque.empty()){
int p=pque.top().second;
int t=e[p].t;
int td=pque.top().first;
pque.pop();
if(used[t])continue;
if(e[p].tel)ans++;
sum+=td;
used[t]=true;
unum++; for(int p=first[t];p!=-1;p=e[p].nxt){
if(!used[e[p].t]){
pque.push(P(e[p].c,p));
}
} }
while(!pque.empty())pque.pop();
return ans;
}
int main(){
int ki=0;
while(scanf("%d%d%d",&n,&m,&k)==3&&++ki){
memset(first,-1,sizeof(first));
for(int i=0;i<m;i++){
int f,t,c,color;
scanf("%d%d%d%d",&f,&t,&c,&color);
addedge(f,t,c,1-color,2*i);
addedge(t,f,c,1-color,2*i+1);
}
int l=-110,r=110;
int ans;
while(l<=r){
int mid=(l+r)/2;
copy(org,org+2*m,e);
for(int i=0;i<2*m;i++){
if(e[i].tel){
e[i].c+=mid;
}
}
int kk=prim();
// if(kk==k){ans=sum-k*mid;break;}
if(kk<k)r=mid-1;
else {ans=sum-k*mid;l=mid+1;}
}
printf("Case %d: %d\n",ki,ans);
}
return 0;
}

  

SPOJ COMPANYS Two Famous Companies 最小生成树,二分,思路 难度:2的更多相关文章

  1. HDU 4253-Two Famous Companies(二分+最小生成树)

    Description In China, there are two companies offering the Internet service for the people from all ...

  2. HDU 4253 Two Famous Companies

    Two Famous Companies Time Limit: 15000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...

  3. hdu4253 Two Famous Companies --- 二分+MST

    给n个点.m条边的图.每条边要么属于a公司,要么属于b公司.要求一颗最小生成树,条件是当中属于a公司的边数为k. 这题做法非常巧妙. 要求最小生成树,但有一定限制,搜索.贪心显然都不正确. 要是能找到 ...

  4. HDOJ 4253 Two Famous Companies 二分+MST

    题目意思:给出n个点,m条边,边分为两种,一种是A公司的,一种是B公司的.边上有权值, 问用n-1条边把n个点连起来的最小费用是多少,其中A公司的边刚好有k条.题目保证有解. 题解:题目意思很简单就是 ...

  5. 洛谷 P1991 无线通讯网 Label:最小生成树 || 二分

    题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都 ...

  6. hdu 4253 Two Famous Companies BZOJ 2654 tree

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

  7. bnuoj25660 Two Famous Companies

    题目链接:https://www.bnuoj.com/v3/problem_show.php?pid=25660 这个二分真的是烧脑QAQ,想了一晚上才懂了一个大概. 首先,整体思路是二分,直观上感受 ...

  8. LuoguP2323 [HNOI2006]公路修建问题 【最小生成树+二分】By cellur925

    题目大意:给你\(n\)个点,\(m\)条边,每条边上有两个权值:一级和二级的.选\(n-1\)条边使这个图连通,并至少有\(k\)个一级边,求花费最多的一条边最小值及方案. 最大值最小,肯定会先想到 ...

  9. P2619 [国家集训队2]Tree I(最小生成树+二分)

    P2619 [国家集训队2]Tree I 每次二分一个$x$,每条白边加上$x$,跑最小生成树 统计一下满足条件的最小值就好了. to me:注意二分不要写挂 #include<iostream ...

随机推荐

  1. Windows:子线程中创建窗口

    一般来讲,UI的所有操作都必须在主线程,否则会出现未知错误.但有时候我们会需要一个功能比较单一的窗口,同时希望他在一个单独的线程运行.并不影响主线程的效率. 下面说明一下新建子线程创建的新窗口的方法, ...

  2. linux 安装libevent

    今天再ubuntu下安装libevent,下载源码 tar -xzvf libevent-1.4.15.tar.gz cd libevent-1.4.15 ./configure make make ...

  3. 商铺项目(使用DES加密配置信息)

    package com.ouyan.o2o.util; import java.security.Key; import java.security.SecureRandom; import java ...

  4. 商铺项目(Logback配置与使用)

    <?xml version="1.0" encoding="utf-8"?> <configuration debug="false ...

  5. mysql 数据操作 单表查询 where 约束 目录

    mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...

  6. git-【三】理解工作区与暂存区的区别

    基本概念 工作区:就是你在电脑上看到的目录,比如目录下testgit里的文件(.git隐藏目录版本库除外).或者以后需要再新建的目录文件等等都属于工作区范畴.       版本库(Repository ...

  7. hive两大表关联优化试验

    呼叫结果(call_result)与销售历史(sale_history)的join优化: CALL_RESULT: 32亿条/444G SALE_HISTORY:17亿条/439G 原逻辑 Map: ...

  8. 基因芯片与NGS区别[转载]

    转自:http://blog.sina.com.cn/s/blog_40d4ae110101fjzy.html 1 二代测序与基因芯片的区别与优缺点. 生物芯片相对第二代测序而言,优势在于价格便宜,便 ...

  9. 在windows下MySQL-python的安装

    安装MySQL-python下载文件PyMySQL-0.7.11.tar.gz 解压到任意目录 https://pypi.python.org/pypi/PyMySQL 然后在cmd命令行行下进行安装 ...

  10. #C++初学记录

    输入与输出,头文件. #include<iostream> #include<algorithm> using namespace std; int main() { char ...