Two Famous Companies

Time Limit: 15000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 4253
64-bit integer IO format: %I64d      Java class name: Main

 
In China, there are two companies offering the Internet service for the people from all cities: China Telecom and China Unicom. They both are planning to build cables between cities. Obviously, the government wants to connect all the cities in minimum costs. So the minister of finance Mr. B wants to choose some of the cable plans from the two companies and calculate the minimum cost needed to connect all the cities. Mr. B knows that N-1 cables should be built in order to connect all N cities of China. For some honorable reason, Mr. B should choose K cables from the China Telecom and the rest N-1-K cables from the China Unicom. Your job is to help Mr. B determine which cables should be built and the minimum cost to build them. You may assume that the solution always exists.

 

Input

Each test case starts with a line containing the number of cities N (1 <= N <= 50,000), number of cable plans M (N-1 <= M <= 100,000) and the number of required cables from China Telecom K (0 <= K <= N-1). This is followed by M lines, each containing four integers a, b, c, x (0 <= a, b <= N-1, a != b, 1 <= c <= 100, x in {0,1} indicating the pair of cities this cable will connect, the cost to build this cable and the company this cable plan belongs to. x=0 denotes that the cable plan belongs to China Telecom and x=1 denotes that the cable plan is from China Unicom.

 

Output

For each test case, display the case number and the minimum cost of the cable building.

 

Sample Input

2 2 1
0 1 1 1
0 1 2 0
2 2 0
0 1 1 1
0 1 2 0

Sample Output

Case 1: 2
Case 2: 1
Hint

In the first case, there are two cable plans between the only two cities, one from China Telecom and one from China Unicom. Mr. B needs to choose the one from China Telecom to satisfy the problem requirement even the cost is higher. In the second case, Mr. B must choose the cable from China Unicom, which leads the answer to 1.

Source

 
解题:最小生成树+二分。很不错的一题。。。在某个公司的边上增加一个值,直到取到了该公司指定的数目的边。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int u,v,w,id;
arc(int uu = ,int vv = ,int ww = ,int iid = ){
u = uu;
v = vv;
w = ww;
id = iid;
}
bool operator<(const arc &tmp) const{
return w < tmp.w;
}
};
arc e[][maxn];
int uf[maxn],n,m,k,tot1,tot2,cost;
int Find(int x){
if(x == uf[x]) return x;
return uf[x] = Find(uf[x]);
}
bool uset(int u,int v){
int tx = Find(u);
int ty = Find(v);
if(tx != ty) uf[tx] = ty;
return tx != ty;
}
bool check(int delta){
int i,j,cnt;
for(i = ; i <= n; ++i) uf[i] = i;
i = j = cnt = cost = ;
arc tmp;
while(i < tot1 || j < tot2){
if(e[][i].w + delta <= e[][j].w){
tmp = e[][i++];
tmp.w += delta;
}else tmp = e[][j++];
if(uset(tmp.u,tmp.v)){
cost += tmp.w;
if(!tmp.id) cnt++;
}
}
return cnt >= k;
}
int main() {
int u,v,w,id,cs = ;
while(~scanf("%d %d %d",&n,&m,&k)){
for(int i = tot1 = tot2 = ; i < m; ++i){
scanf("%d %d %d %d",&u,&v,&w,&id);
if(id) e[][tot2++] = arc(u,v,w,id);
else e[][tot1++] = arc(u,v,w,id);
}
e[][tot1].w = e[][tot2].w = INF;
sort(e[],e[]+tot1);
sort(e[],e[]+tot2);
int low = -,high = ,mid,delta;
while(low <= high){
mid = (low + high)>>;
if(check(mid)){
delta = mid;
low = mid + ;
}else high = mid - ;
}
check(delta);
printf("Case %d: %d\n",cs++,cost - delta*k);
}
return ;
}
 

HDU 4253 Two Famous Companies的更多相关文章

  1. hdu 4253 Two Famous Companies BZOJ 2654 tree

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

  2. HDOJ 4253 Two Famous Companies 二分+MST

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

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

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

  4. hdu 4255 A Famous Grid

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...

  5. HDU 4256 The Famous Clock

    The Famous Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 4251 The Famous ICPC Team Again 主席树

    The Famous ICPC Team Again Problem Description   When Mr. B, Mr. G and Mr. M were preparing for the ...

  7. HDU 4294 A Famous Equation(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4249 题目大意:给一个a+b=c的表达式,但是a.b.c中部分位的数字丢失,并用?代替,问有多少种方案 ...

  8. HDU 4248 A Famous Stone Collector 组合数学dp ****

    A Famous Stone Collector Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. HDU 4251 The Famous ICPC Team Again(划分树)

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

随机推荐

  1. A server is already running. Check tmp/pids/server.pid.

    A server is already running. Check  tmp/pids/server.pid. 把server.pid删除: 学习了: http://stackoverflow.co ...

  2. 使用SecueCRT在本地主机与远程主机之间交互文件

    名词解释 本地主机:执行SecueCRT,以管理远程Linux或Unix的机器(一般为Windows系统) 远程主机:被SecueCRT进行管理控制的机器(一般为Linux或Unix) 1.开启SFT ...

  3. hadoop分布式架构和设计

    引言 Hadoop分布式文件系统(HDFS)被设计成适合执行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有非常多共同点.但同一时候,它和其它的分布式文 ...

  4. exFAT格式

    买了一个64GB的T卡,发如今Windows XP上格式化."文件系统"仅仅有exFAT选项. 用这个exFAT格式化还失败了. 给XP打上KB955704补丁,能够用exFAT格 ...

  5. Android scrollTo() scrollBy() Scroller解说及应用

    版本号:1.0  日期:2014.6.17  2014.6.18 版权:© 2014 kince 转载注明出处   scrollTo() .scrollBy()及 Scroller在视图滑动中常常使用 ...

  6. Spring经常使用属性的注入及属性编辑器

    对于对象的注入,我们使用ref方式,能够指定注入的对象.以下看下对于基本类型的注入.以及当spring无法转换基本类型进行注入时,怎样编写一个相似转换器的东西来完毕注入. 一.基本类型的注入 以下写一 ...

  7. HDU 5654 xiaoxin and his watermelon candy 离线树状数组

    xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...

  8. [yueqian_scut]Android多点触控技术和应用框架

    Android多点触控技术跟Linux输入子系统紧密相关.本文将从应用的角度说明Android多点触控技术的接口和应用. 一.多点触控场景分析 网络上有关Android多点触控技术的文章多见于两点拉伸 ...

  9. php5.5安装笔记

    这次没想到本来很简单的php编译,没想到遇到那么多问题.再此记录一下. 1.php5.5编译安装主要有一个难点,就是GD库的问题,因为php5.5的GD库必须是2.1以上的版本哦 原来都是用的gd2. ...

  10. HTML5-1、标签

    本文只是自己学习HTML5时的一些笔记.希望自己能够学好HTML5. 如果有感兴趣的同学.可以互相学习. 我觉得HTML5在未来的开发中站主导地位. 下面开始学习HTML5. 还是从HTML5标签开始 ...