HDU 1534 Schedule Problem

Schedule Problem

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

 

题目大意:告诉每个项目完成需要的时间,以及m条限制关系,即项目a的起始/终止时间晚于项目b的起始/终止时间,求每个项目的开始时间,若不可行,输出impossible。

 

题解:差分约束系统。对于项目j和项目k:(d[i]为项目i的起始时间,a[i]为完成项目i所需要的时间)

若j的起始时间晚于k的起始时间,则d[j]-d[k]>=0

若j的起始时间晚于k的终止时间,则d[j]-(d[k]+a[k])>=0

若j的终止时间晚于k的起始时间,则d[j]+a[j]-d[k]>=0

若j的终止时间晚于k的终止时间,则d[j]+a[j]-(d[k]+a[k])>=0

还有一个条件 d[i]>=0,就是说每个点的开始时间都要大于等于0,

即 d[i]-0>=0

题目要求是在方案可行时求最早的开始时间,所以就是求 d[i]-0的最小值,要写成大于等于的不等式形式,求最长路。

 

代码:

#include<bits/stdc++.h>
#define N 1010
#define pk push_back
#define LL long long
using namespace std;

int n,m,x;
int d[N],f[N],c[N],a[N];
struct edge
{
    int v,w;
};
vector<edge> G[N];

bool spfa(int x)
{
    memset(c,0,sizeof c);
    memset(d,-1,sizeof d);
    memset(f,0,sizeof f);
    d[x]=0;queue<int>q;
    q.push(x);f[x]=1;c[x]=1;
    while(!q.empty())
    {
        int x=q.front();  q.pop(); f[x]=0;
        for (int i=0;i<G[x].size();i++)
        if (d[x]+G[x][i].w>d[G[x][i].v])
        {
            d[G[x][i].v]=d[x]+G[x][i].w;
            if (!f[G[x][i].v])
            {
                f[G[x][i].v]=1;
                q.push(G[x][i].v);
                if (++c[G[x][i].v]>n) return false;
            }
        }
    }
    return true;
}

int main()
{
    int tt=0;
    while(~scanf("%d",&n) && n)
    {
        for (int i=1;i<=n;i++)scanf("%d",&a[i]);
        memset(G,0,sizeof G);
        char x[5];
        while (scanf("%s",x) && x[0]!='#')
        {
            int j,k;
            scanf("%d%d",&j,&k);
            if (x[0]=='S' && x[2]=='S')G[k].pk(edge{j,0});
            if (x[0]=='S' && x[2]=='F')G[k].pk(edge{j,a[k]});
            if (x[0]=='F' && x[2]=='S')G[k].pk(edge{j,-a[j]});
            if (x[0]=='F' && x[2]=='F')G[k].pk(edge{j,a[k]-a[j]});
        }
        for (int i=1;i<=n;i++) G[0].pk(edge{i,0});
        printf("Case %d:\n",++tt);
        if (!spfa(0))puts("impossible");else
            for (int i=1;i<=n;i++)printf("%d %d\n",i,d[i]);
        puts("");
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值