您的位置:首页 > 其它

hud-1534 Schedule Problem(差分约束)

2017-09-02 19:55 330 查看

Schedule Problem

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 48 Accepted Submission(s): 18
 
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)
 

很明显的差分约束,找到约束条件就好了
FAS 第一个的结束 应该在 第二个的开始 之后。 f(u) + a[u] >= f[v].    --->     f(u) - f(v) >= -a[u]
FAF                                                                             f(u) + a[u] >= f(v) + a[v] ---->.  f(u) - f(v) >+ a[v] - a[u]
SAF                                                                            f(u) >= f(v) + a[v]     ---->.      f(u) + f(v) >=  a[v]
SAS                                                                             f(u) >=. f(v)         --->.  f[u] - f[v] >= 0

通过题意来找到约束的范围,这个题是要求最长路,也就是  约束范围最小  是最长路的值

#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <sstream>
#include <stdio.h>
#include <queue>
#include <stack>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 10000;
struct Edage {
int v, w, next;
}edages[maxn * 3];
int head[maxn], dis[maxn], visited[maxn], a[maxn], times[maxn];
int cnt, n;
void add(int u, int v, int w) {
edages[cnt].v = v;
edages[cnt].w = w;
edages[cnt].next = head[u];
head[u] = cnt++;
}

void init() {
cnt = 0;
memset(visited, 0, sizeof(visited));
memset(times, 0, sizeof(times));
memset(head, -1, sizeof(head));
memset(dis, -1, sizeof(dis));
}

bool spfa() {
queue<int> que;
que.push(0);
dis[0] = 0;
visited[0] = 1;
int u;
while (!que.empty()) {
u = que.front();
que.pop();
for (int i = head[u]; i != -1; i = edages[i].next) {
int v = edages[i].v;
int w = edages[i].w;

if (dis[u] + w > dis[v]) {
dis[v] = dis[u] + w;
if (!visited[v]) {
times[v]++;
if (times[v] >= n)
return false;
que.push(v);
visited[v] = 1;
}
}
}
visited[u] = 0;
}
return true;
}

int main() {
//freopen("in.txt", "r", stdin);
int ca = 1;
char str[10];
int u, v;
while (~scanf("%d", &n)) {
if (n == 0)
break;
init();
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}

while (scanf("%s", str)) {
if (str[0] == '#')
break;
scanf("%d%d", &u, &v);
if (strcmp(str, "FAS") == 0) {
add(v, u, -a[u]);
} else if (strcmp(str, "FAF") == 0){
add(v, u, a[v] - a[u]);
} else if (strcmp(str, "SAF") == 0) {
add(v, u, a[v]);
} else {
add(v, u, 0);
}
}

for (int i = 1; i <= n; ++i) {
add(0, i, 0);
}
printf("Case %d:\n", ca++);
if (spfa()) {
int m = 0;
for (int i = 1; i <= n; ++i) {
m = min(m, dis[i]);
}
for (int i = 1; i <= n; ++i) {
printf("%d %d\n", i, dis[i] - m);
}
} else {
printf("impossible\n");
}
printf("\n");
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: