HDU_1874 畅通工程续(Floyd)

  求图中任意两点间的最短路径,个人感觉用Floyd比较好,有人说这道题可以用SPFA,目前还没有这方面的思路,先把Floyd的做法贴上,以后再补

Floyd code:

#include <iostream>
#include
<cstdio>
using namespace std;
const int inf = 100000000;
const int N = 208;
int dis[N][N];
int n;
void Floyd()
{
int i, j, k;
for(k = 0; k < n; k++)
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
if(dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j]
= dis[i][k] + dis[k][j];
}

int main()
{
int m, a, b, c, i, j;
//freopen("data.in", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
{
dis[i][j]
= inf;
if(i == j)
dis[i][j]
= 0;
}
while(m--)
{
scanf(
"%d%d%d", &a, &b, &c);
if(c < dis[a][b])
dis[a][b]
= dis[b][a] = c;
}
scanf(
"%d%d", &a, &b);Floyd();
if(dis[a][b] < inf)
printf(
"%d\n", dis[a][b]);
else
printf(
"-1\n");
}
return 0;
}

你可能感兴趣的:(floyd)