-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathurgenta.cpp
58 lines (55 loc) · 1.13 KB
/
urgenta.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
# include <cstdio>
# include <algorithm>
using namespace std;
struct elem
{
int x,y,cost;
}v[35000],sol[35000];
int i,j,n,m,k,x,y,Rx,Ry,grupe,VV,cost;
int R[300], H[300];
bool cmp (elem x, elem y)
{
if (x.cost>=y.cost) return 0;
else return 1;
}
int radacina (int k)
{
while (k!=R[k])
k=R[k];
return k;
}
void APM ()
{
for (i=1; i<=n; ++i)
R[i]=i, H[i]=1;
sort (v+1, v+m+1, cmp);
grupe=n;
for (i=1; i<=m; ++i)
{
Rx=radacina(v[i].x);
Ry=radacina(v[i].y);
if (Rx!=Ry && grupe!=k)
{
--grupe;
if (H[Rx]>H[Ry]) R[Ry]=Rx, H[Rx]+=H[Ry];
else R[Rx]=Ry, H[Ry]+=H[Rx];
}
else {
cost+=v[i].cost;
sol[++VV]=v[i];
}
}
}
int main ()
{
freopen ("urgenta.in", "r", stdin);
freopen ("urgenta.out", "w", stdout);
scanf ("%d%d%d", &n, &m, &k);
for (i=1; i<=m; ++i)
scanf ("%d%d%d", &v[i].x, &v[i].y, &v[i].cost);
APM();
printf ("%d\n%d\n", cost, VV);
for (i=1; i<=VV; ++i)
printf ("%d %d\n", sol[i].x, sol[i].y);
return 0;
}