Cod sursa(job #778400)

Utilizator visanrVisan Radu visanr Data 14 august 2012 17:11:35
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;


#define from first
#define to second
#define pb push_back
#define mp make_pair
#define max 250010
#define Kmax 1001
int N, M, X, Y, a, b, c, T[max];
vector<pair<int, int> > G[Kmax];


void Unite(int X, int Y)
{
     T[X] = Y;
}

int Search(int X)
{
    if(X != T[X]) T[X] = Search(T[X]);
    return T[X];
}

int main()
{
    freopen("pscnv.in", "r", stdin);
    freopen("pscnv.out", "w", stdout);
    int i;
    vector<pair<int, int> > :: iterator it;
    scanf("%i %i %i %i", &N, &M, &X, &Y);
    for(i = 1; i <= N; ++i) T[i] = i;
    for(i = 1; i <= M; i++)
    {
          scanf("%i %i %i", &a, &b, &c);
          G[c].pb(mp(a, b));
    }
    for(i = 1; i < Kmax; i++)
    {
          for(it = G[i].begin(); it != G[i].end(); ++it)
                 if(Search(it -> from) != Search(it -> to))
                              Unite(Search(it -> from), Search(it -> to));
          if(Search(X) == Search(Y))
          {
                       printf("%i\n", i);
                       return 0;
          }
    }
    return 0;
}