Bzoj 1196: [HNOI2006] Road construction problems (MST)

Source: Internet
Author: User

Water problem ...

It is easy to find that the most expensive is to seek MST

Split each edge into one level, level two two, then run MST. When running MST, add K first-level road to ensure that test instructions is met and then run the normal MST.

------------------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#define REP (i, n) for (int i = 0; i < n; ++i)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const INT MAXN = 10000 + 5;int n, K;inline int Read () {char C = getchar ();For (;! isdigit (c); c = GetChar ());int ans = 0;For (; IsDigit (c); c = GetChar ())ans = ans * + C-' 0 ';return ans;}struct Edge {int U, V, W;bool T;//T = = 0-SecondEdge () {}Edge (int _u, int _v, int _w, int _t):u (_u), V (_v), W (_w), T (_t) {}BOOL operator < (const edge &e) Const {Return W < E.W;}};Edge e[MAXN << 2];int cnt = 0;int p[MAXN];void init () {n = read ();k = read ();int m = read ();While (--m) {int u = Read ()-1, v = Read ()-1, C1 = Read (), C2 = Read ();e[cnt++] = Edge (U, V, C1, 1);e[cnt++] = Edge (U, V, c2, 0);}Rep (i, N) p[i] = i;}int find (int x) {return x = = p[x]? x:p[x] = find (p[x]);}void Work () {int ans = 0;sort (E, E + CNT);Rep (i, CNT) if (e[i].t) {edge* e = e + i;int a = find (e-u), B = Find (e-v);if (A! = b) {p[A] = b;ans = max (e-W, ans);if (!--k) break;}}Rep (i, CNT) {edge* e = e + i;int a = find (e-u), B = Find (e-v);if (A! = b)p[A] = B,ans = max (ans, E-W);}cout << ans << "\ n";}int main () {freopen ("test.in", "R", stdin);init ();Work ();return 0;}

------------------------------------------------------------------------------------

1196: [HNOI2006] Highway construction Time limit: ten Sec Memory Limit: 162 MB
Submit: 1345 Solved: 750
[Submit] [Status] [Discuss] Description

OI Island is a very beautiful islands, since the development, come here to travel a lot of people. However, because the island has just been developed soon, the traffic situation there is still very bad. Therefore, the Oier Association organization was established to establish the transport system of Oi Island. There are n tourist attractions in OI Island, which may be labeled from 1 to N. Now, Oier Association need to repair roads to connect these attractions together. A highway connects two attractions. Highways, which may be called first-class highways and two-level highways. The speed on the first road is fast, but the cost of repairing the road is much bigger. Oier Association intends to fix n-1 roads to connect these attractions (there will be a path between any of the two attractions). In order to ensure the efficiency of the highway system, Oier Association hopes to have at least K (0≤k≤n-1)-level highways in this N-1 road. Oier Association also don't want to spend money on a road. So, they want to spend as little as possible on the most expensive road in the case of meeting the above conditions. And your job is to choose the N-1 Highway to meet the above conditions, given some of the possible roads to be built.

Input

The first line has three number n (1≤n≤10000), K (0≤k≤n-1), M (n-1≤m≤20000), and these numbers are separated by a space. N and K as stated above, m indicates that there is a road between the sites where m can be repaired. The following line of M-1 , each line has 4 positive integers a,b,c1,c2 (1≤a,b≤n,a≠b,1≤c2≤c1≤30000) means that between attractions A and B can be repaired road, if the first road, it will require the cost of C1, if the two-grade highway, The cost of C2 is required.

Output

A data that represents the cost of the most expensive road.

Sample Input10 4 20
3 9 6 3
1 3 4 1
5 3 10 2
8 9 8 7
6 8 8 3
7 1 3 2
4 9 9 5
10 8 9 1
2 6 9 1
6 7 9 8
2 6 2 1
3 8 9 5
3 2 9 6
1 6 10 3
5 6 3 1
2 7 6 1
7 8 6 2
10 9 2 1
7 1 10 2Sample Output5HINT

Source

Bzoj 1196: [HNOI2006] Road construction problems (MST)

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.