forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK.cpp
More file actions
158 lines (122 loc) · 4.25 KB
/
K.cpp
File metadata and controls
158 lines (122 loc) · 4.25 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
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(X) ((int) ((X).size()))
using namespace std;
int nSection, startSection;
pair<int,int> pool;
int c[111][111]; // common edge between 2 section
double totalRate;
struct Section {
public:
int x1, y1, x2, y2, d;
int common_boundary;
bool finished;
double expected_finish;
double rate;
private:
int volume;
double filled;
public:
void read() {
cin >> x1 >> y1 >> x2 >> y2 >> d;
d *= 10;
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
rate = 0;
filled = 0;
common_boundary = 0;
finished = false;
expected_finish = 1e20;
volume = (x2 - x1) * (y2 - y1) * d;
}
void set_rate(double r) {
assert(!finished);
if (r > 1e-9) {
rate = r;
double each = rate / (x2 - x1) / (y2 - y1);
expected_finish = (d - filled) / each;
}
}
void fill(double time) {
// 1s --> rate
double each = rate / (x2 - x1) / (y2 - y1);
filled += each * time;
}
} a[111];
int common(int u, int v, int x, int y) {
if (y < u || v < x) return 0;
return min(v, y) - max(u, x);
}
void initBoundary() {
FOR(i,1,nSection) {
FOR(j,1,nSection) if (i != j) {
if (a[i].x2 == a[j].x1 || a[j].x2 == a[i].x1)
c[i][j] += common(a[i].y1, a[i].y2, a[j].y1, a[j].y2);
if (a[i].y2 == a[j].y1 || a[j].y2 == a[i].y1)
c[i][j] += common(a[i].x1, a[i].x2, a[j].x1, a[j].x2);
}
if (a[i].x1 == 0)
c[i][0] += common(0, pool.second, a[i].y1, a[i].y2);
if (a[i].x2 == pool.first)
c[i][0] += common(0, pool.second, a[i].y1, a[i].y2);
if (a[i].y1 == 0)
c[i][0] += common(0, pool.first, a[i].x1, a[i].x2);
if (a[i].y2 == pool.second)
c[i][0] += common(0, pool.first, a[i].x1, a[i].x2);
}
}
int main() {
ios :: sync_with_stdio(false);
freopen("swimming.in", "r", stdin);
freopen("swimming.out", "w", stdout);
while (cin >> nSection >> pool.first >> pool.second >> startSection >> totalRate) {
FOR(i,1,nSection) a[i].read();
initBoundary();
// FOR(i,1,nSection) {
// PR(c[i], nSection);
// }
ll sum_boundary = 0;
a[startSection].set_rate(totalRate);
double ellapsed_time = 0.0;
FOR(turn,1,nSection) {
int u = -1;
FOR(i,1,nSection) if (!a[i].finished) {
if (u < 0 || a[i].expected_finish < a[u].expected_finish)
u = i;
}
double t = a[u].expected_finish;
a[u].finished = true;
// DEBUG(t);
// cout << u << " is filled" << endl;
FOR(i,1,nSection) if (!a[i].finished) {
a[i].fill(t);
}
FOR(v,0,nSection) {
if (!a[v].finished) {
a[v].common_boundary += c[u][v];
sum_boundary += c[u][v];
}
else {
sum_boundary -= c[u][v];
}
}
// DEBUG(sum_boundary);
FOR(v,1,nSection) if (!a[v].finished) {
a[v].set_rate(totalRate / sum_boundary * a[v].common_boundary);
}
ellapsed_time += t;
// FOR(i,1,nSection) if (!a[i].finished) {
// cout << i << ' ' << a[i].rate << ' ' << a[i].expected_finish << endl;
// }
}
cout << (fixed) << setprecision(12) << ellapsed_time << endl;
}
}