ZOJ-1455 Schedule Problem

Schedule Problem

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

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

————————————————————惊奇的分割线————————————————————

前言:突然发现之前我写的差分约束的代码全部都是错的啊!要求最小值的时候,应该跑最长路,但是我一律跑了最短路啊!

思路:求最小值,跑最长路,按照大于等于的关系建图。隐藏的关系是S[i] >= 0,因此引入超级源点的时候,S[0] = 0,满足S[i] - S[0] >= 0。即add(0, i, 0)。

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int N = 1e4, M = 1e5;
int n, tot;
int head[N], dis[N], table[N], enq[N], q[N];
bool inq[N];
struct Node {
	int v, w, next;
}edge[M];

void add(int u, int v, int w)
{
	edge[tot].v = v; edge[tot].w = w;
	edge[tot].next = head[u]; head[u] = tot++;
}

bool spfa(int st)
{
	for(int i = 1; i <= n; i++) {
		dis[i] = -INF;
		inq[i] = enq[i] = 0;
	}
	int fron = 0, rear = 0;
	q[rear++] = st;
	dis[st] = 0; inq[st] = true; enq[st]++;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		inq[u] = false;
		for(int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			if(dis[v] < dis[u] + edge[i].w) {
				dis[v] = dis[u] + edge[i].w;
				if(!inq[v]) {
					q[rear%N] = v; rear++;
					inq[v] = true;
					enq[v]++;
					if(enq[v] > n+1) return false;
				}
			}
		}
	}
	return true;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int cas = 1;
	while(scanf("%d", &n), n) {
		tot = 0;
		memset(head, -1, sizeof(head));
		for(int i = 1; i <= n; i++) {
			scanf("%d", &table[i]);
		}
		char op[10];
		int a, b;
		while(scanf("%s", op), op[0] != '#') {
			scanf("%d%d", &a, &b);
			if(op[0] == 'F') {
				if(op[2] == 'S') {
					//S[a] + table[a] >= S[b]
					add(b, a, -table[a]);
				}
				else {
					//S[a] + table[a] >= S[b] + table[b]
					add(b, a, table[b] - table[a]);
				}
			}
			else if(op[0] == 'S') {
				if(op[2] == 'F') {
					//S[a] >= S[b] + table[b]
					add(b, a, table[b]);
				}
				else {
					//S[a] >= S[b]
					add(b, a, 0);
				}
			}
		}
		for(int i = 1; i <= n; i++)
			add(0, i, 0);//S[i] - S[0] >= 0
		printf("Case %d:\n", cas++);
		if(spfa(0)) {
			for(int i = 1; i <= n; i++) {
				printf("%d %d\n", i, dis[i]);
			}
		}
		else {
			puts("impossible");
		}
		puts("");
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值