-
Notifications
You must be signed in to change notification settings - Fork 1
/
10525 - New to Bangladesh?.cpp
154 lines (133 loc) · 2.64 KB
/
10525 - New to Bangladesh?.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
/*input
3
3 2
1 2 2 5
2 3 3 6
2
1 3
1 3
4 2
1 2 5 2
2 3 6 3
1
1 4
5 5
1 2 2 3
1 2 2 4
2 3 5 1
2 3 3 6
4 5 6 7
20
1 2
1 3
1 4
1 5
2 1
2 3
2 4
2 5
3 1
3 2
3 4
3 5
4 1
4 2
4 3
4 5
5 1
5 2
5 3
5 4
*/
#include <stdio.h>
#include <cmath>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string.h>
#include <stack>
#include <map>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <functional>
using namespace std;
#define r(input) freopen("input.txt","r",stdin)
#define w(output) freopen("output.txt","w",stdout)
#define FOR(i, a) for ( int i = 0 ; i < a ; i++ )
#define FOREACH(l) for (auto it = l.begin(); it != l.end(); it++)
#define BitCount(i) __builtin_popcount(i)
#define Sort(v) sort(v.begin(),v.end())
#define cover(a, d) memset(a,d,sizeof(a))
#define remove(v,e) v.erase(std::find(v.begin(),v.end(),e))
#define sz size
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define VISITED 1
#define UNVISITED 0
#define INF 1000000000
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef pair<int,ii> iii;
inline int toInt(string s){int v; istringstream sin(s);sin>>v;return v;}
inline ll toll(string s){ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
int t;
pair<ll,ll> mat[100][100];
int main()
{
cin >> t;
cin.ignore();
while ( t--)
{
FOR(i , 100)FOR(j,100)
{
if(i == j) mat[i][j] = mp(0,0);
else mat[i][j] = mp(INF, INF);
}
int places , roads;
cin >> places >> roads;
FOR(i, roads)
{
int a , b , c , d;
cin >> a >> b >> c >>d;
if( a > places || b > places) continue;
if(c < mat[a-1][b-1].ft || (c == mat[a-1][b-1].ft && d < mat[a-1][b-1].sd))
mat[a-1][b-1] = mat[b-1][a-1] = mp(c , d);
}
for (int k = 0; k < places; k++)
for (int i = 0; i < places; i++)
for (int j = 0; j < places; j++)
{
if(mat[i][j].ft > mat[i][k].ft + mat[k][j].ft)
{
mat[i][j].ft = mat[i][k].ft + mat[k][j].ft;
mat[i][j].sd = mat[i][k].sd + mat[k][j].sd;
}
else if(mat[i][j].ft == mat[i][k].ft + mat[k][j].ft && mat[i][j].sd >mat[i][k].sd + mat[k][j].sd)
{
mat[i][j].sd = mat[i][k].sd + mat[k][j].sd;
}
}
int query;
cin >> query;
FOR(i , query)
{
int a , b;
cin >> a>>b;
a--;b--;
if(mat[a][b].ft != INF && mat[a][b].sd != INF)
cout << "Distance and time to reach destination is "<< mat[a][b].sd<<" & "<< mat[a][b].ft<<'.'<< endl;
else
cout << "No Path."<<endl;
}
if(t)cout << endl;
}
}