-
Notifications
You must be signed in to change notification settings - Fork 1
/
最短路径.cpp
68 lines (64 loc) · 826 Bytes
/
最短路径.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
#include<iostream>
#include<vector>
#define MAX 10000
using namespace std;
int D[50][50],path[50][50];
int vnum;
int main()
{
void build();
void solve();
void print();
build();
solve();
print();
return 0;
}
void build()
{
cin>>vnum;
for(int i=0;i<vnum;i++)
{
for(int j=0;j<vnum;j++)
{
cin>>D[i][j];
if(D[i][j]==MAX)
path[i][j]=j;
else
path[i][j]=j;
}
}
}
void solve()
{
for(int k=0;k<vnum;k++)
for(int i=0;i<vnum;i++)
for(int j=0;j<vnum;j++)
{
if(D[i][k]+D[k][j]<D[i][j])
{
D[i][j]=D[i][k]+D[k][j];
path[i][j]=path[i][k];
}
}
}
void print()
{
int m;
cin>>m;
while(m--)
{
int tx,ty,k;
cin>>tx>>ty;
cout<<D[tx][ty]<<endl;
k=path[tx][ty];
cout<<tx<<' ';
while(k!=ty)
{
cout<<k<<' ';
k=path[k][ty];
}
cout<<ty;
cout<<endl;
}
}