博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Subway Chasing HDU - 6252 (差分约束)
阅读量:6863 次
发布时间:2019-06-26

本文共 3495 字,大约阅读时间需要 11 分钟。

Subway Chasing

 

Mr. Panda and God Sheep are roommates and working in the same company. They always take subway to work together. There are NN subway stations on their route, numbered from 1 to NN. Station 1 is their home and station NN is the company. 

One day, Mr. Panda was getting up later. When he came to the station, God Sheep has departed XX 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 AA and BB, God Sheep is just between station CC and DD. 
BB is equal to A+1A+1 which means Mr. Panda is between station AA and A+1A+1exclusive, or BB is equal to AA which means Mr. Panda is exactly on station AA. Vice versa for CC and DD. 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, TT. TT test cases follow.

Each test case starts with a line consists of 3 integers NN, MM and XX, indicating the number of stations, the number of chat contents and the minute interval between Mr. Panda and God Sheep. Then MM lines follow, each consists of 4 integers AA, BB, CC, DD, indicating each chat content. 
1≤T≤301≤T≤30 
1≤N,M≤20001≤N,M≤2000 
1≤X≤1091≤X≤109 
1≤A,B,C,D≤N1≤A,B,C,D≤N 
A≤B≤A+1A≤B≤A+1 
C≤D≤C+1C≤D≤C+1

Output

For each test case, output one line containing “Case #x: y”, where xx is the test case number (starting from 1) and yy is the minutes between stations in format t1,t2,...,tN−1t1,t2,...,tN−1. titi represents the minutes between station ii and i+1i+1. If there are multiple solutions, output a solution that meets 0<ti≤2×1090<ti≤2×109. If there is no solution, output “IMPOSSIBLE” instead.

Sample Input

24 3 21 1 2 32 3 2 32 3 3 44 2 21 2 3 42 3 2 3

Sample Output

Case #1: 1 3 1Case #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.

题目大意:

给出两个人之间的距离以及在一些相同的时刻,他们所属的区段,问这些区段的长度

思路:

对于每一组给出的区段信息,我们都可以得到:

d-a>=x c-b<=x

当a,b重叠或者c,d重叠时等号无法取到

也就是d-a>=x+1&&c-b<=x-1

与此同时,任意两点之间的距离还要大于1,也就是

x-(x-1)>=1

根据上述这些式子进行差分约束

AC代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define endl '\n'#define sc(x) scanf("%lld",&x)using namespace std;const int inf=0xc0c0c0c0;const int size=2005;struct Edge{ int u,v,w; Edge(int u=0,int v=0,int w=0):u(u),v(v),w(w){}};vector
edge;vector
No[2005];int vis[2005];int dis[2005];int cnt[2005];void init(int n){ fill(dis,dis+n+1,inf); memset(vis,0,sizeof(vis)); memset(cnt,0,sizeof(cnt)); for(int i=0;i<=n;i++) No[i].clear(); edge.clear();}void addedge(int u,int v,int w){ edge.push_back(Edge(u,v,w)); No[u].push_back(edge.size()-1);}bool spfa(int s,int n){ dis[s]=0; queue
Q; Q.push(s); while(!Q.empty()) { int k=Q.front(); Q.pop(); vis[k]=0; for(int i=0;i
n) return false; } } } } return true;}int32_t main(){ int n,m,x; int t; scanf("%d",&t); for(int kace =1;kace <= t ;kace++) { scanf("%d%d%d",&n,&m,&x); int i; init(n+1); for(int i=1;i<=n;i++) { addedge(i-1,i,1); } for(i=0;i

 

转载于:https://www.cnblogs.com/fly-white/p/10092698.html

你可能感兴趣的文章
使用docker的kms服务器激活office2016专业增强版
查看>>
Redis
查看>>
程序员需要淡定
查看>>
大整数算法[11] Karatsuba乘法
查看>>
为什么可以用while(cin)?
查看>>
Cisco 交换机与路由器故障处理方法分享
查看>>
linux运行级别
查看>>
Debian(Linux)+XAMPP(LAMPP)+Zend Studio + PHP +XDebug 完整的开发环境配置方法。
查看>>
Python字符集编码和文件读写 [转]
查看>>
COGS728. [网络流24题] 最小路径覆盖问题
查看>>
Python----切片
查看>>
处理浏览器兼容性(持续更新)
查看>>
常用数据库 JDBC URL 格式
查看>>
c#在不安装Oracle客户端的情况下与服务器上的Oracle数据库交互
查看>>
Java回环变位和倒置字符串
查看>>
前景检测算法_1(codebook和平均背景法)
查看>>
分析函数(一)
查看>>
去掉iframe横向滚动条_iframe滚动条
查看>>
Linux Shell编程--入门脚本
查看>>
无法启动iis express web 服务器
查看>>