POJ 1847

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#define MAXN 105
#define MAXM 305
#define MOD 10000
#define inf 100000000
#define eps 1e-9
#define pi acos(-1.0)
typedef long long LL;
using namespace std;
int n, s, e, x, u, mat[MAXN][MAXN], dis[MAXN];
bool vis[MAXN];
void Dijkstra() {
memset(vis, false, sizeof(vis)); vis[s] = true;
for (int i = 1; i <= n; i++) 
dis[i] = mat[s][i];
dis[s] = 0;
for (int i = 1; i < n; i++) {
int k = -1;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (k == -1 || dis[j] < dis[k])) 
k = j;
}
if (k == -1) continue;
vis[k] = true;
for (int j = 1; j <= n; j++) {
if (!vis[j] && dis[k] + mat[k][j] < dis[j])
dis[j] = dis[k] + mat[k][j];
}
}
}
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d %d %d", &n, &s, &e);
for (int i = 1; i <= n; i++) 
for (int j = 1; j <= n; j++) 
mat[i][j] = inf;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
for (int j = 1; j <= x; j++) {
scanf("%d", &u);
(j == 1) ? mat[i][u] = 0 : mat[i][u] = 1;
}
}
Dijkstra();
if (dis[e] == inf) puts("-1");
else printf("%d\n", dis[e]);
    return 0;
}

posted on 2012-11-28 13:38  Sure_Yi  阅读(148)  评论(0编辑  收藏  举报

导航