HDU6252 - Subway Chasing(差分约束)

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

差分约束系统解释:
推荐博客:https://blog.csdn.net/m0_37953323/article/details/79908197
https://blog.csdn.net/qq_24451605/article/details/47121853

这个题目的特殊之处(就是我没有考虑到的)是:
1>为了保证连通性,设立一个超级源点,与每一个点相连,权值为0
2>每相邻两个点之间的距离>=1;
然后再根据题目所给的条件,进行分类讨论,构建不等式,然后跑最短路就行了。
最后如果出现负环,则无解,否则就是不等式的解(s[i,i - 1] = dis[i] - dis[i - 1])

#include
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 2005;

int n,m,x;
bool vis[N];
vector >ve[N];
int dis[N];
int num[N];

int spfa()
{
    memset(vis,false,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(dis,inf,sizeof(dis));
    queueque;
    que.push(0);
    vis[0] = true;
    dis[0] = 0;
    num[0] = 1;
    while(!que.empty())
    {
        int tmp = que.front();
        que.pop();
        vis[tmp] = false;
        for(int i = 0;i < ve[tmp].size();++i){
            int v = ve[tmp][i].fi;
            int w = ve[tmp][i].se;
            if(dis[tmp] + w < dis[v]){
                dis[v] = dis[tmp] + w;
                if(!vis[v]){
                    vis[v] = true;
                    num[v]++;
                    if(num[v] > (n + 1)){
                        return -1;
                    }
                    que.push(v);
                }
            }
        }
    }
    return 1;
}

int main()
{
    int t;
    scanf("%d",&t);
    int cnt = 0;
    while(t--)
    {
        cnt++;
        scanf("%d %d %d",&n,&m,&x);
        for(int i = 0;i <= n;++i){
            ve[i].clear();
        }

        int a,b,c,d;

        for(int i = 0;i < m;++i){
            scanf("%d %d %d %d",&a,&b,&c,&d);
           //对每类情况进行分类讨论
            if(a != b && c != d){
                if(a == c){
                    ve[b].pb(mp(a,-1 * x - 1));
                }else if(b == c){
                    ve[d].pb(mp(a,-1 * x - 1));
                }else{
                    ve[d].pb(mp(a,-1 * x - 1));
                    ve[b].pb(mp(c,x - 1));
                }
            }else if(a == b && c == d){
                ve[a].pb(mp(c,x));
                ve[c].pb(mp(a,-x));
            }else if(a != b && c == d){
                if(b == c){
                    ve[b].pb(mp(a,-1 * x - 1));
                }else{
                    ve[b].pb(mp(c,x - 1));
                    ve[c].pb(mp(a,-x - 1));
                }
            }else{
                if(b == c){
                    ve[d].pb(mp(a,-x-1));
                }else{
                    ve[a].pb(mp(c,x - 1));
                    ve[d].pb(mp(a,-1 * x - 1));
                }
            }
        }
	    //相邻两个节点距离>=1
        for(int i = 2;i <= n;++i){
            ve[i].pb(mp(i - 1,-1));
        }
        //超级源点到各点的距离为0
        for(int i = 1;i <= n;++i){
            ve[0].pb(mp(i,0));
        }

        int p = spfa();
        if(p == -1){
            printf("Case #%d: IMPOSSIBLE\n",cnt);
        }else{
            printf("Case #%d:",cnt);
            for(int i = 2;i <= n;++i){
                printf(" %d",dis[i] - dis[i - 1]);
            }
            printf("\n");
        }

    }
    return 0;
}

你可能感兴趣的:(ACM__差分约束系统)