-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP2323 [HNOI2006]公路修建问题.cpp
97 lines (90 loc) · 2.25 KB
/
P2323 [HNOI2006]公路修建问题.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10010;
int father[maxn];
struct Edge{
int from;
int to;
int dis;
int type;
/*1:I级道路, 2:II级道路, -1:当前方案准备打印的*/
};
Edge edges[maxn];
bool cmp(Edge a, Edge b) {
return a.dis<b.dis;
}
void init() {
memset(father, -1, sizeof(father));
}
int findRoot(int x) {
if(father[x]!=-1) {
return father[x] = findRoot(father[x]);
}
return x;
}
/*0:Fail 1:Success*/
int union_(int x, int y) {
int xRoot = findRoot(x);
int yRoot = findRoot(y);
if(xRoot==yRoot) {
return 0;
}
father[xRoot] = yRoot;
return 1;
}
set<pair<int, int> > ans;
/*第t对, 第p级*/
int main() {
int n, k, m;
cin>>n>>k>>m;
int a, b, I, II;
int cnt = 0;
for(int i=0; i<m-1; i++) {
cin>>a>>b>>I>>II;
edges[cnt] = ((Edge){a, b, I, 1});
edges[cnt+1] = ((Edge){a, b, II, 2});
cnt+=2;
}
sort(edges, edges+cnt, cmp);
init();
int cntOfI = 0, maxAns = 0;
for(int i=0; i<cnt; i++) {
/*循环找出前k个I级道路*/
if(cntOfI>=k) {
break;
}
Edge& now = edges[i];
if(now.type==1) {
if(union_(now.from, now.to) == 1) {
cntOfI++;
maxAns = max(maxAns, now.dis);
/*答案:因为分开放了,cnt是m的两倍,所以
*求原来的对数应+1/2
* */
ans.insert(make_pair((i+1)/2+1, now.type));
}
}
}
int tot = cntOfI;
for(int i=0; i<cnt; i++) {
/*循环找出剩下k~(n-1)个道路*/
if(tot>=n-1) {
break;
}
Edge& now = edges[i];
if(union_(now.from, now.to)==1) {
tot++;
maxAns = max(maxAns, now.dis);
// ans.insert(make_pair(i+1, now.type));
/*答案:因为分开放了,cnt是m的两倍,所以
*求原来的对数应+1/2
* */
ans.insert(make_pair((i+1)/2+1, now.type));
}
}
cout<<maxAns<<endl;
for(set<pair<int, int> >::iterator it = ans.begin(); it!=ans.end(); it++) {
cout<<(*it).first<<" "<<(*it).second<<endl;
}
return 0;
}