-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path523.cpp
More file actions
204 lines (193 loc) · 3.5 KB
/
523.cpp
File metadata and controls
204 lines (193 loc) · 3.5 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<climits>
#include<sstream>
#include<algorithm>
#define Fin freopen("input.txt","r",stdin)
#define Fout freopen("output.txt","w",stdout)
#define FasterIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define inf INT_MAX/2
using namespace std;
int i,j,k;
int _i;
int charlie;
string s;
int test;
bool p;
int cost;
stringstream ss;
int numOfVertices;
int debugMax;
vector< vector<int> > G;
vector< vector<int> > nxt;
vector<int>taxes;
vector<int>path;
void seeNxt();
void seeTable();
void pathFind();
void findPath(int i,int j);
void setNumofVertices(string s);
void printPath();
void floydWarshell();
void init();
void takeInput();
int main(){
//Fin;
//Fout;
FasterIO;
int M;
cin>>M;
cin.ignore();
cin.ignore();
while(M--){
getline(cin,s);
numOfVertices = 0;
setNumofVertices(s);
//cout<<s<<endl;
init();
takeInput();
//seeTable();
floydWarshell();
//seeTable();
cin.ignore();
p = false;
while(getline(cin,s)){
if(p) cout<<endl;
p = true;
debugMax= 0;
if(s == ""){
break;
}
ss<<s;
int source,destination;
ss>>source>>destination;
findPath(source,destination);
cout<<"From "<<source<<" to "<<destination<<" :"<<endl;
printPath();
cout<<"Total cost : "<< G[source][destination]<<endl;
path.clear();
ss.clear();
}
G.clear();
taxes.clear();
nxt.clear();
}
}
void seeNxt(){
for(i = 1;i<=numOfVertices;i++){
for(j = 1;j<=numOfVertices;j++){
cout<<nxt[i][j]<<' ';
}
cout<<endl;
}
}
void seeTable(){
cout<<numOfVertices<<endl;
for(i = 1;i<=numOfVertices;i++){
for(j = 1;j<=numOfVertices;j++){
cout<<G[i][j]<<"\t\t\t";
}
cout<<endl;
}
cout<<"taxes"<<endl;
for(i = 1;i<(int) taxes.size();i++){
cout<<taxes[i]<<' ';
}
cout<<endl;
}
void init(){
G.resize(numOfVertices+1,vector<int>());
nxt.resize(numOfVertices+1,vector<int>());
taxes.resize(numOfVertices+1);
for(i = 1;i<=numOfVertices;i++){
G[i].resize(numOfVertices+1);
nxt[i].resize(numOfVertices+1);
for(j = 1;j<=numOfVertices;j++){
nxt[i][j] = j;
G[i][j] = (i==j)?0:inf;
}
}
}
void setNumofVertices(string s){
ss<<s;
while(ss>>test){
//cout<<test<<' ';
numOfVertices++;
}
ss.clear();
}
void findPath(int i,int j){
path.push_back(i);
while(i!=j){
if(debugMax == 50){
return;
}
i = nxt[i][j];
//cout<<i<<' ';
path.push_back(i);
debugMax++;
}
}
void pathFind(int s, int d)
{
if(s==d)
{
printf("%d",s);
return;
}
pathFind(s,nxt[s][d]);
printf("-->%d",d);
}
void printPath(){
p = false;
cout<<"Path: ";
for(auto i:path){
if(p) cout<<"-->";
p = true;
cout<<i;
}
cout<<endl;
}
void floydWarshell(){
for(k=1;k<=numOfVertices;k++){
for(i=1;i<=numOfVertices;i++){
for(j=1;j<=numOfVertices;j++){
if(G[i][k] == inf && G[k][j] == inf) continue; //if middle node does not lead the way.
cost = taxes[k];
if(k == i || k == j) cost = 0; //if middile node is not unique at all.
if(G[i][j]> (G[i][k]+G[k][j]+cost)){
G[i][j] = G[i][k]+G[k][j]+cost;
nxt[i][j] = nxt[i][k];
}
}
}
}
}
void takeInput(){
ss<<s;
_i = 1;
while(ss>>test){
if(test == -1) test = inf;
G[1][_i] = test;
_i++;
}
ss.clear();
if(numOfVertices != 1){
for(i = 2;i<=numOfVertices;i++){
for(j = 1;j<=numOfVertices;j++){
cin>>charlie;
if(charlie == -1){
charlie = inf;
}
G[i][j] = charlie;
}
}
}
for(i = 1;i<=numOfVertices;i++){
cin>>taxes[i];
}
}