Pagini recente »
Istoria paginii utilizator/ilincaoancea
|
Istoria paginii runda/romaniaecool2
|
Rating Alexandra Garban (AlexaGarban)
|
Monitorul de evaluare
|
Cod sursă (job #757087)
Cod sursă (job
#757087)
#include <bits/stdc++.h>
using namespace std;
class Edge {
public:
int x, y, cost;
Edge()
{
x=0;
y=0;
cost=0;
}
Edge(int x, int y, int cost)
{
this->x=x;
this->y=y;
this->cost=cost;
}
bool operator<(const Edge& a) const {
return cost < a.cost;
}
};
vector<int> parent, Rank;
int Find(int i) {
if (parent[i] == i)
return i;
return parent[i] = Find(parent[i]);
}
void Union(int x, int y) {
int xRoot = Find(x);
int yRoot = Find(y);
if (Rank[xRoot] < Rank[yRoot])
parent[xRoot] = yRoot;
else if (Rank[xRoot] > Rank[yRoot])
parent[yRoot] = xRoot;
else {
parent[xRoot] = yRoot;
Rank[yRoot]++;
}
}
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator>>(int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator>>(long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator>>(Edge &e) {
*this >> e.x >> e.y >> e.cost;
return *this;
}
~InParser() {
fclose(fin);
delete[] buff;
}
};
class OutParser {
private:
ofstream fout;
public:
OutParser(const string& filename) {
fout.open(filename);
}
void writeInt(int x) {
fout << x << '\n';
}
~OutParser() {
fout.close();
}
};
int kruskal(vector<Edge>& edges, int n) {
sort(edges.begin(), edges.end());
parent.resize(n + 1);
Rank.assign(n + 1, 0);
for (int i = 1; i <= n; ++i)
parent[i] = i;
int minCost = 0;
for (const Edge& e : edges) {
int x = e.x;
int y = e.y;
if (Find(x) != Find(y)) {
minCost += e.cost;
Union(x, y);
}
}
return minCost;
}
int main() {
InParser in("zapada.in");
OutParser out("zapada.out");
int n, m, k;
in >> n >> m >> k;
vector<Edge> edges;
for (int i = 0; i < m; ++i) {
Edge e;
in >> e;
edges.push_back(e);
}
parent.resize(n + 1);
Rank.assign(n + 1, 0);
int c0 = kruskal(edges, n);
out.writeInt(c0);
for (int i = 0; i < k; ++i) {
Edge e;
in >> e;
edges.push_back(e);
int ck = kruskal(edges, n);
out.writeInt(ck);
}
return 0;
}