Module: 弗洛伊德算法


Problem

1/10

弗洛伊德:起点 (C++)

Theory Click to read/hide

Error

Problem

给定一个有向图,其边被分配了一些非负权重(长度)。求从顶点 s 到顶点 t 的最短路径的长度。
 
输入
第一行包含三个数字:图中的顶点个数N≤50,顶点s和t的个数。接下来是图的邻接矩阵,即N行,每行包含N个数。邻接矩阵第 i 行中的第 j 个数指定从第 i 个顶点到第 j 个顶点的边的长度。长度可以取 0 到 1000000 之间的任何值,数字 -1 表示没有对应的边。保证矩阵的主对角线上有零点。
 
输出
打印单个数字 –最小路径长度。如果路径不存在,打印-1。

例子 <头> <日># <正文>
输入 输出
1
3 1 2
0 -1 3
7 0 1
2 215 0
218
Write the program below
#include <vector>
#include <iostream>
#include <climits>
using namespace std;

const int inf = INT_MAX;

int main()
{
	int n, s, t;
	cin >> n >> s >> t;
	vector<vector<int> > d(n + 1, vector<int>(n + 1,inf));
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			int a;
			cin >> a;
			if (a != -1)
			{
				d[i + 1][j + 1] = a;
			}
		}
	}
	for (int k = 1; k <= n; ++k)
	{
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= n; ++j)
			{      
			}
		}
	}
	if (d[s][t] == inf)
	{
		cout << -1;
		return 0;
	}
	cout << d[s][t] << endl;

}      

     

Program check result

To check the solution of the problem, you need to register or log in!