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