#include <stdio.h>
#include <bitset>
using namespace std;
#define MAX_INT 0x7fffffff
bitset<100100010> path;
int map[10010], processed[10010];
void init_single_source(int *map, int size, int source)
{
int i;
for (i=1 ; i<=size ; i++)
map[i] = MAX_INT;
map[source] = 0;
}
int extract_min(int *map, int size)
{
int i, min, min_index;
min = MAX_INT;
for (i=1 ; i<=size ; i++)
{
if (map[i] < min && 0 == processed[i])
{
min = map[i];
min_index = i;
}
}
if (min == MAX_INT)
return -1;
processed[min_index] = 1;
return min_index;
}
int main(int argc, char *argv[])
{
int n, m, c, a, b, l, s, t, i, min_index, j;
scanf("%d %d %d", &n, &m, &c);
for (i=0 ; i<m ; i++)
{
scanf("%d %d %d", &a, &b, &l);
if (l > c)
continue;
path.set(a * n + b);
}
init_single_source(map, n, a);
for (i=0 ; i<n ; i++)
{
min_index = extract_min(map, n);
if (-1 == min_index)
{
printf("Impossible\n");
return 0;
}
/* do release */
for (j=1 ; j<=n ; j++)
{
if (!path.test(min_index * n + j))
continue;
if (map[j] > map[min_index] + 1)
{
map[j] = map[min_index] + 1;
}
}
}
/* print out min path */
if (MAX_INT == map[b])
printf("Impossible\n");
else
printf("%d\n", map[b] + 1);
}
|