HDU-1534 SPFA 差分约束

版权声明:本文为博主原创文章,若需要引用、转载,只需注明来源及原文链接,若有错误,欢迎纠正。 https://blog.csdn.net/u014137295/article/details/88047684

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
代码:

#include<bits/stdc++.h>
#define maxn 1001
using namespace std;
inline int read(){
    int x=0,f=0;char ch=getchar();
    while(ch>'9'||ch<'0')f|=ch=='-',ch=getchar();
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return f?-x:x;
}
vector<pair<int,int> >e[maxn];
int n,c[maxn],d[maxn],vis[maxn],in[maxn];
int SPFA(int s){
	memset(d,-0x3f,sizeof(d));
	memset(vis,0,sizeof(vis));
	memset(in,0,sizeof(in));
	queue<int>q;
	q.push(s);
	d[s]=0,vis[s]=in[s]=1;
	while(!q.empty()){
		int now=q.front();q.pop();
		vis[now]=0;
		if(in[now]>n)return 0;
		for(int i=0;i<e[now].size();++i){
			int v=e[now][i].first;
			if(d[v]<d[now]+e[now][i].second){
				d[v]=d[now]+e[now][i].second;
				if(!vis[v])q.push(v),vis[v]=1,++in[v];
			}
		}
	}
	return 1;
}
int main(){
	int cnt=0;
	while(n=read()){
		for(int i=0;i<=n;++i)e[i].clear();
		for(int i=1;i<=n;++i)c[i]=read(),e[0].push_back(make_pair(i,0));
		string op;
		while(cin>>op&&op[0]!='#'){
			int u=read(),v=read();
			if(op[0]=='S'&&op[2]=='S')e[v].push_back(make_pair(u,0));
			if(op[0]=='S'&&op[2]=='F')e[v].push_back(make_pair(u,c[v]));
			if(op[0]=='F'&&op[2]=='S')e[v].push_back(make_pair(u,-c[u]));
			if(op[0]=='F'&&op[2]=='F')e[v].push_back(make_pair(u,c[v]-c[u]));
		}
		printf("Case %d:\n",++cnt);
		if(SPFA(0))for(int i=1;i<=n;++i)printf("%d %d\n",i,d[i]);
		else puts("impossible");
		putchar('\n');
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u014137295/article/details/88047684