(hdu1534)Schedule Problem(差分约束+SPFA算法)

来源:互联网 发布:淘宝盗版举报 编辑:程序博客网 时间:2024/05/01 09:16

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1961 Accepted Submission(s): 896
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个工作的计划,给定N个工作的开始时间,有4种约束方式,给出你这些时间的n个约束..
如果计划是可行的,求出每一件事发生的最早时间..否则输出“impossible”..
  ①. FAF a b a要在b完成后完成..
  ②. FAS a b a要在b开始前完成..
  ③. SAS a b a要在b开始前开始..
  ④. SAF a b a要在b结束前开始..
分析:
SAF a b 设a,b开始的时间分别为s[a],s[b],完成a,b这件事分别需要val[a],val[b],a,b,结束时间为e[a],e[b]; a要在b结束前开始,那么s[a]-e[b]>=0,由此推出s[a]-s[b]>=val[b];
FAF a b e[a]-e[b]>=0即s[a]-s[b]>=val[b]-val[a]; 后面类似,以此来构建约束图,
转换为差分约束系统的图的构建形式:
SAF s[b]-s[a]<=-val[b];
FAF s[b]-s[a]<=val[a]-val[b];
SAS s[b]-s[a]<=0;
FAS s[b]-s[a]<=val[a];

另外,根据我在算法导论和网上看到,额外增加一个点V0,增加从V0到所有题中所给点的边,其权值为0,保证图中至少存在一个结点到达其他所有结点.
因为要求的是每一件事开始的最早时间,那么就要求最大值

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespace std;const int INF=0x3f3f3f3f;///使用memset()按字节分配const int N=1005;///不能太小int head[N],val[N];int cnt,n;struct Edge{    int to,w;    int next;} edges[N];void add(int u,int v,int w,int &num)///构建约束图,u为起始点,v为边指向的点(即这条边的终点),w为边的权值,num为当前边的个数{    edges[num].to=v;    edges[num].w=w;    edges[num].next=head[u];    head[u]=num++;}int vis[N],times[N],d[N];int SPFA(int src){    memset(vis,0,sizeof(vis));///vis为点是否入队的标志    memset(d,-INF,sizeof(d));///求最大值,初始值均置为极小的值    memset(times,0,sizeof(times));///记录入队的次数    d[src]=0;    times[src]++;    queue<int>que;    que.push(src);    while(!que.empty())    {        int tou=que.front();        que.pop();        vis[tou]=0;        for(int i=head[tou]; i!=-1; i=edges[i].next)///遍历所有出边指向的点        {            int to=edges[i].to;            int w=edges[i].w;            if(d[to]<d[tou]+w)//求最大值满足的约束条件,此题可以加=号            {                d[to]=d[tou]+w;                if(!vis[to])///若不在队列中                {                    vis[to]=1;                    que.push(to);                    times[to]++;                    if(times[to]>=n)///入队次数大于等于n-1次,就说明存在负权环                        return 1;                }            }        }    }    return 0;}int main(){    int k=1;    while(~scanf("%d",&n)&&n)    {        cnt=0;        memset(head,-1,sizeof(head));///置初值,说明没有边指向该点        for(int i=1; i<=n; i++)///给定的工作开始时间            scanf("%d",&val[i]);        char str[5];        int a,b;        while(~scanf("%s",str))        {            if(str[0]=='#') break;            scanf("%d%d",&a,&b);            if(!strcmp(str,"SAS"))                add(b,a,0,cnt);            else if(!strcmp(str,"SAF"))                add(b,a,val[b],cnt);            else if(!strcmp(str,"FAF"))                add(b,a,val[b]-val[a],cnt);            else if(!strcmp(str,"FAS"))                add(b,a,-val[a],cnt);        }        printf("Case %d:\n",k++);        for(int i=1; i<=n; i++)            add(0,i,0,cnt);///添加的一个点V0---称超级源点        if(SPFA(0))///若存在负权环            puts("impossible");        else        {            for(int i=1; i<=n; i++)                printf("%d %d\n",i,d[i]);        }        printf("\n");    }    return 0;}

如有错误,望大牛指出,谢谢