Cod sursă (job #625726)

Utilizator avatar MihaiBratu Mihai-Alexandru Bratu MihaiBratu IP ascuns
Problemă Zapada (clasele 11 și 12) Compilator cpp-32 | 1,14 kb
Rundă cex_gj11_12 Status evaluat
Dată 16 ian. 2022 16:26:28 Scor 40
#include <fstream>
#include <vector>
#include <map>
using namespace std;
multimap <int, pair <int, int>> pam;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
int n, m, k, i, j, x, y, z;
vector <int> viz;
int snow()
{
    viz.resize(1);
    viz.resize(n + 1);
    int ct = 0;
    map <int, pair <int, int>>::iterator it;
    it = pam.begin();
    ct = (*it).first;
    x = (*it).second.first;
    y = (*it).second.second;
    viz[x] = viz[y] = 1;
    for (int q = 2; q < n; ++q)
    {
        it = pam.begin();
        while (viz[(*it).second.first] == viz[(*it).second.second]) it++;
        if (viz[(*it).second.first])
            viz[(*it).second.second] = 1;
        else viz[(*it).second.first] = 1;
        ct += (*it).first;
    }
    return ct;
}
int main()
{
    fin >> n >> m >> k;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y >> z;
        pam.insert(make_pair(z, make_pair(x, y)));
    }
    fout << snow() << "\n";
    for (i = 1; i <= k; i++)
    {
        fin >> x >> y >> z;
        pam.insert(make_pair(z, make_pair(x, y)));
        fout << snow() << "\n";
    }
    return 0;
}