HDU 1534Schedule Problem

S c h e d u l e   P r o b l e m Schedule\ Problem Schedule Problem

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2389Accepted Submission(s): 1105
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个任务的耗时时间,然后有一些约束条件,每个约束条件表示为两个任务的起始和结束之间的先后关系,问是否存在一个方法满足约束条件并且耗时最短。
起始时间是0,可以多个任务同时进行(如果没有冲突的话)

分析

把每个任务拆成两个点,起始点st[ i ]和结束点ed[ i ],可以得到ed[ i ] - st[ i ] = 任务耗时,建边:ed[i] - st[i] >= 耗时,st[i] - ed[i] >= -耗时
对于之后的约束条件,
SAS i j:st[j] - st[i] = 0;
SAF i j:ed[j] - st[i] = 0;
FAS i j:st[j] - ed[i] = 0;
FAF i j:ed[j] - ed[i] = 0

用SPFA跑一遍最长路就好了,判断一下有没有正环

AC代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 2e5+7;
const int INF = 0x3f3f3f3f;
struct EDGE{
    int to,cost,next;
};
int n,edge_num,kase=0;
int head[maxn],dist[maxn],vis[maxn],inqueue[maxn],indegree[maxn];
EDGE edge[maxn];
void init(){
    edge_num = 0;
    memset(head,255,sizeof(head));
    memset(dist,-INF,sizeof(dist));
    memset(vis,0,sizeof(vis));
    memset(inqueue,0,sizeof(inqueue));
    memset(indegree,0,sizeof(indegree));
}
void add_edge(int u,int v,int c){
    edge[edge_num].to = v;
    edge[edge_num].cost = c;
    edge[edge_num].next = head[u];
    head[u] = edge_num++;
    indegree[v]++;
}
void input_build(){
    init();
    int cst;
    for(int i=1;i<=n;i++){
        scanf("%d",&cst);
        add_edge(i,i+n,cst);
        add_edge(i+n,i,-cst);
    }
    char op[5];
    while(scanf("%s",op)&&op[0]!='#'){
        int pa,pb;
        scanf("%d %d",&pa,&pb);
        if(op[0]=='S'){
            if(op[2]=='S') add_edge(pb,pa,0);
            else add_edge(pb+n,pa,0);
        }
        else if(op[0]=='F'){
            if(op[2]=='S') add_edge(pb,pa+n,0);
            else add_edge(pb+n,pa+n,0);
        }
    }
}
bool SPFA(){
    queue<int> que;
    for(int i=1;i<=n;i++) if(indegree[i]==1){
        que.push(i);
        vis[i] = inqueue[i] = 1;
        dist[i] = dist[i]>0?dist[i]:0;
    }
    if(que.empty()) return false;
    while(!que.empty()){
        int now = que.front();
        que.pop(); vis[now] = 0;
        for(int i=head[now];i!=-1;i=edge[i].next){
            EDGE e = edge[i];
            if(dist[e.to]<dist[now]+e.cost){
                dist[e.to] = dist[now]+e.cost;
                if(!vis[e.to]){
                    if(++inqueue[e.to]>=2*n) return false;
                    que.push(e.to); vis[e.to] = 1;
                }
            }
        }
    }
    return true;
}
int main(){
    while(scanf("%d",&n)!=EOF&&n){
        input_build();
        printf("Case %d:\n",++kase);
        if(!SPFA()) printf("impossible\n");
        else for(int i=1;i<=n;i++) if(i<=n) printf("%d %d\n",i,dist[i]);
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值