CODEVS 3000公路修建问题

题目描述 Description

OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多。然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕。所以,OIER Association组织成立了,旨在建立OI island的交通系统。 OI island有n个旅游景点,不妨将它们从1到n标号。现在,OIER Association需要修公路将这些景点连接起来。一条公路连接两个景点。公路有,不妨称它们为一级公路和二级公路。一级公路上的车速快,但是修路的花费要大一些。 OIER Association打算修n-1条公路将这些景点连接起来(使得任意两个景点之间都会有一条路径)。为了保证公路系统的效率, OIER Association希望在这n-1条公路之中,至少有k条(0≤k≤n-1)一级公路。OIER Association也不希望为一条公路花费的钱。所以,他们希望在满足上述条件的情况下,花费最多的一条公路的花费尽可能的少。 而你的任务就是,在给定一些可能修建的公路的情况下,选择n-1条公路,满足上面的条件。

输入描述 Input Description

第一行有三个数n,k,m,这些数之间用空格分开。 N和k如前所述,m表示有m对景点之间可以修公路。以下的m行,每一行有4个正整数a,b,c1,c2表示在景点a与b 之间可以修公路,如果修一级公路,则需要c1的花费,如果修二级公路,则需要c2的花费。

输出描述 Output Description

一个数据,表示花费最大的公路的花费。

样例输入 Sample Input
4 2 5
1 2 6 5
1 3 3 1
2 3 9 4
2 4 6 1
3 4 4 2
样例输出 Sample Output

4

数据范围及提示 Data Size & Hint

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

解题思路

标程:二分长度之后用01树建树判断输出
但由于数据太水,贪心即可:对c1排序,然后取k条边,再对c2排序,取剩余边,跑一边克鲁斯卡尔更新最大值,即可AC

 1 program t5;
 2 type tre=record
 3 l,r,c1,c2:longint;
 4 end;
 5 var
 6 tr:array[1..20000] of tre;
 7 ro:array[1..10000] of longint;
 8 m,n,i,max,sum,k:longint;
 9 function root(x:longint):longint;
10 begin
11     if ro[x]=x then exit(x);
12     root:=root(ro[x]);
13     ro[x]:=root;
14     exit(root);
15 end;
16   procedure sort1(l,r: longint);
17       var
18          i,j,x: longint;
19          y:tre;
20       begin
21          i:=l;
22          j:=r;
23          x:=tr[(l+r) div 2].c1;
24          repeat
25            while tr[i].c1<x do
26             inc(i);
27            while x<tr[j].c1 do
28             dec(j);
29            if not(i>j) then
30              begin
31                 y:=tr[i];
32                 tr[i]:=tr[j];
33                 tr[j]:=y;
34                 inc(i);
35                 j:=j-1;
36              end;
37          until i>j;
38          if l<j then
39            sort1(l,j);
40          if i<r then
41            sort1(i,r);
42       end;
43       procedure sort2(l,r: longint);
44       var
45          i,j,x: longint;
46          y:tre;
47       begin
48          i:=l;
49          j:=r;
50          x:=tr[(l+r) div 2].c2;
51          repeat
52            while tr[i].c2<x do
53             inc(i);
54            while x<tr[j].c2 do
55             dec(j);
56            if not(i>j) then
57              begin
58                 y:=tr[i];
59                 tr[i]:=tr[j];
60                 tr[j]:=y;
61                 inc(i);
62                 j:=j-1;
63              end;
64          until i>j;
65          if l<j then
66            sort2(l,j);
67          if i<r then
68            sort2(i,r);
69       end;
70 begin
71     read(n,k,m);
72     for i:=1 to n do ro[i]:=i;
73     for i:=1 to m do
74     begin
75         read(tr[i].l,tr[i].r,tr[i].c1,tr[i].c2);
76     end;
77     sort1(1,m);
78     sum:=0;
79     for i:=1 to m do
80     begin
81         if root(tr[i].l)<>root(tr[i].r) then
82         begin
83             inc(sum);
84             ro[root(tr[i].l)]:=root(tr[i].r);
85             if max<tr[i].c1 then max:=tr[i].c1;
86             if sum=k then break;
87         end;
88     end;
89     sort2(1,m);
90     for i:=1 to m do
91     begin
92         if root(tr[i].l)<>root(tr[i].r) then
93         begin
94             ro[root(tr[i].l)]:=root(tr[i].r);
95             if max<tr[i].c2 then max:=tr[i].c2;
96         end;
97     end;
98     writeln(max);
99 end.
原文地址:https://www.cnblogs.com/wuminyan/p/4738264.html