Subway Chasing_差分约束_2018_3_18

首先建图,l[]数组判断增环

Subway Chasing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 336    Accepted Submission(s): 116
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.
1T30
1N,M2000
1X109
1A,B,C,DN
ABA+1
CDC+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,...,tN1. ti represents the minutes between station i and i+1. If there are multiple solutions, output a solution that meets 0<ti2×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
 

Recommend
liuyiding


#include<bits/stdc++.h>
using namespace std;
const int V(2011),E(6011),inf(0xc0c0c0c0);
int h[V],n[E],e[E],w[E],et,vt,d[V],l[V];
void ins(int f,int t,int wt){
	n[++et]=h[f];h[f]=et;e[et]=t;w[et]=wt;
}

int main(){
	int t,cs,m,x,i,j,k,u;
	scanf("%d",&t);
	for(cs=1;cs<=t;++cs){
		scanf("%d%d%d",&vt,&m,&x);
		fill(h,h+vt+5,et=0);
		while(m--){
			scanf("%d%d%d%d",&i,&j,&k,&u);
			ins(i,u,x+(i!=j||k!=u));
			ins(k,j,-x+(i!=j||k!=u));
		}
		for(int i=0;i<vt;i++)
		ins(i,i+1,1);
		deque<int> q;
		fill(d,d+vt+5,inf);
		d[0]=l[0]=0;
		q.push_back(0);
		while(!q.empty()){
			int j=q.front();
			q.pop_front();
			for(i=h[j];i;i=n[i])
			if(d[j]+w[i]>d[e[i]]){
				d[e[i]]=d[j]+w[i];
				l[e[i]]=l[j]+1;
				if(l[e[i]]>vt){
					d[vt]=inf;
					goto ed;
				}
				if(!q.empty()&&d[e[i]]>d[q.front()])
				q.push_front(e[i]);
				else q.push_back(e[i]);
			}
		}
		ed:
		printf("Case #%d:",cs);
		if(d[vt]==inf)printf(" %s","IMPOSSIBLE");
		else for(int i=2;i<=vt;i++)
		printf(" %d",d[i]-d[i-1]);
		puts("");
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值