Subway Chasing HDU - 6252 (差分約束)
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
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.
題目大意:
給出兩個人之間的距離以及在一些相同的時刻,他們所屬的區段,問這些區段的長度
思路:
對於每一組給出的區段資訊,我們都可以得到:
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<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#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> edge;
vector<int> 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<int> Q;
Q.push(s);
while(!Q.empty())
{
int k=Q.front();
Q.pop();
vis[k]=0;
for(int i=0;i<No[k].size();i++)
{
int id=No[k][i];
if(dis[edge[id].v]<dis[k]+edge[id].w)
{
dis[edge[id].v]=dis[k]+edge[id].w;
if(!vis[edge[id].v])
{
Q.push(edge[id].v);vis[edge[id].v]=1;
if(++cnt[edge[id].v]>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<m;i++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
int pf=0;
if(a!=b||c!=d) pf=1;
addedge(c,b,-x+pf);
addedge(a,d,x+pf);
}
printf("Case #%d:",kace);
if(spfa(0,n+1)&&dis[n]!=inf)
for(int i=2;i<=n;i++)
{
printf(" %d",dis[i]-dis[i-1]);
}
else printf(" IMPOSSIBLE");
puts("");
}
return 0;
}