#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <algorithm>
const int MAXN = 11;
const double EPS = 1e-8;
inline int SGN(double x) {
return (x > EPS) - (x < -EPS);
}
struct Setion {
double ti;
int dr;
std::set< int > ban; // record the index of train which is waiting
} S[MAXN];
struct C_Time {
double ti;
int idx; // train index
int dr; // train running direction
bool operator< (C_Time t) const {
return t.ti < ti;
}
} tmp;
bool vis[MAXN]; // ready to depart
int st[MAXN], ans[MAXN], di[MAXN], sp[MAXN];
int main() {
int c, n, m;
double s;
scanf("%d",&c);
while (c--) {
scanf("%d%d%lf", &n, &m, &s);
for (int i = 0; i <= n; ++i) {
S[i].ti = 0.0;
S[i].dr = 0;
S[i].ban.clear();
}
std::priority_queue< C_Time > pq;
for (int i = 1; i <= m; ++i) {
scanf("%d%d%lf%d", st + i, di + i, &tmp.ti, sp + i);
tmp.idx = i;
tmp.dr = 0;
pq.push(tmp);
vis[i] = false;
}
while (!pq.empty()) {
C_Time cur;
do {
cur = pq.top();
pq.pop();
if (cur.dr == 1) --S[st[cur.idx]].dr;
else if(cur.dr == -1) ++S[st[cur.idx] + 1].dr;
if(st[cur.idx] == di[cur.idx])
ans[cur.idx] = ceil(cur.ti);
else
vis[cur.idx] = true;
} while (!pq.empty() && !SGN(pq.top().ti - cur.ti));
for (int i = 1; i <= m; ++i) {
if (!vis[i])
continue;
tmp.idx = i;
int tmp_di = st[i] + (di[i] > st[i]); // the index of section
if (!(S[tmp_di].ban.empty()) && *(S[tmp_di].ban.begin()) < i) // if the train with smaller index is waiting
tmp.dr = 0;
else if (di[i] < st[i])
tmp.dr = S[tmp_di].dr > 0 ? 0 : -1;
else
tmp.dr = S[tmp_di].dr < 0 ? 0 : 1;
if (tmp.dr) {
S[tmp_di].ti = tmp.ti = std::max(cur.ti + s / sp[i], S[tmp_di].ti);
S[tmp_di].dr += tmp.dr;
st[i] += tmp.dr;
S[tmp_di].ban.erase(i);
} else {
S[tmp_di].ban.insert(i); // This train will wait
tmp.ti = S[tmp_di].ti;
}
pq.push(tmp);
vis[i] = false;
}
}
for(int i = 1; i <= m; ++i)
printf("%d\n", ans[i]);
}
return 0;
}
HDU 5132
最新推荐文章于 2025-12-15 22:32:00 发布
4575

被折叠的 条评论
为什么被折叠?



