码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1534 Schedule Problem【SPFA】【差分约束】

时间:2015-01-27 14:59:12      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

Schedule Problem


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1404    Accepted Submission(s): 599
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种安排方式(约束条件)。

条件1:FAF a b,a要在b完成后完成。

条件2:FAS a b,a在在b开始前完成。

条件3:SAS a b,a要在b开始前开始。

条件4:SAF a b,a要在b结束前开始。 

给你一系列的约束条件。问:使其工作时间最小且满足所有约束条件的各个工作最早

时间各是什么。如果不满足条件则输出"impossible"。

思路:差分约束系统。设第i件工作的开始时间为t[i]。4个约束条件变成:

条件1:FAF Sa + t[a] - (Sb + t[b]) >= 0

条件2:FAS Sa + t[a] - Sb >= 0

条件3:SAS Sa - Sb >= 0

条件4:SAF Sa - (Sb + t[b]) >= 0

转换为差分约束系统:

条件1:FAF Sb - Sa <= t[a]-t[b]

条件2:FAS Sb - Sa <= t[a]

条件3:SAS Sb - Sa <= 0

条件4:SAF Sb - Sa <= -t[b]

最后求出的Dist[]数组即为各项工作开始的最早时间。不过题目要求最早开始时间为0。

所以计算出Dist[]数组中最小的项,每项工作开始的最早时间减去最小的项即为答案。


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 1010;
const int MAXM = MAXN*MAXN;
const int INF = 0xffffff0;

struct EdgeNode
{
    int to;
    int w;
    int next;
}Edges[MAXM];
int id,Head[MAXN],Dist[MAXN],vis[MAXN],outque[MAXN],num[MAXN];

void AddEdges(int u,int v,int w)
{
    Edges[id].to = v;
    Edges[id].w = w;
    Edges[id].next = Head[u];
    Head[u] = id++;
}

bool SPFA(int s,int N)
{
    memset(vis,0,sizeof(vis));
    memset(outque,0,sizeof(outque));
    for(int i = 0; i <= N; ++i)
        Dist[i] = INF;
    queue<int> Q;
    Dist[s] = 0;
    vis[s] = 1;
    Q.push(s);
    while( !Q.empty() )
    {
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        outque[u]++;
        if(outque[u] > N)
            return false;
        for(int i = Head[u]; i != -1; i = Edges[i].next)
        {
            int temp = Dist[u] + Edges[i].w;
            if(temp < Dist[Edges[i].to])
            {
                Dist[Edges[i].to] = temp;
                if( !vis[Edges[i].to])
                {
                    vis[Edges[i].to] = 1;
                    Q.push(Edges[i].to);
                }
            }
        }
    }
    return true;
}

int main()
{
    int N,kase = 0,u,v;
    char ch[4];
    while(~scanf("%d",&N) && N)
    {
        int Min = INF;
        for(int i = 1; i <= N; ++i)
            scanf("%d", &num[i]);

        memset(Head,-1,sizeof(Head));
        id = 0;
        while(scanf("%s",ch) && ch[0] != '#')
        {
            scanf("%d%d", &u, &v);
            if(strcmp(ch,"SAS") == 0)
                AddEdges(u,v,0);
            else if(strcmp(ch,"SAF") == 0)
                AddEdges(u,v,-num[v]);
            else if(strcmp(ch,"FAS") == 0)
                AddEdges(u,v,num[u]);
            else
                AddEdges(u,v,num[u]-num[v]);
        }
        for(int i = 1; i <= N; ++i)
            AddEdges(0,i,0);
        printf("Case %d:\n",++kase);
        if(SPFA(0,N) == 0)
        {
            printf("impossible\n\n");
            continue;
        }
        for(int i = 1; i <= N; ++i)
            if(Dist[i] < Min)
                Min = Dist[i];
        for(int i = 1; i <= N; ++i)
            printf("%d %d\n", i, Dist[i]-Min);
        printf("\n");
    }
    return 0;
}


HDU1534 Schedule Problem【SPFA】【差分约束】

标签:

原文地址:http://blog.csdn.net/lianai911/article/details/43193179

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!