看一眼题目,求商店1到赛场n的最短距离,第一感觉Floyd,看一眼数据,100以内,OK安排上。
#include <。iostream>(方便看,加。)
#include <。iomanip>
using namespace std;
const int MAX = 99999999;
int e[101][101];
int cs;
int main()
{
int m,n;
int a,b,c;
while (cin >> n >> m)
{
if (n == 0 && m == 0)
break;
cs=0;
for (int i = 0; i < 101; i++)
{
for (int j = 0; j < 101; j++)
{
e[i][j] = MAX;
}
}
for (int i = 1; i <= m; i++)
{
cin >> a >> b >> c;
e[a][b] = e[b][a] = c;
}
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (e[i][k] == MAX || e[k][j] == MAX)
continue;
if (e[i][j] == MAX || e[i][k] + e[k][j] < e[i][j])
e[i][j] = e[i][k] + e[k][j];
}
}
}
cout << e[1][n] << endl;
}
return 0;
}
图论专题A
最新推荐文章于 2024-03-21 10:50:13 发布