Schedule Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1954 Accepted Submission(s): 891
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
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.
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个小的任务,每个小任务的时间是t[i],然后把这些小任务给员工们做。但是有的任务要建立在别的任务完成的情况下。
下面给出四种约束:FAS(a,b),FAF(a,b),SAS(a,b),SAF(a,b),以SAF为例,即a要在b结束之后开始。
求完成所有任务的最短时间。
//思路:通过差分约束建图,用SPFA求最短路(因为存在负权边且可能有负环,所以不能有Dijkstra)
了解差分约束:http://blog.csdn.net/xuezhongfenfei/article/details/8685313
了解SPFA:http://www.360doc.com/content/13/1208/22/14357424_335569176.shtml
FAS:a+t[a]>=b => b<=a+t[a]
FAF:a+t[a]>=b+t[b] => b<=a+t[a]-t[b]
SAF:a>=b+t[b] => b<=a-t[b]
SAS:a>=b => b<=a+0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX = 10000 + 10;
const int INF = 100000000;
typedef struct {
int to, next;
int val;
}Edge;
int n;
int time[MAX];
int len;
Edge edge[MAX];
int head[MAX];
int dis[MAX]; //储存源点到每个点的最小距离
bool vis[MAX];
int cnt[MAX]; //储存每个点进队列的次数
void add(int from, int to, int val)
{
edge[len].to = to;
edge[len].val = val;
edge[len].next = head[from];
head[from] = len++;
}
bool SPFA()
{
queue<int>q;
memset(cnt, 0, sizeof(cnt));
memset(vis, false, sizeof(vis));
for (int i = 1; i <= n; i++)
{
//设置一个超级源点0
add(0, i, 0);
//源点到每个点的最小距离先初始化为无穷大
dis[i] = INF;
}
dis[0] = 0;
vis[0] = true;
q.push(0);
while (!q.empty())
{
int now = q.front();
q.pop();
vis[now] = false;
cnt[now]++;
//这个点进队列超过n次,说明这里存在负环
//即这种情况impossible
if (cnt[now] > n)
return false;
for (int i = head[now]; i != -1; i = edge[i].next)
{
int next = edge[i].to;
//求最短路径
if (dis[now] + edge[i].val < dis[next])
{
dis[next] = dis[now] + edge[i].val;
if (!vis[next])
{
vis[next] = true;
q.push(next);
}
}
}
}
return true;
}
int main()
{
int Case = 1;
while (scanf("%d", &n), n)
{
char str[5];
int a, b;
memset(time, 0, sizeof(time));
memset(head, -1, sizeof(head));
len = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &time[i]);
}
while (scanf("%s", str), str[0] != '#')
{
scanf("%d%d", &a, &b);
//根据差分约束建图
if (strcmp(str, "FAS") == 0)
{
add(a, b, time[a]);
}
else if (strcmp(str, "FAF") == 0)
{
add(a, b, time[a] - time[b]);
}
else if (strcmp(str, "SAF") == 0)
{
add(a, b, -time[b]);
}
else if (strcmp(str, "SAS") == 0)
{
add(a, b, 0);
}
}
printf("Case %d:\n", Case++);
if (!SPFA())
{
printf("impossible\n\n");
continue;
}
//因为图里存在负权边,所以最早的开始时间(即最短路里的最小值)可能为负
//但题目要求最早从0开始
//所以要找到这个最小值,dis[]每个数都减它就行了
int min = INF;
for (int i = 0; i <= n; i++)
{
if (min > dis[i])
min = dis[i];
}
for (int i = 1; i <= n; i++)
{
printf("%d %d\n", i, dis[i] - min);
}
printf("\n");
}
return 0;
}