forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10808.cpp
309 lines (295 loc) · 4.88 KB
/
10808.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
#include <bits/stdc++.h>
using namespace std;
struct Frac
{
long long p, q;
inline void reduce()
{
if (q < 0)
{
p = -p;
q = -q;
}
if (q == 0)
{
p = 1;
return;
}
long long g = __gcd(p, q);
p /= g;
q /= g;
}
Frac(long long p = 0, long long q = 1) : p(p), q(q)
{
reduce();
}
Frac operator*(const Frac &b) const
{
return Frac(p * b.p, q * b.q);
}
Frac operator/(const Frac &b) const
{
return Frac(p * b.q, q * b.p);
}
Frac operator+(const Frac &b) const
{
return Frac(p * b.q + b.p * q, q * b.q);
}
Frac operator-(const Frac &b) const
{
return Frac(p * b.q - b.p * q, q * b.q);
}
void operator/=(const Frac &b)
{
p *= b.q;
q *= b.p;
reduce();
}
void operator-=(const Frac &b)
{
p = p * b.q - b.p * q;
q *= b.q;
reduce();
}
bool operator==(const Frac &b) const
{
return p * b.q == q * b.p;
}
bool isZero() const
{
return p == 0 && q != 0;
}
bool isInf() const
{
return p != 0 && q == 0;
}
};
Frac abs(const Frac &a)
{
return Frac(a.p < 0 ? -a.p : a.p, a.q);
}
int dim;
Frac mat[140][140];
void gauss()
{
#if 0
printf("input matrix\n");
for(int i = 0; i < dim; i++)
{
for(int j = 0; j <= dim; j++)
{
printf(mat[i][j].q==1?"%lld ":"%lld/%lld ", mat[i][j].p, mat[i][j].q);
}
printf("\n");
}
#endif
for (int row = 0; row < dim; row++)
{
int bestRow = row;
for (int i = row; i < dim; i++)
{
if (mat[i][row].isZero())
{
continue;
}
bestRow = i;
break;
}
if (mat[bestRow][row].isZero())
{
//printf("matrix is singular\n");
return;
}
if (bestRow != row)
{
for (int i = row; i <= dim; i++)
{
swap(mat[bestRow][i].p, mat[row][i].p);
swap(mat[bestRow][i].q, mat[row][i].q);
}
}
for (int i = dim; i > row; i--)
{
mat[row][i] /= mat[row][row];
}
mat[row][row] = 1;
for (int r = row + 1; r < dim; r++)
{
if (!mat[r][row].isZero())
{
for (int i = dim; i > row; i--)
{
mat[r][i] -= mat[r][row] * mat[row][i];
}
mat[r][row] = 0;
}
}
}
#if 0
printf("gauss: output\n");
for(int i = 0; i < dim; i++)
{
for(int j = 0; j <= dim; j++)
{
printf(mat[i][j].q==1?"%lld ":"%lld/%lld ", mat[i][j].p, mat[i][j].q);
}
printf("\n");
}
#endif
}
int N;
Frac adj[32][32], INF(1, 0);
void dfs(int x, int seen[])
{
if (seen[x] != -1)
{
return;
}
seen[x] = -2;
for (int y = 0; y < N; y++)
if (!adj[x][y].isInf())
{
dfs(y, seen);
}
}
Frac solve(int s, int t)
{
if (s == t)
{
return 0;
}
int id[32];
memset(id, 0xff, sizeof(id));
dfs(s, id);
if (id[t] == -1)
{
return INF;
}
id[s] = 0;
id[t] = 1;
int K = 2;
for (int i = 0; i < N; i++)
if (id[i] == -2)
{
id[i] = K++;
}
int edgeId[16][16];
memset(edgeId, 0xff, sizeof(edgeId));
int M = 0;
for (int x = 0; x < N; x++)
{
for (int y = x + 1; y < N; y++)
{
if (id[x] < 0 || id[y] < 0 || adj[x][y].isInf())
{
continue;
}
edgeId[x][y] = edgeId[y][x] = M++;
}
}
dim = K + M + 1;
assert(dim <= 139);
for (int i = 0; i < dim; i++)
for (int j = 0; j <= dim; j++)
{
mat[i][j] = 0;
}
// K+M+1 vars (currents, potentials, goal)
// M+(K-2)+2 + 1 eq (ohm's; conservation of pot. - potentials of s and t, goal eq)
int eq = 0;
mat[eq][0] = 1;
eq++;// V0 = 0
mat[eq][1] = 1;
mat[eq][dim] = 1;
eq++;// V1 = 1
for (int x = 0; x < N; x++)
{
if (id[x] < 0)
{
continue;
}
for (int y = x + 1; y < N; y++)
{
if (id[y] < 0 || adj[x][y].isInf())
{
continue;
}
// V[y] - V[x] = R*I[xy]
mat[eq][id[y]] = 1;
mat[eq][id[x]] = -1;
mat[eq][K + edgeId[x][y]] = Frac(0) - adj[x][y];
eq++;
}
if (x != t)
{
// sum_y I(xy) = 0 for x!=s, x!=t
for (int y = 0; y < N; y++)
{
if (id[y] < 0 || x == y || adj[x][y].isInf())
{
continue;
}
mat[eq][K + edgeId[x][y]] = (y < x ? -1 : 1);
}
if (x == s)
{
// sum_y I(sy) = voltage between s and t
mat[eq][K + M] = -1;
}
eq++;
}
}
assert(dim == eq);
gauss();
Frac I = abs(mat[K + M][dim]);
if (I.isZero())
{
return INF;
}
return Frac(1) / I;
}
int main()
{
int T, M;
scanf("%d", &T);
for (int cs = 1; cs <= T && scanf("%d %d", &N, &M) == 2; cs++)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
adj[i][j] = (i == j ? 0 : INF);
}
for (int i = 0; i < M; i++)
{
int x, y, r;
scanf("%d %d %d", &x, &y, &r);
if (adj[x][y].isInf())
{
adj[y][x] = adj[x][y] = r;
}
else
{
adj[y][x] = adj[x][y] = (adj[x][y] * r) / (adj[x][y] + r);
}
}
printf("Case #%d:\n", cs);
#if 0
for(int i = 0; i < N; i++)
for(int j = j+1; j < N; j++)
if(!adj[i][j].isInf())
{
printf("%d-%d %lld/%lld\n", i, j, adj[i][j].p, adj[i][j].q);
}
#endif
int Q;
scanf("%d", &Q);
for (int i = 0; i < Q; i++)
{
int s, t;
scanf("%d %d", &s, &t);
Frac res = solve(s, t);
printf("Resistance between %d and %d is %lld/%lld\n", s, t, res.p, res.q);
}
printf("\n");
}
}