HDU 4253 two famous companies

Source: Internet
Author: User
Two famous companiestime limit: 15000 msmemory limit: 32768 kbthis 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. obviusly, 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 Co Mpanies and calculate the minimum cost needed to connect all the cities. mr. B knows that N-1 cables shoshould be built in order to connect all N cities of China. for some honorable reason, Mr. B shoshould 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 shoshould be built and the minimum cost to build them. you may ass UME that the solution always exists. inputeach 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,! = B, 1 <= C <= 100, X in {} indicating the pair of cities this cable will connect, the cost to build this cable and the company this cable Plan belongs. X = 0 denotes that the cable Plan belongs to China Telecom and x = 1 denotes that the cable plan is from China Unicom. outputfor each test case, display the case number and the minimum cost of the cable building. sample Input
2 2 10 1 1 10 1 2 02 2 00 1 1 10 1 2 0
Sample output
Case 1: 2Case 2: 1
HintIn 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. sourcefudan local programming contest 2012: Minimum Spanning Tree + second. Very good question... Add a value to the edge of a company until the number of edges specified by the company is obtained.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 100010;18 struct arc{19     int u,v,w,id;20     arc(int uu = 0,int vv = 0,int ww = 0,int iid = 0){21         u = uu;22         v = vv;23         w = ww;24         id = iid;25     }26     bool operator<(const arc &tmp) const{27         return w < tmp.w;28     }29 };30 arc e[2][maxn];31 int uf[maxn],n,m,k,tot1,tot2,cost;32 int Find(int x){33     if(x == uf[x]) return x;34     return uf[x] = Find(uf[x]);35 }36 bool uset(int u,int v){37     int tx = Find(u);38     int ty = Find(v);39     if(tx != ty) uf[tx] = ty;40     return tx != ty;41 }42 bool check(int delta){43     int i,j,cnt;44     for(i = 0; i <= n; ++i) uf[i] = i;45     i = j = cnt = cost = 0;46     arc tmp;47     while(i < tot1 || j < tot2){48         if(e[0][i].w + delta <= e[1][j].w){49             tmp = e[0][i++];50             tmp.w += delta;51         }else tmp = e[1][j++];52         if(uset(tmp.u,tmp.v)){53             cost += tmp.w;54             if(!tmp.id) cnt++;55         }56     }57     return cnt >= k;58 }59 int main() {60     int u,v,w,id,cs = 1;61     while(~scanf("%d %d %d",&n,&m,&k)){62         for(int i = tot1 = tot2 = 0; i < m; ++i){63             scanf("%d %d %d %d",&u,&v,&w,&id);64             if(id) e[1][tot2++] = arc(u,v,w,id);65             else e[0][tot1++] = arc(u,v,w,id);66         }67         e[0][tot1].w = e[1][tot2].w = INF;68         sort(e[0],e[0]+tot1);69         sort(e[1],e[1]+tot2);70         int low = -100,high = 100,mid,delta;71         while(low <= high){72             mid = (low + high)>>1;73             if(check(mid)){74                 delta = mid;75                 low = mid + 1;76             }else high = mid - 1;77         }78         check(delta);79         printf("Case %d: %d\n",cs++,cost - delta*k);80     }81     return 0;82 }
View code

 

 

HDU 4253 two famous companies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.