zoukankan      html  css  js  c++  java
  • zoj1455

    Schedule Problem

    Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

    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

    #include <cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    #define inf  100000000
    #define maxn  50005
    int sum[maxn], dist[maxn], n, time[maxn], num, head[maxn];
    bool flag[maxn];
    struct Node
    {
        int u,v,w,next;
    }node[maxn];
    void add(int u,int v,int w)
    {
        node[num].v = v;
        node[num].w = w; 
        node[num].next = head[u];
        head[u] = num++;
    }
    int SPFA(int s)
    {
        int i,v,w,u;
        for(i=0;i<=n;i++)
        {
            sum[i] = 0;
            dist[i] = -inf; 
            flag[i] = false;
        }
        queue<int>q;
        q.push(s);
        dist[s] = 0;
        ++sum[s];
        while(!q.empty())
        {
            u = q.front();
            q.pop();
            flag[u] = false;
            for(i=head[u];~i;i=node[i].next)
            {
                v = node[i].v;
                w = node[i].w;
                if(dist[u]!=inf && dist[v] < dist[u] + w)
                {
                    dist[v] = dist[u] + w;
                    if(!flag[v])
                    {
                        q.push(v);
                        flag[v] = true;
                        if(++sum[v]>n)
                        {
                            return 0;
                        }
                    }
                }
            }
        }
        return 1;
    }
    int main()
    {
        int h=1;
        char str[10];
        int i,x,y;
        int temp ;
        while(scanf("%d",&n)!=EOF && n)
        {
            
            memset(head,-1,sizeof(head));
            num=0;
            for(i=1;i<=n;i++)
            {
                scanf("%d",&time[i]);
            }
            while(scanf("%s",str) && str[0]!='#')
            {
                scanf("%d %d",&x,&y);
                if(strcmp(str, "FAS") == 0)
                {
                    add(y, x, -time[x]);
                }
                else if(strcmp(str,"FAF") == 0)
                {
                    add(y, x, time[y] - time[x]);
                }
                else if(strcmp(str,"SAF") == 0)
                {
                    add(y, x, time[y]);
                }
                else
                {
                    add(y, x, 0);
                }
            }
            for( i = 1; i <= n; i++)
            {
                add(0, i, 0);
            }
            
            temp = SPFA(0);
            printf("Case %d:
    ",h++);
            if(temp)
            {
                for(i=1;i<=n;i++)
                {
                    printf("%d %d
    ",i,dist[i]);
                }
            }
            else
                printf("impossible
    ");
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    阿里云服务器Linux CentOS安装配置(五)jetty配置、部署
    阿里云服务器Linux CentOS安装配置(四)yum安装tomcat
    阿里云服务器Linux CentOS安装配置(三)yum安装mysql
    阿里云服务器Linux CentOS安装配置(二)yum安装svn
    【搭建git+maven+jenkins持续集成环境】[一] 搭建git服务器
    使用nginx的反向代理后play获取不到客户端的ip的问题
    MyBatis Generator配置文件翻译
    MyBatis -generator应用
    编程之术与道
    java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3237770.html
Copyright © 2011-2022 走看看