-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATouristGuide.cpp
More file actions
97 lines (80 loc) · 3.11 KB
/
Copy pathATouristGuide.cpp
File metadata and controls
97 lines (80 loc) · 3.11 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
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <climits>
#include <utility>
#include <algorithm>
using namespace std;
// used to store the input information about capacity for each edge within the graph
typedef vector<vector<pair<int, int> > > UndirectedGraph;
static int minimumNumberOfEdgesToGoThrough(const UndirectedGraph & undirectedGraph, int sourceVertex,
int destinationVertex, int initialFlowRate);
/**
* Problem statement can be viewed at:
* http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110903&format=html
*
* @author Quinn Liu (quinnliu@vt.edu)
* @author Jason Riddle (jr1285@vt.edu)
*
* The following is a solution for the above problem.
*/
int main() {
int numberOfVertices;
int numberOfEdges;
int scenarioNumber = 1;
// when numberOfVertices == 0 && numberOfEdges == 0 the while loop will be existed
while ((cin >> numberOfVertices) && (cin >> numberOfEdges) && numberOfVertices) {
UndirectedGraph graphOfCityAndBusRoutes(numberOfVertices + 1);
for (int currentEdgeIndex = 0; currentEdgeIndex < numberOfEdges;
currentEdgeIndex++) {
int firstCityNumber;
int secondCityNumber;
int maximumNumberOfPassengers;
cin >> firstCityNumber >> secondCityNumber
>> maximumNumberOfPassengers;
graphOfCityAndBusRoutes[firstCityNumber].push_back(
make_pair(secondCityNumber, maximumNumberOfPassengers));
graphOfCityAndBusRoutes[secondCityNumber].push_back(
make_pair(firstCityNumber, maximumNumberOfPassengers));
}
int startingCityNumber;
int destinationCityNumber;
int numberOfPassengers;
cin >> startingCityNumber >> destinationCityNumber
>> numberOfPassengers;
cout << "Scenario #" << scenarioNumber++ << endl;
cout << "Minimum Number of Trips = "
<< minimumNumberOfEdgesToGoThrough(graphOfCityAndBusRoutes,
startingCityNumber, destinationCityNumber,
numberOfPassengers) << endl;
cout << endl;
}
return 0;
}
/**
* The shortest path is found by using Dijkstra's Algorithm.
*/
int minimumNumberOfEdgesToGoThrough(const UndirectedGraph & undirectedGraph, int sourceVertex,
int destinationVertex, int initialFlowRate) {
queue<int> queueOfAllUnvisitedVerticesInGraph;
queueOfAllUnvisitedVerticesInGraph.push(sourceVertex);
vector<int> flowInEachEdge(undirectedGraph.size());
flowInEachEdge[sourceVertex] = INT_MAX; // simulate setting edge flow to infinity
while (!queueOfAllUnvisitedVerticesInGraph.empty()) {
int vertex1 = queueOfAllUnvisitedVerticesInGraph.front();
int edgeCapacity1 = flowInEachEdge[vertex1];
queueOfAllUnvisitedVerticesInGraph.pop();
for (int i = 0; i < undirectedGraph[vertex1].size(); i++) {
// vertex2 eventually becomes the destinationVertex
int vertex2 = undirectedGraph[vertex1][i].first;
int edgeCapacity2 = undirectedGraph[vertex1][i].second;
int smallerEdgeCapacity = min(edgeCapacity1, edgeCapacity2);
if (flowInEachEdge[vertex2] < smallerEdgeCapacity) {
flowInEachEdge[vertex2] = smallerEdgeCapacity;
queueOfAllUnvisitedVerticesInGraph.push(vertex2);
}
}
}
return ceil(1.0 * initialFlowRate / (flowInEachEdge[destinationVertex] - 1));
}