HDU1534 Schedule Problem 【差分约束系统】

Schedule Problem

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

Total Submission(s): 1283    Accepted Submission(s): 534

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

Source

Asia 1996, Shanghai (Mainland China)

题意:给定n个任务的执行时间以及相互之间开始执行时的顺序,求如何安排才能使用时最少。

题解:用SPFA求差分约束系统的最长路,要判断是否有“负环”。

#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 1002
#define maxm maxn * maxn
#define inf 0x3f3f3f3f
using std::queue;

int head[maxn], t[maxn], id;
struct Node{
    int to, w, next;
} E[maxm];
int dist[maxn], out[maxn], in[maxn];
bool vis[maxn];

void addEdge(int u, int v, int w)
{
    E[id].to = v; E[id].w = w;
    E[id].next = head[u]; head[u] = id++;
}

bool SPFA(int n)
{
    int i, u, v, tmp;
    for(i = 0; i <= n; ++i){
        vis[i] = out[i] = 0; dist[i] = -inf;
    }
    u = 0; vis[u] = 1; dist[u] = 0;
    queue<int> Q; Q.push(u);
    while(!Q.empty()){
        u = Q.front(); Q.pop(); vis[u] = 0;
        if(++out[u] > n) return false;
        for(i = head[u]; i != -1; i = E[i].next){
            tmp = dist[u] + E[i].w;
            v = E[i].to;
            if(tmp > dist[v]){
                dist[v] = tmp;
                if(!vis[v]){
                    vis[v] = 1; Q.push(v);
                }
            }
        }
    }
    return true;
}

int main()
{
    int n, cas = 1, u, v, i;
    char str[5];
    while(scanf("%d", &n), n){
        for(i = 1; i <= n; ++i)
            scanf("%d", &t[i]);
        memset(head, -1, sizeof(head)); id = 0;
        while(scanf("%s", str), str[0] != '#'){
            scanf("%d%d", &u, &v);
            if(!strcmp(str, "SAS")) addEdge(v, u, 0);
            else if(!strcmp(str, "SAF")) addEdge(v, u, t[v]);
            else if(!strcmp(str, "FAS")) addEdge(v, u, -t[u]);
            else addEdge(v, u, t[v] - t[u]);
        }
        printf("Case %d:\n", cas++);
        for(i = 1; i <= n; ++i)
            addEdge(0, i, 0);
        if(!SPFA(n)){
            printf("impossible\n\n");
            continue;
        }
        for(i = 1; i <= n; ++i)
            printf("%d %d\n", i, dist[i]);
        printf("\n");
    }
    return 0;
}

HDU1534 Schedule Problem 【差分约束系统】

时间: 2024-07-29 09:22:59

HDU1534 Schedule Problem 【差分约束系统】的相关文章

HDU1534 Schedule Problem 差分约束

囧,还是暴露出了对差分约束理解的不透彻... 一开始根据开始和结束的关系建边,然后建立一个超级源点,连接每一个其他节点,先把这个点入队.本质上相当于把一开始所有的节点都入队了,然后做一遍最长路(最短路,怎么建边的怎么来),相当于把每一个点都作为起点做了一遍最短路,每个点的d取最大的那个. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include &l

HDU1534 Schedule Problem【SPFA】【差分约束】

Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1404    Accepted Submission(s): 599 Special Judge Problem Description A project can be divided into several parts. Each part sho

HDOJ 1534 Schedule Problem 差分约束

差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1503    Accepted Submission(s): 647 Special Judge Problem Descr

HDU-1534 Schedule Problem

四种约束条件..照做就行了.. 最长路建图. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <fstream> #include <iostream> #include <deque> #define rep(i, l, r) for(int i=l;

UVA11478 Halum [差分约束系统]

https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的边的权值增加d,最后让所有边的权值的最小值大于零且尽量大. 该死书上翻译错了 >0不是非负 WA好几次因为这个 考虑每条边的约束,di表示i的halum量 w-dv+du>0 dv-du<w 但求解这个差分约束系统只是让这组不等式成立,最长路和最短路控制的都是单个d的最值而不是最小值最大 那

POJ 3169 Layout(差分约束系统)

题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ML组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w.2.有MD组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w.问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛[n]的距离可以无限远,则输出-2,否则则输出牛[1]和牛[n]之间的最

HDU1384 Intervals 【差分约束系统】

Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2880    Accepted Submission(s): 1048 Problem Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn

UVA 11478 bf+差分约束系统+二分逼近

Halum You are given a directed graph G(V, E) with a set of vertices and edges. Each edge (i, j) that connects some vertex i to vertex j has an integer cost associated with that edge. Define the operation Halum(v, d) to operate on a vertex v using an

ZOJ– 2770Burn the Linked Camp单源最短路径差分约束系统

ZOJ Problem Set – 2770 Burn the Linked Camp 题目的大意为: 陆逊侦查得知刘备将自己的军队分为N个营,从左到右编号为1,2,3…N.第i个营有最大士兵数Ci,通过一段时间的观察,陆逊可以估算至少有k个士兵住在第i到第j个营地,最后陆逊必须要估算刘备的军队至少有多少士兵,以便应对. 输入: 有多个测试案例,在每个测试案例的第一行,有两个整数N(0<n<=1000)和M(0 <= M = 10000).第二行中,有n个整数C1…CN.然后M行,每行有