Subway Chasing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1535 Accepted Submission(s): 523
Special Judge
Problem Description
Mr. Panda and God Sheep are roommates and working in the same company. They always take subway to work together. There are N subway stations on their route, numbered from 1 to N. Station 1 is their home and station N is the company.
One day, Mr. Panda was getting up later. When he came to the station, God Sheep has departed X minutes ago. Mr. Panda was very hurried, so he started to chat with God Sheep to communicate their positions. The content is when Mr. Panda is between station A and B, God Sheep is just between station C and D.
B is equal to A+1 which means Mr. Panda is between station A and A+1 exclusive, or B is equal to A which means Mr. Panda is exactly on station A. Vice versa for C and D. What’s more, the communication can only be made no earlier than Mr. Panda departed and no later than God Sheep arrived.
After arriving at the company, Mr. Panda want to know how many minutes between each adjacent subway stations. Please note that the stop time at each station was ignored.
One day, Mr. Panda was getting up later. When he came to the station, God Sheep has departed X minutes ago. Mr. Panda was very hurried, so he started to chat with God Sheep to communicate their positions. The content is when Mr. Panda is between station A and B, God Sheep is just between station C and D.
B is equal to A+1 which means Mr. Panda is between station A and A+1 exclusive, or B is equal to A which means Mr. Panda is exactly on station A. Vice versa for C and D. What’s more, the communication can only be made no earlier than Mr. Panda departed and no later than God Sheep arrived.
After arriving at the company, Mr. Panda want to know how many minutes between each adjacent subway stations. Please note that the stop time at each station was ignored.
Input
The first line of the input gives the number of test cases, T. T test cases follow.
Each test case starts with a line consists of 3 integers N, M and X, indicating the number of stations, the number of chat contents and the minute interval between Mr. Panda and God Sheep. Then M lines follow, each consists of 4 integers A, B, C, D, indicating each chat content.
1≤T≤30
1≤N,M≤2000
1≤X≤109
1≤A,B,C,D≤N
A≤B≤A+1
C≤D≤C+1
Each test case starts with a line consists of 3 integers N, M and X, indicating the number of stations, the number of chat contents and the minute interval between Mr. Panda and God Sheep. Then M lines follow, each consists of 4 integers A, B, C, D, indicating each chat content.
1≤T≤30
1≤N,M≤2000
1≤X≤109
1≤A,B,C,D≤N
A≤B≤A+1
C≤D≤C+1
Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the minutes between stations in format t1,t2,...,tN−1. ti represents the minutes between station i and i+1. If there are multiple solutions, output a solution that meets 0<ti≤2×109. If there is no solution, output “IMPOSSIBLE” instead.
Sample Input
2 4 3 2 1 1 2 3 2 3 2 3 2 3 3 4 4 2 2 1 2 3 4 2 3 2 3
Sample Output
Case #1: 1 3 1
Case #2: IMPOSSIBLE
Hint
In the second test case, when God Sheep passed the third station, Mr. Panda hadn’t arrived the second station. They can not between the second station and the third station at the same time.
Source
Recommend
liuyiding
题意:有A,B两个人, B比A早走x分钟,每隔一段时间AB两人会给出自己的位置。间隔的时间不一定相等。问每个站点之间需要多少时间。
题解:把AB每次汇报位置的所有可能出现的情况都列出来。每种情况写出关系式。比如
若每次两人报的位置分别是 a,b,c,d.
当a!=b && c!=d && c>b的情况
c-b<x--> c-b<=x-1 --> b>=c-x+1 即连上 c到b的边,边权为-x+1;
d-a>x--> d-a>=x+1--> d>=a+x+1 即连上a到d的边,边权为x+1;
其他还有很多情况 一一列举即可
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll cnt;
ll t, n, m, x, tot;
const ll mx = 1e5+20;
bool vis[mx];
struct edge{
ll v, d, nex;
}e[mx];
ll head[mx], dis[mx], ds[mx];
void init(){
memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head));
memset(dis, 0, sizeof(dis));
memset(ds, 0, sizeof(ds));
tot=0;
}
void add(ll u, ll v, ll w){
e[tot].v=v;
e[tot].d=w;
e[tot].nex=head[u];
head[u]=tot++;
}
bool spfa(){
queue<ll>q;
q.push(1);
vis[1]=true;
ds[1]++;
while(!q.empty()){
ll u=q.front();
q.pop();
vis[u]=false;
for(ll i=head[u]; i!=-1; i=e[i].nex){
ll v=e[i].v, d=e[i].d+dis[u];
if(dis[v]<d){
dis[v]=d;
if(!vis[v]){
ds[v] ++;
if(ds[v]>=n)
return false;
q.push(v);
vis[v]=true;
}
}
}
}
return true;
}
int main() {
scanf("%lld",&t);
for(ll o=1; o<=t; o++){
scanf("%lld%lld%lld",&n,&m,&x);
init();
for(ll i=1; i<=m; i++){
ll a, b, c, d;
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
if(a==b){
if(c==d){
add(a, c, x);
add(c, a, -x);
}
else{
if(a==c){
add(a, d, x+1);
}
else{
add(c, a, 1-x);
add(a, d, x+1);
}
}
}
else{
if(c==d){
if(b==d){
add(a, b, x+1);
}
else{
add(a, c, x+1);
add(c, b, -x+1);
}
}
else{
if(b==d){
add(a, b, x+1);
}
else{
add(c, b, -x+1);
add(a, d, x+1);
}
}
}
}
for(ll i=2; i<=n; i++)
add(i-1, i, 1);
bool f=spfa();
printf("Case #%lld: ",o);
if(!f){
puts("IMPOSSIBLE");
}
else{
for(ll i=2; i<=n; i++)
printf("%lld%c",dis[i]-dis[i-1],i==n?'\n':' ');
}
}
return 0;
}