Pagini recente » Cod sursa (job #361248) | Cod sursa (job #2190138) | Cod sursa (job #1012890) | Cod sursa (job #1256795) | Cod sursa (job #528638)
Cod sursa(job #528638)
//solutia cu dijkstra
#include <iostream>
#include <cstdio>
using namespace std;
const char iname[] = "pscnv.in";
const char oname[] = "pscnv.out";
const int nmax = 250005;
const int infinite= 2188989;
int n, m, a1, a2, x, y, c, i, j;
int father[nmax];
int len[nmax];
struct muchie
{
int nod1;
int nod2;
}muchii[1005][10000];
int find(int nod)
{
if(father[nod] == nod)
return nod;
father[nod] = find(father[nod]);
return father[nod];
}
void unite(int x, int y)
{
int f1 = find(x);
int f2 = find(y);
father[f1] = f2;
}
int main()
{
freopen(iname, "r", stdin);
freopen(oname, "w", stdout);
scanf("%d %d %d %d", &n, &m, &a1, &a2);
for(i = 1; i <= m; i ++)
{
scanf("%d %d %d", &x, &y, &c);
muchii[c][++len[c]].nod1 = x;
muchii[c][len[c]].nod2 = y;
}
a1 = 1, a2 = n;
for(i = 1; i <= n; i ++)
father[i] = i;
for(i = 1; i <= 1005; i ++)
for(j = 1; j <= len[i]; j ++)
{
if(father[muchii[i][j].nod1] != father[muchii[i][j].nod2])
unite(find(muchii[i][j].nod1), find(muchii[i][j].nod2));
if(find(a1) == find(a2))
{
printf("%d", i);
return 0;
}
}
return 0;
}