-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol_p1.cpp
138 lines (125 loc) · 3.2 KB
/
sol_p1.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
#include <map>
#include <string>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
typedef int ll; // you can change to long long (?)
const int N = 808; // number of vertex
const int M = 888; // number of edges
const int INF = 0x3f3f3f3f;
int n = 0, m = 0;
ll dp[N][N];
ll par[N][N];
map<string, ll> mp;
string rev_mp[N];
ll u[M], v[M], w[M];
void init() {
memset(dp, INF, sizeof(dp));
memset(par, -1, sizeof(par));
for (int i = 0; i < N; ++i) {
dp[i][i] = 0;
}
}
int get_node_id(string s) {
if (mp.find(s) != mp.end()) {
return mp[s];
}
else {
mp[s] = (++n);
rev_mp[n] = s;
return mp[s];
}
}
void read_input(char* file) {
fstream fs;
fs.open(file, ios::in);
string s;
while (getline(fs, s)) {
++m;
stringstream ss;
ss.str(s);
string s1, s2, s3;
ll s4;
ss >> s1 >> s2 >> s3 >> s4;
// cerr << "s1 = " << s1 << " , s2 = " << s2 << " , s3 = " << s3 << " , s4 = " << s4 << endl;
u[m] = get_node_id(s1);
v[m] = get_node_id(s2);
w[m] = s4;
}
cerr << "finish reading input, m = " << m << " , n = " << n << endl;
/* for (int i = 1; i <= m; ++i) {
cerr << u[i] << ' ' << v[i] << ' ' << w[i] << endl;
} */
}
void U(int i, int j, ll _par, ll val1, ll val2) {
ll val = val1 + val2;
if (val < dp[i][j]) {
dp[i][j] = val;
par[i][j] = _par;
}
}
void cal_dp() {
for (int i = 1; i <= m; ++i) {
U(u[i], v[i], u[i], w[i], 0);
U(v[i], u[i], v[i], w[i], 0);
}
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
U(i, j, k, dp[i][k], dp[k][j]);
}
}
}
}
vector<int> path;
void find_path(int u, int v) {
if (par[u][v] == u) {
path.push_back(u);
return;
}
find_path(u, par[u][v]);
find_path(par[u][v], v);
}
void query(int u, int v) {
cout << "shortest path weight = " << dp[u][v] << endl;
path.clear();
find_path(u, v);
path.push_back(v);
for (int i: path) cout << rev_mp[i] << ' ';
cout << endl;
}
int main (int argc, char** argv) {
if (argc != 2) {
cerr << "Usage: ./a.out [input_file]" << endl;
return 0;
}
cerr << "welcome to this program" << endl;
cerr << "all the error message will show on stderr, and the output of query will show on stdout" << endl;
init();
read_input(argv[1]);
cal_dp();
cerr << "query スタート" << endl;
cerr << "input two vertex to query" << endl;
cerr << "for example: " << endl;
cerr << "C T" << endl;
cerr << endl << "input EOF as terminate of query" << endl;
string u, v;
while (cin >> u >> v) {
bool flag = false;
if (mp.find(u) == mp.end()) {
cerr << "vertex " << u << " does not exist" << endl;
flag = true;
}
if (mp.find(v) == mp.end()) {
cerr << "vertex " << v << " does not exist" << endl;
flag = true;
}
if (flag) {
continue;
}
query(get_node_id(u), get_node_id(v));
}
}