Cod sursă (job #357784)

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

using namespace std;

const int NMAX = 10005;
const int MMAX = 100005;

short Father[NMAX];
long long  bigger;
long long P;
int countt;

struct edge
{
    short x,y;
    int price;
}Edge[MMAX];
long long Answer = 0;
int N,Last1,K;
int Last2;

inline bool cmp( const edge &a , const edge &b)
{
    return a.price < b.price;
}
inline void ScanData()
{
    scanf("%d%d%d", &N ,&Last1 , &K);

    for ( int i = 1; i <= Last1 ; ++i)
    {
        scanf("%hd%hd%d", &Edge[i].x , &Edge[i].y  , &Edge[i].price);
    }
    sort(Edge+1,Edge+Last1+1, cmp);

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

inline int Root(short Node)
{
    short  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;
    int CT = 0;
    for ( int i = 1 ; i <= Last1 && CT < N-1 ; ++i)
    {
        short Node1 = Edge[i].x;
        short Node2 = Edge[i].y;
        int Price = Edge[i].price;
      //cout << Node1 <<" "<< Node2 <<" "<<Price <<"\n";
        short Root1 = Root(Node1);
        short Root2 = Root(Node2);


        if( Root1 != Root2)
        {
            Unite(Root1,Root2);
            Answer+=Price;
            Last2 = i;
            CT++;
        }

        else Edge[i].price = (1<<30);
    }

    printf("%lld\n", Answer);


}
int main()
{
    freopen("zapada.in" , "r" ,stdin);
   freopen("zapada.out" , "w", stdout);

   ScanData();

   APM();

   for ( int i = 1; i <= K ; ++i)
   {
       int a,b,c;
       scanf("%d%d%d", &a,&b,&c);

       if( c >= Edge[Last2].price)
        printf("%lld\n", Answer);
       else
       {
           Last1 = Last2;
           Last1++;
           Edge[Last1].x = a;
           Edge[Last1].y = b;
           Edge[Last1].price = c;
           sort(Edge+1,Edge+Last1+1,cmp);
           APM();
       }
   }
   return 0;

}