-
Notifications
You must be signed in to change notification settings - Fork 30
/
minCost.cpp
342 lines (200 loc) · 9.48 KB
/
minCost.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
Solution by Rahul Surana
***********************************************************
Given a directed graph, find the minimum path from node <em>1</em> to node <em>g_nodes</em>
Given a directed graph with weighted edges, determine the minimum weighted path from node <em>1</em> to the last node.
A directed graph is defined <em>g</em> such that:
The total number of nodes in the graph is <em>g_nodes</em>.
The nodes are numbered sequentially as <em>1, 2, 3, …, g_nodes</em>.
The total number of edges in the graph is <em>g_edges</em>.
Each edge connects two distinct nodes (i.e., no edge connects a node to itself).
The edge connecting nodes <em>g_from[i]</em> and <em>g_to[i]</em> is directed. In other words, it describes a path only in the direction <em>g_from[i] → g_to[i]</em>.
The weight of the edge connecting nodes <em>g_from[i]</em> and <em>g_to[i]</em> is <em>g_weight[i]</em>.
The <em>weight</em> of a path from node <em>1</em> to node <em>g_nodes</em> is defined to be the sum of all edges traversed on that path.
Find the path from node <em>1 </em>to node <em>g_nodes </em>having the minimum possible weight. Extra directed edges having weight <em>1 </em>(one) can be added between any two distinct nodes that are not already connected by an edge.
<p class="section-title">Example
The following list of edges in a <em>4 </em>note graph is given:
<pre>From To Weight
1 2 3
1 3 2
2 1 3
1 4 5
</pre>
In the graph below, an additional edge has been added from <em>3</em> to <em>4</em>. The minimum total cost to get from node <em>1</em> to node <em>4</em> is <em>3</em>, <em>1 →<sub>2</sub> 3 →<sub>1</sub> 4</em>.
<img height="270" src="https://hrcdn.net/s3_pub/istreet-assets/SH_Lf1Y6yY5uUND3ftWByA/minimum_weight_path_in_a_directed_graph_example.svg" width="193">
<p class="section-title">Function Description
Complete the function <em>minCost</em> in the editor below.
<em>minCost</em> has the following parameter(s):
<em>g_nodes:</em> an integer representing the number of nodes in graph <em>g</em>
<em>g_from[n-1]:</em> an array of integers representing edge origin nodes
<em>g_to[n-1]:</em> an array of integers representing edge target nodes
<em>g_weight[n-1]:</em> an array of integers representing edge weights
Returns:
<em>int: </em>the minimum possible weight of any path from node <em>1 </em>to node <em>g_nodes</em>
<p class="section-title">Constraints
<em>3 ≤ g_nodes ≤ 10<sup>3</sup></em>
<em>1 ≤ g_edges ≤ <em>min</em>(10<sup>4</sup>, <sup>(g_nodes × (g_nodes − 1))</sup> ⁄ <sub>2</sub>)</em>
<em>1 ≤ g_weight[i] ≤ 10<sup>6</sup></em>
<!-- <StartOfInputFormat> DO NOT REMOVE THIS LINE-->
<summary class="section-title">Input Format for Custom Testing</summary>
<div class="collapsable-details">
Input from stdin will be processed as follows and passed to the function.
The first line contains two space-separated integers, <em>g_nodes</em> and <em>g_edges</em>.
Each of the next <em>g_edges</em> lines contains three space-separated integers, <em>g_from, g_to</em> and <em>g_weight</em>.
<details open="open"><summary class="section-title">Sample Case 0</summary>
<div class="collapsable-details">
<p class="section-title">Sample Input
<pre>STDIN Function
----- -----
2 1 → g_nodes = 2, g_edges = 1
1 2 3 → g_from = 1, g_to = 2, g_weight = 3
</pre>
<p class="section-title">Sample Output
<pre>3
</pre>
<p class="section-title">Explanation
<img height="150" src="https://hrcdn.net/s3_pub/istreet-assets/V4dYVst8zqwx9C0H_AmwNA/minimum_weight_path_in_a_directed_graph_sample_0.svg" width="241">
A directed edge already exists from node <em>1</em> to node <em>2</em> and the path <em>1 → 2</em> is the minimum cost path, so the function returns <em>3</em>.
<summary class="section-title">Sample Case 1</summary>
<div class="collapsable-details">
<p class="section-title">Sample Input
<pre>STDIN Function
----- -----
3 1 → g_nodes = 3, g_edges = 1
1 2 3 → g_from = 1, g_to = 2, g_weight = 3</pre>
<p class="section-title">Sample Output
<pre>1</pre>
<p class="section-title">Explanation
<img height="200" src="https://hrcdn.net/s3_pub/istreet-assets/wvwwSJG9zbf2QZ5fn21uAA/minimum_weight_path_in_a_directed_graph_sample_1.svg" width="206">
As graph <em>g</em> has no edge between node <em>1</em> and node <em>3</em>, an extra edge can be added from node <em>1</em> to node <em>3</em> having weight <em>1</em>. Thus, the path <em>1 → 3</em> is the minimum weight path and the function returns <em>1</em>.
<summary class="section-title">Sample Case 2</summary>
<div class="collapsable-details">
<p class="section-title">Sample Input
<pre>STDIN Function
----- -----
4 4 → g_nodes = 4, g_edges = 4
1 2 3 → g_from = 1, g_to = 2, g_weight = 3
1 3 3 → g_from = 1, g_to = 3, g_weight = 3
1 4 3 → g_from = 1, g_to = 4, g_weight = 3
2 1 3 → g_from = 2, g_to = 1, g_weight = 3</pre>
<p class="section-title">Sample Output
<pre>3</pre>
<p class="section-title">Explanation
<img height="280" src="https://hrcdn.net/s3_pub/istreet-assets/Ub2A_2h_gDdpuCo0tYd2KA/minimum_weight_path_in_a_directed_graph_sample_2.svg" width="233">
A directed edge already exists from node <em>1</em> to node <em>4</em> and the path <em>1 → 4</em> is the minimum cost path, so the function returns <em>3</em>.
***********************************************************
*/
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
/*
* Complete the 'minCost' function below.
*
* The function is expected to return an INTEGER.
* The function accepts WEIGHTED_INTEGER_GRAPH g as parameter.
*/
/*
* For the weighted graph, <name>:
*
* 1. The number of nodes is <name>_nodes.
* 2. The number of edges is <name>_edges.
* 3. An edge exists between <name>_from[i] and <name>_to[i]. The weight of the edge is <name>_weight[i].
*
*/
int minCost(int g_nodes, vector<int> g_from, vector<int> g_to, vector<int> g_weight) {
vector<vector<pair<int,int>>> adj(g_nodes+1,vector<pair<int,int>>());
// vector<int> cg;
for(int i = 0; i < g_from.size(); i++){
// if(g_to[i] == g_nodes) cg.push_back(g_from[i]);
adj[g_from[i]].push_back({g_weight[i],g_to[i]});
}
bool f = false,d = false;
for(int i = 0; i < adj[1].size(); i++){
if(adj[1][i].second == g_nodes) f = true;
// if(find(cg.begin(),cg.end(),adj[1][i].second) != cg.end()){
// d = true;
// }
}
if(!f) return 1;
for(int i = 2; i < g_nodes; i++){
bool n = false;
// if(adj[1][i].second == g_nodes) f = true;
for(int j = 0; j < adj[1].size(); j++){
if(adj[1][j].second == i) n = true;
}
for(int j = 0; j < adj[g_nodes].size(); j++){
if(adj[g_nodes][j].second == i) n = true;
}
if(!n) return 2;
}
priority_queue<pair<int,int>> x;
x.push({0,1});
int ans = 1e9;
while(!x.empty()){
pair<int,int> z = x.top();
x.pop();
if(z.first > ans) continue;
if(z.second == g_nodes) ans = min(z.first,ans);
for(int i = 0; i < adj[z.second].size(); i++){
x.push({z.first+adj[z.second][i].first,adj[z.second][i].second});
}
}
if(ans == 1e9) return 1;
return ans;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string g_nodes_edges_temp;
getline(cin, g_nodes_edges_temp);
vector<string> g_nodes_edges = split(rtrim(g_nodes_edges_temp));
int g_nodes = stoi(g_nodes_edges[0]);
int g_edges = stoi(g_nodes_edges[1]);
vector<int> g_from(g_edges);
vector<int> g_to(g_edges);
vector<int> g_weight(g_edges);
for (int i = 0; i < g_edges; i++) {
string g_from_to_weight_temp;
getline(cin, g_from_to_weight_temp);
vector<string> g_from_to_weight = split(rtrim(g_from_to_weight_temp));
int g_from_temp = stoi(g_from_to_weight[0]);
int g_to_temp = stoi(g_from_to_weight[1]);
int g_weight_temp = stoi(g_from_to_weight[2]);
g_from[i] = g_from_temp;
g_to[i] = g_to_temp;
g_weight[i] = g_weight_temp;
}
int result = minCost(g_nodes, g_from, g_to, g_weight);
fout << result << "\n";
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str) {
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}