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 ;
}