HDU - 6252 Subway Chasing (2017 CCPC FInal)(差分约束)

版权声明:Why is everything so heavy? https://blog.csdn.net/lzc504603913/article/details/83017863

Subway Chasing

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

2017中国大学生程序设计竞赛-总决赛-重现赛(感谢哈工大)

 

Recommend

liuyiding

 

题意:告诉你两个人同时在某个地方的信息,问你各个站之间的长度可以是多少

解题思路:根据题目信息建立不等式,分四种情况讨论

1. A==B && C==D

这种情况   B-C=X

2. A!=B  && C!=D  && B!=C

这情况下

B-C<X

D-A>X

3 A!=B && B==C &&C==D

D-A>X

4 A==B && B==C && C!=D

D-A>X

根据上述不等式,建差分约束图。

注意要判负环,可以新建一个超级起点,然后跟所有点连边,这样用SPFA一次就能找到负环。

=号可以转换成 >= && <=

<X可以转换成 <=X-1

>X可以转换成 >=X+1

#include<iostream>
#include<string.h>
#include<queue>
#include<math.h>
using namespace std;
const int MAXN=2005;
typedef long long ll;

inline void scan_d(int &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}

struct edge{
    int u;
    int v;
    int w;
    int next;
}e[MAXN*4];
int edge_num,head[MAXN];
void insert_edge(int u,int v,int w){
    e[edge_num].u=u;
    e[edge_num].v=v;
    e[edge_num].w=w;
    e[edge_num].next=head[u];
    head[u]=edge_num++;
}

int N,M;
bool vis[MAXN];
int time[MAXN];
int d[MAXN];
bool spfa(int s){
    queue<int> que;
    memset(d,0x3f,sizeof(d));
    memset(vis,0,sizeof(vis));
    memset(time,0,sizeof(time));
    que.push(s);
    d[s]=0;
    time[s]++;
    vis[s]=1;
    while(!que.empty()){
        int tp=que.front();
        que.pop();
        vis[tp]=0;
        for(int i=head[tp];~i;i=e[i].next){
            int v=e[i].v;
            if(d[tp]+e[i].w<d[v]){
                d[v]=d[tp]+e[i].w;
                if(vis[v]==0){
                    vis[v]=1;
                    que.push(v);
                    if(++time[v]>=N)
                        return 1;
                }
            }
        }
    }
    return 0;
}

int ans[MAXN];
int main(){

    int T;
    scan_d(T);
    for(int ttt=1;ttt<=T;ttt++){

        edge_num=0;
        memset(head,-1,sizeof(head));

        int X;
        int A,B,C,D;
        scan_d(N);
        scan_d(M);
        scan_d(X);
        for(int i=1;i<=M;i++){
            scan_d(A);
            scan_d(B);
            scan_d(C);
            scan_d(D);
            if(A==B&&C==D){
                insert_edge(B,C,X);
                insert_edge(D,A,-X);
            }
            else if(A==B&&B==C&&A==C){
                insert_edge(D,A,-X-1);
            }
            else if(B==C&&C==D&&D==B){
                insert_edge(D,A,-X-1);
            }
            else{
                insert_edge(B,C,X-1);
                insert_edge(D,A,-X-1);
            }
        }

        for(int i=1;i<=N;i++){
            if(i>1)
                insert_edge(i,i-1,-1);
            insert_edge(0,i,0);
        }


        if(spfa(0))
        {
            printf("Case #%d: IMPOSSIBLE\n",ttt);
        }
        else{

            printf("Case #%d: ",ttt);
            for(int i=2;i<=N;i++){

                int dd=d[i];
                if(dd==0x3f3f3f3f)
                    printf("%d",2000000000);
                else{
                    printf("%d",d[i]-d[i-1]);
                }
                if(i!=N)
                    printf(" ");
                else
                    puts("");
            }
        }

    }

    return 0;
}








猜你喜欢

转载自blog.csdn.net/lzc504603913/article/details/83017863