-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathonline.cpp
69 lines (68 loc) · 1.23 KB
/
online.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
# include <fstream>
# include <vector>
# include <algorithm>
using namespace std;
ifstream f("online.in");
ofstream g("online.out");
struct elem
{
int x,y,cost;
}E,v[10005];
bool cmp (elem x, elem y)
{
if (x.cost>=y.cost) return 0;
else return 1;
}
int i,j,n,m,x,y,K;
int R[205],H[205];
int radacina (int k)
{
while (k!=R[k])
k=R[k];
return k;
}
void APM (int nr)
{
int i,ok=0,VV,cost=0,Rx,Ry;
if (nr==1) sort (v+1, v+m+1, cmp);
else {
for (i=n; i>=1; --i)
{
R[i]=i; H[i]=1;
if (v[i].cost<v[i-1].cost) swap(v[i],v[i-1]);
}
}
VV=0;
for (i=1; i<=m; ++i)
{
Rx=radacina(v[i].x);
Ry=radacina(v[i].y);
if (Rx!=Ry)
{
v[++VV]=v[i];
cost+=v[i].cost;
if (H[Rx]>=H[Ry]) R[Ry]=Rx, H[Rx]+=H[Ry];
else R[Rx]=Ry, H[Ry]+=H[Rx];
}
}
m=n;
g<<cost<<"\n";
}
int main ()
{
f>>n>>m;
for (i=1; i<=m; ++i)
{
if (i<=n) R[i]=i, H[i]=1;
f>>v[i].x>>v[i].y>>v[i].cost;
}
APM (1);
f>>K;
for (i=2; i<=K+1; ++i)
{
f>>E.x>>E.y>>E.cost;
v[n]=E;
APM(K);
}
return 0;
}