HDU 6252 Subway Chasing (差分约束)

Subway Chasing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 884    Accepted Submission(s): 288
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.

 

 

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

 

 

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

 

 

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.

题意:羊和猫家都在1号,公司都在n号。途中依次经过2,3,...,n-1号。

羊比猫提前X分钟走。他们俩走的速度是一样的。

在他俩到公司的途中会通话m次,每次他俩都会告诉对方自己在哪。

羊说自己在C,D号之间。C可以等于D或者D-1。

猫说自己在A,B号之间。A可以等于B或者B-1。

问你是否存在一个方案,使他们说的都是对的,即输出1~2 2~3... n-1~n号点的距离。

如果不存在,输出IMPOSSIBLE

思路:

主要难点还是在读题。。。读懂题发现就是个差分约束。

分情况讨论:

1、如果A==B&&C==D

则 C-B<=X      D-A>=X
否则2、如果A==B&&B==C

则 D-A>X
否则3、如果B==C&&C==D

则 D-A>X

否则4、

C-BX

注意每个点和前一个点距离大于等于1。

注意如果b-a<=c,那么从a到b有一条长c的边!!!

代码:

#include
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=2005;
struct edge{
    int u;
    int v;
    int w;
    int next;
}e[maxn*4];
int cnt,head[maxn];
void add(int u,int v,int w){
    e[cnt].u=u;
    e[cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt++;
}

int n,m,X;
bool vis[maxn];
int tim[maxn];
int d[maxn];
bool spfa(int s){
    queue que;
    for(int i=0;i=n)
                        return 1;
                }
            }
        }
    }
    return 0;
}
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&X);
        init();
        //如果b-a<=c,那么从a到b有一条长c的边!!!
        for(int i=0;i1) add(i,i-1,-1);
            add(0,i,0);
        }
        bool ans=spfa(0);
        if(ans) printf("Case #%d: IMPOSSIBLE\n",cas++);
        else {
            printf("Case #%d: ",cas++);
            for(int i=2;i<=n;i++)
            {
                if(d[i]==inf) printf("%d",2000000000);
                else printf("%d",d[i]-d[i-1]);
                printf("%c",i==n?'\n':' ');
            }
        }
    }
    return 0;
}

 

你可能感兴趣的:(图论:图论基础)