Cod sursă (job #357021)

Utilizator avatar cozmacatalin Cozma Catalin cozmacatalin IP ascuns
Problemă Zapada (clasele 11 și 12) Compilator cpp | 2,04 kb
Rundă Arhiva de probleme Status evaluat
Dată 27 feb. 2018 07:09:18 Scor 40
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 10005;
const int MMAX = 100005;
int big;
bitset < NMAX > Good;
short Father[NMAX];
int countt;

 int Answer = 0;

 set < pair < short , pair < short , short > > > S;
int N,M,K;
inline void ScanData()
{
    scanf("%d%d%d", &N ,&M , &K);

    for ( int i = 1; i <= M ; ++i)
    {
        int x,y,price;
        scanf("%d%d%d", &x , &y , &price);
        S.insert(make_pair(price,make_pair(x,y)));
    }

}
inline void Unite(int x, int y)
{
    Father[y] = x;
}

inline int Root(int Node)
{
    int R = Node;
    while(Father[R] > 0)
        R = Father[R];

    while(Father[Node] > 0)
    {
        Father[Node] = R;
        Node = R;
    }

    return Node;
}

inline void APM()
{
    memset(Father , 0 , sizeof Father);
    Answer = 0;
    big = 0;

    multiset < int > :: iterator it;
    for ( auto it : S)
    {
        int Node1 = it.second.first;
        int Node2 = it.second.second;;
        int Price = it.first;
        //cout << Node1 <<" " << Node2 <<" " << Price <<"\n";

        int Root1 = Root(Node1);
        int Root2 = Root(Node2);
        if(Price == SHRT_MAX)
            break;

        if( Root1 != Root2 && Price != SHRT_MAX)
        {
            Unite(Root1,Root2);
            Answer+=Price;
            if(it.first > big)
                big = it.first;
        }
           else {
                it.first = SHRT_MAX;
                //cout << it.first <<"\n";
           }
    }
    //cout << endl;


    printf("%d\n" , Answer);
}
int main()
{
    freopen("zapada.in" , "r" ,stdin);
   freopen("zapada.out" , "w", stdout);

   ScanData();

   APM();

   for ( int i = 1 ; i <= K ; ++i)
   {
       int x,y,price;
        scanf("%d%d%d", &x , &y , &price);
            if( price > big)
                 printf("%d\n" , Answer);
                 //int nufac=0;
        else
        {

        S.insert(make_pair(price,make_pair(x,y)));
       APM();

        }

   }
   return 0;
}