HDU6252 - Subway Chasing(差分约束)

本文详细解析了SubwayChasing算法,这是一个关于差分约束系统的算法问题,涉及到多个站点间的距离计算。通过构建特殊的图结构和使用SPFA算法求解最短路径,解决了熊猫先生和上帝羊之间的地铁追逐问题,确保了所有条件下的连通性和距离约束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<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.

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

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

#include<bits/stdc++.h>
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<pair<int,int> >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));
    queue<int>que;
    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值