HDU 6252 CCPC FINAL 2017

本文解析了一个名为SubwayChasing的问题,讲述了两个人在不同时间乘坐地铁并利用聊天记录来推算各站间距离的算法挑战。通过构建差分约束系统,并采用最短路径算法求解,最终给出了具体的实现代码。

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

Subway Chasing

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中国大学生程序设计竞赛-总决赛-重现赛(感谢哈工大)

 

题目大意:一个人比另一个人先走x分钟,给出m条信息,即一个人在车站a、b之间时,另一个人在车站c、d之间,求所有相邻车站之间的距离。

 

题解:差分约束系统。不知道差分约束系统?大佬博客:点击打开链接  为什么是求最短路呢?试想一下,如果有些点没有受到条件约束,他们之间的路径为多少都可以,按照题目要求应该是1~2*10^9。其实对于受到条件约束的路径,最后的长度也是一个范围,我们就不妨设所有路径都取范围的最小值,这样求最短路肯定是对的。

几个约束条件如下:

相邻两站间路径大于1,即d[i+1]-d[i]>0,需要建一条i+1指向i的边长为-1的边。

ABCD分8种情况讨论

1、A==B==C==D

2、A==B      C==D

3、A==B==C

4、A==B     B!=C

5、B==C==D

6、B!=C    C==D

7、A==C B==D

8、一般情况

需要注意的问题:

8个情况的顺序有一定要求,比如在描述第4个条件时,其实是包含第2个条件的,也可以说第2个条件是第4个条件的一中特殊

情况,这就需要先描述第2个条件,不然会出错。

我们以点n为源点,求最短路。为什么?因为有一个条件是贯穿始终的,即i+1指向i,从点n出发,才一定会求出解。

代码:

#include<bits/stdc++.h>
#define N 2010
#define pk push_back
#define LL long long
using namespace std;

int n,m,x;
int d[N],f[N],c[N];
struct edge
{
    int v,w;
};
vector<edge> G[N];
queue<int>q;

bool spfa()
{
    memset(c,0,sizeof c);memset(d,0x3f,sizeof d);memset(f,0,sizeof f);
    d[n]=0;while (!q.empty())q.pop();
    q.push(n);f[n]=1;c[n]=1;
    while(!q.empty())
    {
        int x=q.front();  q.pop();
        for (int i=0;i<G[x].size();i++)if (d[x]+G[x][i].w<d[G[x][i].v])
        {
            d[G[x][i].v]=d[x]+G[x][i].w;
            if (!f[G[x][i].v])
            {
                f[G[x][i].v]=1;
                q.push(G[x][i].v);
                if (++c[G[x][i].v]>n+1) return false;
            }
        }
        f[x]=0;
    }
    return true;
}

int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while (T--)
    {
        printf("Case #%d: ",++tt);
        scanf("%d%d%d",&n,&m,&x);
        memset(G,0,sizeof G); int fg=0; 

        for (int i=2;i<=n;i++) G[i].pk(edge{i-1,-1});

        for (int i=0;i<m;i++)
        {
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            if (a==b && c==d && a==c)
            {
                fg=1;
                continue;
            }else
            if (a==b && c==d)
            {
                G[d].pk(edge{a,-x});
                G[a].pk(edge{d,x});
            }else
            if (a==b && a==c) G[d].pk(edge{a,-x-1});else
            if (a==b && a!=c)
            {
                G[d].pk(edge{a,-x-1});
                G[a].pk(edge{c,x-1});
            }else
            if (d==c && b==d) G[d].pk(edge{a,-x-1});else
            if (c==d && b!=d)
            {
                G[d].pk(edge{a,-x-1});
                G[b].pk(edge{d,x-1});
            }else
            if (a==c && b==d) G[d].pk(edge{a,-x-1});else
            {
                G[d].pk(edge{a,-x-1});
                G[b].pk(edge{c,x-1});
            }
        }
        if (fg || !spfa())puts("IMPOSSIBLE");else
        for (int i=2;i<=n;i++) printf("%d%c",d[i]-d[i-1],i==n?'\n':' ');
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值