hdu1534 Schedule Problem (spfa差分约束)

Schedule Problem

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

Recommend
LL   |   We have carefully selected several similar problems for you:   1529  1384  1531  3592  1535 
 

题意:安排n项工作的开始时间,使其总工作时间最小且满足约束条件 分析:很明显的差分约束,有一个小技巧,建一个超级源点,再建图. 代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN=1005;
const int MAXM=1005;
int v[MAXM],w[MAXM],nex[MAXM];
int inq[MAXN],done[MAXN],dis[MAXN],first[MAXN],da[MAXN];
int cnt;

int spfa(int s,int n)
{
    for(int i=0;i<n;i++)dis[i]=-INF;
    memset(inq,0,sizeof inq);
    memset(done,0,sizeof done);
    queue<int>q;
    q.push(s);
    done[s]++;
    dis[s]=0;
    while(!q.empty())
    {
        int x=q.front();q.pop();
        inq[x]=0;
        for(int e=first[x];~e;e=nex[e])
        if(dis[v[e]]<dis[x]+w[e]){
            dis[v[e]]=dis[x]+w[e];
            if(!inq[v[e]])
            {
                q.push(v[e]);
                inq[v[e]]=1;
                if(++done[v[e]]>n)return 1;
            }
        }
    }
    return 0;
}
void add_(int a,int b,int c)
{
    v[cnt]=b;
    w[cnt]=c;
    nex[cnt]=first[a];
    first[a]=cnt++;
}
int main()
{
    int n,m,i,cas=0;char cs[4];
    while(~scanf("%d",&n)&&n)
    {
        int a,b;
        memset(first,-1,sizeof first);
        memset(nex,-1,sizeof nex);
        cnt=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&da[i]);
            //add_(i,n+1,da[i]);//若要求最小总时间,加上这句
            add_(0,i,0);
        }
        while(~scanf("%s",cs)&&cs[0]!='#')
        {
            scanf("%d%d",&a,&b);
            if(cs[0]=='F')
            {
                if(cs[2]=='F')add_(b,a,da[b]-da[a]);
                else add_(b,a,-da[a]);
            }
            else
            {
                if(cs[2]=='F')add_(b,a,da[b]);
                else add_(b,a,0);
            }
        }
        printf("Case %d:\n",++cas);

        if(spfa(0,n+1))printf("impossible\n");
        else
        {
            for(i=1;i<=n;i++)
                printf("%d %d\n",i,dis[i]);
        }
        printf("\n");
    }
    return 0;
}



你可能感兴趣的:(hdu1534 Schedule Problem (spfa差分约束))