-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloyddWarshall.cpp
More file actions
89 lines (83 loc) · 1.55 KB
/
Copy pathFloyddWarshall.cpp
File metadata and controls
89 lines (83 loc) · 1.55 KB
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
//https://codeforces.com/contest/1204/problem/C
//https://codeforces.com/contest/295/problem/B
//https://codeforces.com/contest/25/problem/C
//https://codeforces.com/contest/33/problem/B
//https://codeforces.com/contest/1196/problem/F
//FLYOD WARSHALL. distance[i][j] is distance between node i and j;
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
int n;
cin>>n;
int graph[n][n];
for(int i=0;i<n;i++){
string s;
cin>>s;
for(int j=0;j<n;j++){
graph[i][j]=s[j]-'0';
}
}
int m;
cin>>m;
int a[m];
//cout<<"abc"<<endl;
for(int i=0;i<m;i++){
int temp;
cin>>temp;
temp--;
a[i]=temp;
}
int distance[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
distance[i][j]=1000000;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(graph[i][j]==1){
distance[i][j]=1;
}
if(i==j){
distance[i][j]=0;
}
}
}
//cout<<"HERE"<<endl;
for(int k=0;k<n;k++){ //FLOYDD WARSHALL
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
distance[i][j]=min(distance[i][j],distance[i][k]+distance[k][j]);
}
}
}
vector<int> ans;
ans.push_back(a[0]);
int cur=a[0];
int end=a[m-1];
for(int i=1;i<m-1;i++){
cur=a[i];
int start=ans.back();
// cout<<cur<<endl;
//cout<<cur<<endl;
int flag=0;
int j=i+1;
int nn=a[j];
int temp=distance[start][nn];
int temp2=distance[start][cur]+distance[cur][nn];
if(temp2!=temp){
flag++;
}
if(flag!=0){
ans.push_back(cur);
}
}
ans.push_back(a[m-1]);
cout<<ans.size()<<endl;
for(int i:ans){
cout<<i+1<<" ";
}
return 0;
}