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. JAVAEE网上商城项目总结

    发送邮件实现(使用QQ邮箱发送到指定邮箱) 需要的jar 邮件发送类代码: package util; import java.util.Properties; import javax.mail.A ...

  2. 模仿学习小游戏外星人入侵-Python学习,体会“函数”编程

    游戏类如下: # !/usr/bin/python # -*- coding:utf-8 -*- """ Author :ZFH File :alien.py Softw ...

  3. BA-siemens-insight在win7下如何配置opc接口

    一.运行环境:win7(OPC接口在win_xp下配置需安装插件,不好意思没搞定,现在只有win7系统32位下的教程了) 由于OPC(OLE for Process Control)建立在Micros ...

  4. Vijos——T 1016 北京2008的挂钟 || 洛谷—— P1213 时钟

    https://www.luogu.org/problem/show?pid=1213 题目描述 考虑将如此安排在一个 3 x 3 行列中的九个时钟: 目标要找一个最小的移动顺序将所有的指针指向12点 ...

  5. 洛谷——P2657 低头一族

    https://www.luogu.org/problem/show?pid=2657 题目描述 一群青年人排成一队,用手机互相聊天. 每个人的手机有一个信号接收指标,第i个人的接收指标设为v[i]. ...

  6. Android Activity组件的启动过程

    0.总图 1.总图中的第一步,Laucher主线程向ActivityManagerService进程发出START_ACTIVITY_TRANSACTION 如图:第一步 ~/Android/fram ...

  7. android:layout_gravity 和android:gravit的区别?

    Android:layout_gravity 和android:gravit的区别? android:gravity是调整元素本身的内容或元素包含的子元素显示的位置,默认是显示在左侧 android: ...

  8. windows2003安装

    产品密钥JCDPY-8M2V9-BR862-KH9XB-HJ3HMiis的i386文件夹http://pan.baidu.com/s/1dD0EY6twindows2003的iso映像http://p ...

  9. Bata版本

    一.团队成员 1)冯鹏(组长) 201731062617 2)鲜泽   201731062612 3)李家豪 201731062614 4)郭经伟 201731062615 5)程前勇 2017310 ...

  10. VS头部自动注释

    1.找你的vs安装目录, 比如我的是在D盘D:\Program Files\Microsoft\VS2013\Common7\IDE 2.然后点击右上角的 搜索. 搜索Class.cs文件 3.把里面 ...