您的位置:首页 > 其它

杭电1534 Schedule Problem(差分约束)

2014-11-14 21:06 351 查看

Schedule Problem

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1386 Accepted Submission(s): 592

Special Judge


Problem Description
A project can be divided into several parts. Each part should be completed continuously. This means if a part should take 3 days, we should use a continuous 3 days do complete it. There are four types of constrains among these parts
which are FAS, FAF, SAF and SAS. A constrain between parts is FAS if the first one should finish after the second one started. FAF is finish after finish. SAF is start after finish, and SAS is start after start. Assume there are enough people involved in the
projects, which means we can do any number of parts concurrently. You are to write a program to give a schedule of a given project, which has the shortest time.



Input
The input file consists a sequences of projects.

Each project consists the following lines:

the count number of parts (one line) (0 for end of input)

times should be taken to complete these parts, each time occupies one line

a list of FAS, FAF, SAF or SAS and two part number indicates a constrain of the two parts

a line only contains a '#' indicates the end of a project



Output
Output should be a list of lines, each line includes a part number and the time it should start. Time should be a non-negative integer, and the start time of first part should be 0. If there is no answer for the problem, you should
give a non-line output containing "impossible".

A blank line should appear following the output for each project.



Sample Input
3
2
3
4
SAF 2 1
FAF 3 2
#
3
1
1
1
SAF 2 1
SAF 3 2
SAF 1 3
#
0




Sample Output
Case 1:
1 0
2 2
3 1

Case 2:
impossible




Source
Asia 1996, Shanghai (Mainland China)
/*
可转化为求最长路,最短路edge(u,v)为u-v<=w。最长路将"<="换成">="初始化成-INF
加油!!!
Time:2014-11-14 21:06
*/
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAX=1200;
const int INF=1<<28;
struct Edge{
	int to,w;
	int next;
}edge[MAX];
int a[MAX],head[MAX],d[MAX],out[MAX];
int en;
bool vis[MAX];
void AddEdge(int u,int v,int w){
	edge[en].to=v;edge[en].w=w;
	edge[en].next=head[u];head[u]=en++;
}
bool SPFA(int s,int n){
	for(int i=0;i<=n;i++){
		d[i]=-INF;
	}
	memset(vis,0,sizeof(vis));
	memset(out,0,sizeof(out));
	vis[s]=true;d[s]=0;
	queue<int>q; q.push(s);
	while(!q.empty()){
		int u=q.front(); q.pop(); vis[u]=false;
		if(++out[u]>n+1) return false;
		for(int i=head[u];~i;i=edge[i].next){
			int v=edge[i].to; int w=edge[i].w;
			if(d[v]<d[u]+w){
				d[v]=d[u]+w;
				if(!vis[v]){
					vis[v]=true;
					q.push(v);
				}
			}
		}
	}
	return true;
}
int main(){
	int N;
	int nCase=1;
	while(scanf("%d",&N),N){
		for(int i=1;i<=N;i++){
			scanf("%d",&a[i]);
		}
		char cmd[10];
		en=0; memset(head,-1,sizeof(head));
		int u,v;
		while(scanf("%s",cmd)!=EOF){
			if(cmd[0]=='#') break;
			scanf("%d%d",&u,&v);
			if(strcmp(cmd,"SAS")==0)
				AddEdge(v,u,0);//f(u)>=f(v); 
			else if(strcmp(cmd,"SAF")==0)
				AddEdge(v,u,a[v]);//f(u)>=f(v)+a[v];f(u)-f(v)>=a[v]
			else if(strcmp(cmd,"FAS")==0)
				AddEdge(v,u,-a[u]);//f(u)+a[u]>=f(v);
			else if(strcmp(cmd,"FAF")==0)
				AddEdge(v,u,a[v]-a[u]);//f(u)+a[u]>=f(v)+a[v];
		}
		int s=0;
		for(int i=1;i<=N;i++){
			AddEdge(0,i,0);
		}
		printf("Case %d:\n",nCase++);
		if(SPFA(0,N)){
			for(int i=1;i<=N;i++)
			printf("%d %d\n",i,d[i]);
			puts("");
		}else{
			printf("impossible\n");
		}
	}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: