hdu 1534 Schedule Problem(差分约束求最长路)

Schedule Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 940    Accepted Submission(s): 368
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
题意:一个工程分n part来进行,每一part的开始时间或结束时间有限制,给出若干个限制,求每个part的开始时间。
思路:差分约束。对于每个关系,可以化成如下的不等式:
FAS a b :a-b>=-t[a]
FAF a b :a-b>=t[b]-t[a]
SAF a b :a-b>=t[b]
SAS a b :a-b>=0
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <cstdlib>
using namespace std;

const int INF=1000000007;
const int maxn=10005;
struct node1
{
    int v,w;
    int next;
} edge[1000000];
int n,num;
bool vis[maxn];
int head[maxn];
int dis[maxn],t[maxn],cnt[maxn];
void init()
{
    for(int i=0; i<=n; i++)
        head[i]=-1;
    num=0;
}
void add(int u,int v,int w)
{
    edge[num].v=v;
    edge[num].w=w;
    edge[num].next=head[u];
    head[u]=num++;
}
bool spfa()
{
    queue<int>Q;
    for(int i=0; i<=n; i++)
        dis[i]=-INF;
    memset(vis,false,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    vis[0]=true;
    dis[0]=0;
    cnt[0]++;
    Q.push(0);
    while(!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        vis[x]=false;
        for(int i=head[x]; i!=-1; i=edge[i].next)
        {
            if(dis[edge[i].v]<dis[x]+edge[i].w)
            {
                dis[edge[i].v]=dis[x]+edge[i].w;
                if(!vis[edge[i].v])
                {
                    vis[edge[i].v]=true;
                    if(++cnt[edge[i].v]>n+1) return false;
                    Q.push(edge[i].v);
                }
            }
        }
    }
    return true;
}
int main()
{
    int a,b,c=0;
    char re[5];
    while(scanf("%d",&n),n)
    {
        c++;
        init();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&t[i]);
            add(0,i,0);
        }
        while(scanf("%s",re))
        {
            if(re[0]=='#') break;
            scanf("%d%d",&a,&b);
            if(strcmp(re,"FAS")==0)
                add(b,a,-t[a]);
            else if(strcmp(re,"FAF")==0)
                add(b,a,t[b]-t[a]);
            else if(strcmp(re,"SAF")==0)
                add(b,a,t[b]);
            else
                add(b,a,0);
        }
        printf("Case %d:\n",c);
        if(spfa())
        {
            for(int i=1; i<=n; i++)
                printf("%d %d\n",i,dis[i]);
        }
        else
            printf("impossible\n");
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值