题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6252
题目大意:有n个车站,两个人轮流从第一个车站出发,第一个人先出发x分钟,第二个人再出发。接下来给出m个信息,每个信息有A,B,C,D四个参数,表示第二个人在车站A和车站B之间时,第一个人在车站C和车站D之间。通过这些信息,要你求出满足这些信息的情况下,两个相邻车站的距离是多少。
题目思路:对于给出的信息A,B,C,D。当A = B 且 C = D时,满足如下条件:D-A>=x,B-C<=x;当A = B 且 C != D时,满足如下条件:D-A>x,B-Cx,B-C
具体实现如下:
#include
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define MP make_pair
#define lowbit(x) x&-x
#define clr(a) memset(a,0,sizeof(a))
#define _INF(a) memset(a,0x3f,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define IOS ios::sync_with_stdio(false)
#define fuck(x) cout<<"["<<#x<<" "<<(x)<<"]"< >E[MX];
void add_edge(int u, int v, ll w) {
E[u].pb(MP(v, w));
}
int t[MX], vis[MX];
ll d[MX];
bool spfa(int s) {
queueq;
for (int i = 0; i <= n; i++) {
d[i] = 1e18;
t[i] = vis[i] = 0;
}
d[s] = 0; vis[s] = 1; t[s]++;
q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop(); vis[u] = 0;
for (auto nw : E[u]) {
int v = nw.fi; ll w = nw.se;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
if (!vis[v]) {
vis[v] = 1;
if (++t[v] > n) return 0;
q.push(v);
}
}
}
}
return 1;
}
int main() {
//FIN;
int cas = 1;
for (scanf("%d", &_); _; _--) {
scanf("%d%d%d", &n, &m, &x);
for (int i = 0; i <= n; i++) E[i].clear();
for (int i = 1; i <= n; i++) {
if (i < n) add_edge(i + 1, i, -1);
add_edge(0, i, 0);
}
for (int i = 1; i <= m; ++i) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
if (a == b) {
if (c == d) {
add_edge(d, a, -x);
add_edge(b, c, x);
} else {
add_edge(d, a, -x - 1);
add_edge(b, c, x - 1);
}
} else {
add_edge(d, a, -x - 1);
add_edge(b, c, x - 1);
}
}
printf("Case #%d: ", cas++);
if (!spfa(0)) puts("IMPOSSIBLE");
else {
for (int i = 2; i <= n; i++) {
printf("%lld", d[i] - d[i - 1]);
if (i == n) printf("\n");
else printf(" ");
}
}
}
return 0;
}