forked from fatemehkarimi/uvaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva-10525.cpp
87 lines (65 loc) · 1.96 KB
/
uva-10525.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
//uva 10525
//New to Bangladesh?
#include <iostream>
#include <climits>
#include <vector>
#include <set>
#include <map>
using namespace std;
pair <int, int> Dijkstra(vector < multimap <int, pair <int, int> > > & Graph, int source, int destination);
int main(void)
{
int T = 0;
cin >> T;
while(T--){
int n, e;
cin >> n >> e;
vector < multimap <int, pair <int, int> > > Graph(n);
for(int i = 0; i < e; ++i){
int u, v, t, d;
cin >> u >> v >> t >> d;
--u, --v;
Graph[u].insert(make_pair(v, make_pair(t, d)));
Graph[v].insert(make_pair(u, make_pair(t, d)));
}
int query;
cin >> query;
int source, destination;
while(query--){
cin >> source >> destination;
--source, --destination;
pair <int, int > result = Dijkstra(Graph, source, destination);
if(result.first == INT_MAX)
cout << "No Path." << endl;
else
cout << "Distance and time to reach destination is " << result.first << " & " << result.second << "." << endl;
}
if(T) cout << endl;
}
return 0;
}
pair <int, int> Dijkstra(vector < multimap <int, pair <int, int> > > & Graph, int source, int destination)
{
int n = Graph.size();
vector <int> minDis(n, INT_MAX);
vector <int> minTime(n, INT_MAX);
set <pair <int, int> > currTime;
minTime[source] = 0;
minDis[source] = 0;
currTime.insert(make_pair(0, source));
while(!currTime.empty()){
int nodeTime = currTime.begin()->first;
int node = currTime.begin()->second;
currTime.erase(currTime.begin());
for(auto a : Graph[node])
if(a.second.first + nodeTime < minTime[a.first]){
currTime.erase(make_pair(minTime[a.first], a.first));
minTime[a.first] = a.second.first + nodeTime;
minDis[a.first] = minDis[node] + a.second.second;
currTime.insert(make_pair(minTime[a.first], a.first));
}
else if(a.second.first + nodeTime == minTime[a.first])
minDis[a.first] = min(minDis[a.first], minDis[node] + a.second.second);
}
return make_pair(minDis[destination], minTime[destination]);
}