-
Notifications
You must be signed in to change notification settings - Fork 0
/
10986SendingEmail.cpp
96 lines (71 loc) · 1.93 KB
/
10986SendingEmail.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
// Vasudev Vijayaraman
// 10986 - Sending Email
// UVA login name - vasapp
// Data Structure required - Vector, Priority_Queue
// Tricks - Pushing into the vector and using priority queue
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
typedef pair <int, int> ii;
typedef vector <ii> vii;
typedef vector <int> vi;
// vi dist(V, INF);
//memset(d, INF, sizeof d); // catching the overflown
const int INF = 0x3f3f3f3f;
/*This function was taken from the Halim book Page nmber 148 as per the requirements
since priority queue was a must for this program */
void dijakrstraAlgorithm(int s, vi &dist, vector<vii> & AdjList ) {
priority_queue< ii, vector<ii>, greater<ii> > pq;
pq.push(ii(0, s));
while (!pq.empty()) {
ii front = pq.top();
pq.pop();
int d = front.first, u = front.second;
if (d > dist[u])
continue;
for (int j = 0; j < (int)AdjList[u].size(); j++) {
ii v = AdjList[u][j];
if (dist[u] + v.second < dist[v.first]) {
dist[v.first] = dist[u] + v.second;
pq.push(ii(dist[v.first], v.first));
}
}
}
}
int i;
int t;
dist[s] = 0;
void readinData() {
int server1;
int server2;
int cableLength;
int TestCases;
int m, n; s;
cin >> TestCases;
for (i = 0; i <= TestCases; i++) {
cin >> n >> m >> s >> t;
cin >> server1 >> server2 >> cableLength;
//cout << n << m << s << t << endl;
vector<vii> AdjList(n, vii());
for (int j = 0; j < m; ++j) {
cin >> server1 >> server2 >> cableLength;
AdjList[server1].push_back(ii(server2, cableLength));
AdjList[server2].push_back(ii(server1, cableLength));
vi dist(1000000);
dijakrstraAlgorithm(s, dist, AdjList);
}
}
}
int main()
{
readinData();
cout << "Case #" << i + 1 << ": ";
if (dist[t] == INF)
cout << dist[t] << endl;
else
cout << "unreachable" << endl;
return 0;
}