您的位置:首页 > 其它

hdoj 1534 Schedule Problem 【差分约束】【SPFA求最长路】

2015-07-03 23:19 435 查看


Schedule Problem

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

Total Submission(s): 1472 Accepted Submission(s): 627

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



题意:有n个项目,每个项目 i 必须连续花费时间time[i] 才能完成, 有4种约束:

(1)FAS: 项目1在项目2开始后完成
(2)FAF: 项目1在项目2完成后完成,
(3)SAF: 项目1在项目2完成后开始
(4)SAS: 项目1在项目2开始后开始。

问你用最短的时间完成所有项目。 因为工作人员够多,所以在满足约束的情况下 可以同时做多个项目。

差分约束 建模不是很难吧。。。

思路:设S[i] 表示第i个项目开始的最优时间则

FAS a b -> S[a] + time[a] >= S[b] ;
FAF a b -> S[a] + time[a] >= S[b] + time[b];
SAF a b -> S[a] >= S[b] + time[b];
SAS a b -> S[a] >= S[b]。

最后还有S[i] - S[0] >= 0 (1 <= i <= n)。

建好图求最长路就ok了,不建议用最短路做,有点绕。。。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAX 1000+10
#define INF 200000
using namespace std;
struct Edge
{
	int from, to, val, next;
}edge[100000+10];
int head[MAX], top;
int dist[MAX];
int used[MAX];//建立入队次数 
bool vis[MAX];//标记是否入队 
int time[MAX];//time[i] 记录第i个项目需要连续工作的时间 
int n;
void init()
{
	top = 0;
	memset(head, -1, sizeof(head));
} 
void addEdge(int u, int v, int w)
{
	Edge E = {u, v, w, head[u]};
	edge[top] = E;
	head[u] = top++;
}
void getMap()
{
	for(int i = 1; i <= n; i++)
	addEdge(0, i, 0);//保证图连通 所有点和源点0相连 
	int a, b;
	char op[10];
	while(scanf("%s", op), strcmp(op, "#"))
	{
		scanf("%d%d", &a, &b);
		if(op[0] == 'F')
		{
			if(op[2] == 'S')//FAS
			addEdge(b, a, -time[a]);
			else//FAF 
			addEdge(b, a, time[b]-time[a]);
		}
		else
		{
			if(op[2] == 'F')//SAF
			addEdge(b, a, time[b]); 
			else//SAS
			addEdge(b, a, 0); 
		}
	}
} 
bool SPFA()
{
	queue<int> Q;
	for(int i = 0; i <= n; i++)
	{
		dist[i] = i==0 ? 0 : -INF;
		used[i] = 0;
		vis[i] = false; 
	}
	vis[0] = true;
	used[0]++;
	Q.push(0);
	while(!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		vis[u] = false;
		for(int i = head[u]; i != -1; i = edge[i].next)
		{
			Edge E = edge[i];
			if(dist[E.to] < dist[u] + E.val)
			{
				dist[E.to] = dist[u] + E.val;
				if(!vis[E.to])
				{
					vis[E.to] = true;
					used[E.to]++;
					if(used[E.to] > n)//存在负环 
						return false;
					Q.push(E.to);
				}
			}
		} 
	}
	return true;
}
int main()
{
	int k = 1;
	while(scanf("%d", &n), n)
	{
		for(int i = 1; i <= n;  i++)
		scanf("%d", &time[i]);
		init(); 
		getMap();
		printf("Case %d:\n", k++);
		if(SPFA())
		{
			for(int i = 1; i <= n; i++)
	        printf("%d %d\n", i, dist[i]);
		} 
		else
		printf("impossible\n");
		printf("\n");
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: