-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path多源点最短路径问题.cpp
More file actions
31 lines (30 loc) · 894 Bytes
/
多源点最短路径问题.cpp
File metadata and controls
31 lines (30 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<bits/stdc++.h>
using namespace std;
const int MAX = 100;
const int n = 3; //假设图中最多顶点个数
void Floyd(int arc[n][n], int dist[n][n]);
int main()
{
int arc[n][n]= {{0,4,11},{6,0,2},{3,MAX,0}};
int dist[n][n];
Floyd(arc,dist);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
cout<<dist[i][j]<<" ";
cout<<endl;
}
return 0;
}
void Floyd(int arc[n][n], int dist[n][n])
{
int i, j, k;
for (i = 0; i < n; i++) //初始化矩阵dist
for (j = 0; j < n; j++)
dist[i][j] = arc[i][j];
for (k = 0; k < n; k++) //进行n次迭代
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}