Pagini recente » Cod sursa (job #1846592) | Cod sursa (job #1902470) | Cod sursa (job #1729570) | Cod sursa (job #720443) | Cod sursa (job #4908)
Cod sursa(job #4908)
#include <stdio.h>
#define FIN "pscnv.in"
#define FOUT "pscnv.out"
#define nmax 1001
struct nod {int vf, c;
nod *next;};
nod *q;
typedef nod *pnod;
pnod a[nmax], liste[nmax];
int d[nmax], s[nmax], i, j, k, n, m, x, y, cost;
int nstart, min, poz, nfin;
void addmuchie (int x, int y, int cost)
{
nod *p = new nod;
p -> c = cost;
p -> vf = y;
p -> next = a[x];
a[x] = p;
}
void add (int x, int y)
{
nod *p = new nod;
p -> vf = y;
p -> next = liste[x];
liste[x] = p;
}
int max (int x, int y)
{
if (x < y)
return y;
return x;
}
int
main ()
{
freopen (FIN, "rt", stdin);
freopen (FOUT, "wt", stdout);
scanf ("%d%d%d%d", &n, &m, &nstart, &nfin);
for (i = 1; i <= m; i++)
{
scanf ("%d%d%d", &x, &y, &cost);
addmuchie (x, y, cost);
}
for (i = 1; i <= n; i++)
if (i != nstart)
d[i] = 20001;
for (q = a[nstart]; q != NULL; q = q -> next)
{
d[q -> vf] = q -> c;
add (q -> c, q -> vf);
}
s[nstart] = 1;
for (k = 1; k < n; k++)
{
min = 0;
for (i = 1; i <= 1000; i++)
{ if (liste[i] != NULL)
for (q = liste[i]; q != NULL; q = q -> next)
if (d[q -> vf] == i && s[q -> vf] != 1)
{
min = i;
poz = q -> vf;
break;
}
if (min == i)
break;
}
s[poz] = 1;
for (q = a[poz]; q != NULL; q = q -> next)
if (d[q -> vf] > max (min, q -> c))
{
d[q -> vf] = max (min, q -> c);
add (max(min, q -> c), q -> vf);
}
}
printf ("%d\n", d[nfin]);
return 0;
}