松弛那儿写错了一开始没看出来
把拨开关可达视为路径长度1,不用拨开关视为路径长度0,最短路搜索
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;
///
///宏定义
const int INF = 100000000;
const int MAXN = 110;
const int maxn = MAXN;
int N, A, B;
int graph[MAXN][MAXN];
int dist[MAXN];
bool vis[MAXN];
int dij()
{
int i, j;
memset(vis, false, sizeof(vis));
for(i = 1; i <= N; ++i)
dist[i] = INF;
for(i = 1; i <= N; ++i)
dist[i] = graph[A][i];
//初始条件
dist[A] = 0;
vis[A] = true;
//贪心选择,松弛N - 1次
for (i = 2; i <= N; ++i)
{
int minn = INF;
int minpos = -1;
for (j = 1; j <= N; ++j)
{
if (!vis[j] && dist[j] < minn)
{
minpos = j;
minn = dist[j];
}
}
if (minpos == -1)
continue;
vis[minpos] = true;
//松弛
for (j = 1; j <= N; ++j)
{
if (!vis[j] && dist[minpos] + graph[minpos][j] < dist[j])
{
dist[j] = dist[minpos] + graph[minpos][j];
}
}
}
if (dist[B] == INF)
return -1;
else
return dist[B];
}
int main()
{
///
int i, j;
while (cin >> N >> A >> B)
{
for (i = 1; i <= N; ++i)
{
for (j = 1; j <= N; ++j)
{
graph[i][j] = INF;
}
}
for (i = 1; i <= N; ++i)
{
int numbers;
cin >> numbers;
for (j = 0; j < numbers; ++j)
{
int to;
cin >> to;
if (j == 0)
graph[i][to] = 0;
else
graph[i][to] = 1;
}
}
int ans = dij();
cout << ans << endl;
}
///
return 0;
}