求助,floyd为什么过不了
查看原帖
求助,floyd为什么过不了
942242
assas61楼主2024/4/1 19:44

floyd为什么只能过135这三个点

const int N = 2005;
int dist[N][N];
int n, m;
void floyd()
{
	for(int mid=1;mid<=n;mid++)
		for(int i=1;i<=n;i++)
			for (int j = 1; j <= n; j++)
			{
				if (mid != i && i != j && mid != j)
					dist[i][j] = min(dist[i][j], dist[i][mid] + dist[mid][j]);
			}
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			dist[i][j] = 1e9;
	for (int i = 1; i <= m; i++)
	{
		int a, b;
		cin >> a >> b;
		dist[a][b] = 1;
		dist[b][a] = 1;
	}
	floyd();
	int s, e;
	cin >> s >> e;
	if (dist[s][e] >1e8)
		cout << -1;
	else
	cout << dist[s][e] - 1;
	return 0;
}
2024/4/1 19:44
加载中...