Codevs 3000 Highway construction problem

Source: Internet
Author: User

Title Description 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.

Enter a description Input Description

The first line has three number n,k,m, 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 M-line, each line has 4 positive integers a,b,c1,c2 means that the road between the attraction A and B can be repaired, if the first road, you need to C1 the cost, if the two-level highway, you need to C2 cost.

Output description Output Description

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

Sample input Sample Input
4 2 51 2 6 51 3 3 12 3 9 42 4 6 13 4 4 2
Sample output Sample Output

4

Data range and Tips Data Size & Hint

1≤n≤10000,0≤k≤n-1,n-1≤m≤20000,1≤a,b≤n,a≠b,1≤c2≤c1≤30000

Thinking of solving problems

Standard: After two minutes, the output is judged by 01 trees.
But because the data is too water, greedy can: the C1 sort, then take the K-bar, then the C2 sort, take the remaining side, run one side Kruskal update maximum value, you can AC

1  ProgramT5;2 typeTre=Record3 L,r,c1,c2:longint;4 End;5 var6Tr:Array[1..20000] ofTre;7so[Array[1..10000] ofLongint;8 M,n,i,max,sum,k:longint;9 functionRoot (X:longint): Longint;Ten begin One     ifRo[x]=x Thenexit (x); Aroot:=root (Ro[x]); -ro[x]:=Root; - exit (root); the End; -   procedureSort1 (l,r:longint); -       var - I,j,x:longint; + Y:tre; -       begin +i:=l; Aj:=R; atx:=tr[(L+r)Div 2].c1; -          Repeat -             whileTr[i].c1<x Do - Inc (i); -             whileX<tr[j].c1 Do - Dec (j); in            if  not(I&GT;J) Then -              begin toy:=Tr[i]; +tr[i]:=Tr[j]; -tr[j]:=y; the Inc (i); *j:=j-1; $              End;Panax Notoginseng          untilI>J; -          ifL<j Then the Sort1 (l,j); +          ifI<r Then A Sort1 (i,r); the       End; +       procedureSort2 (l,r:longint); -       var $ I,j,x:longint; $ Y:tre; -       begin -i:=l; thej:=R; -x:=tr[(L+r)Div 2].c2;Wuyi          Repeat the             whileTr[i].c2<x Do - Inc (i); Wu             whileX<tr[j].c2 Do - Dec (j); About            if  not(I&GT;J) Then $              begin -y:=Tr[i]; -tr[i]:=Tr[j]; -tr[j]:=y; A Inc (i); +j:=j-1; the              End; -          untilI>J; $          ifL<j Then the Sort2 (l,j); the          ifI<r Then the Sort2 (i,r); the       End; - begin in read (n,k,m); the      fori:=1  toN Doro[i]:=i; the      fori:=1  toM Do About     begin the read (TR[I].L,TR[I].R,TR[I].C1,TR[I].C2); the     End; theSort1 (1, m); +sum:=0; -      fori:=1  toM Do the     beginBayi         ifRoot (TR[I].L) <>root (TR[I].R) Then the         begin the Inc (SUM); -Ro[root (TR[I].L)]:=root (TR[I].R); -             ifMax<tr[i].c1 Thenmax:=tr[i].c1; the             ifSum=k ThenBreak ; the         End; the     End; theSort2 (1, m); -      fori:=1  toM Do the     begin the         ifRoot (TR[I].L) <>root (TR[I].R) Then the         begin94Ro[root (TR[I].L)]:=root (TR[I].R); the             ifMax<tr[i].c2 Thenmax:=tr[i].c2; the         End; the     End;98 Writeln (max); About End.

Codevs 3000 Highway construction problem

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.