-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path图_最短路径_弗洛伊德_(floyd)_.cpp
150 lines (120 loc) · 2.26 KB
/
图_最短路径_弗洛伊德_(floyd)_.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<stdio.h>
#include<iostream>
using namespace std;
#include<stack>
#include<queue>
#define Max_vexs 25
#define status int
#define Max_type 50
typedef struct
{
char vexs[Max_vexs];
status arcs[Max_vexs][Max_vexs];
status vexnum,arcnum;
}graph;
status createUDG(graph *G)
{
int i,j,w;
bool isDirected=1;
//printf("辺と点の数量を輸入してください:\n");
//scanf("%d %d",&G->vexnum,&G->arcnum);
G->vexnum=5;
G->arcnum=7;
for(i=1;i<G->vexnum+1;i++)
{
for(j=1;j<G->vexnum+1;j++)
{
if(i==j)
{
G->arcs[i][j]= 0;
}
else
{
G->arcs[i][j]= Max_type;
}
}
}
/*cout<<"请输入边(vi, vj)和权值w,三个变量空格分隔即可:"<<endl;
for(int k=1;k<G->arcnum+1;k++)
{
cin>>i>>j>>w;
G->arcs[i][j]=w;
if(not isDirected)
{
G->arcs[j][i]=w;
}
}*/
G->arcs[1][2]=5;
G->arcs[1][3]=3;
G->arcs[1][5]=8;
G->arcs[2][3]=2;
G->arcs[2][4]=1;
G->arcs[3][5]=4;
G->arcs[4][3]=1;
}
status printUDG(graph *G)
{
int i,j;
printf("辺と点の数量を輸出します:\n");
printf("%d %d\n",G->vexnum,G->arcnum);
//printf("インプート 頂点のデータ:\n");
printf("全て点の連絡の数字を輸出します:\n");
/*for(i=1;i<G->vexnum+1;i++)
{
printf("%5c",G->vexs[i]);
}*/
printf("\n");
for(i=1;i<G->vexnum+1;i++)
{
for(j=1;j<G->vexnum+1;j++)
{
printf("%5d",G->arcs[i][j]);
}
printf("\n");
}
}
void Dijkstar(graph *G,int u)
{
int A[G->vexnum+1][G->vexnum+1];
int i,j,k;
for(i=1;i<G->vexnum+1;i++)
{
for(j=1;j<G->vexnum+1;j++)
{
A[i][j]=G->arcs[i][j];
}
}
for(k=1;k<G->vexnum+1;k++)
{
for(i=1;i<G->vexnum+1;i++)
{
for(j=1;j<G->vexnum+1;j++)
{
printf("%5d , %5d + %5d \n",A[i][k],A[k][j],A[i][j]);
if(A[i][j]>A[i][k]+A[k][j])
{
//printf("%5d , %5d + %5d , %5d,%5d,%5d \n",A[i][j],A[i][k],A[k][j],i,j,k);
A[i][j]=A[i][k]+A[k][j];
}
//printf("\n");
}
}
}
printf("\n");
for(i=1;i<G->vexnum+1;i++)
{
for(j=1;j<G->vexnum+1;j++)
{
printf("%5d",A[i][j]);
}
printf("\n");
}
}
int main()
{
graph gh;
createUDG(&gh);
printUDG(&gh);
Dijkstar(&gh,1);
return 0;
}