hdu1534差分约束

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

设开始时间为s[i],需要时间为t[i].

SAF  s[a]>=s[b]+t[b]

SAS  s[a]>=s[b]

FAF s[a]+t[a]>=s[b]+t[b]

FAS s[a]+t[a]>=s[b]

最长路,输出对应dis即可,注意第一个的开始时间为0,

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#include<queue>
#define maxn 10005
#define maxm 500005
using namespace std;
struct egde
{
    int next,v,w;
}edges[2*maxm];
int head[maxn];
int vis[maxn];
int num[maxn];
int dis[maxn];
int cnt;
int n;
int t[maxn];
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void addedge(int u,int v,int w)
{
    edges[cnt].v=v;
    edges[cnt].w=w;
    edges[cnt].next=head[u];
    head[u]=cnt++;
}
int spfa()
{memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    queue<int>q;
    for(int i=0;i<=maxn;i++)
        dis[i]=-inf;
    vis[0]=1;
    dis[0]=0;
    num[0]=1;
    q.push(0);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        num[u]++;
        if(num[u]>n)
            return 0;
        for(int i=head[u];i!=-1;i=edges[i].next)
        {
            int v=edges[i].v;
            int w=edges[i].w;
            if(dis[v]<dis[u]+w)
             {

        dis[v]=dis[u]+w;
            if(!vis[v])
            {
                vis[v]=1;
                q.push(v);
            }
        }
    }
    }
    return 1;
}
int main()
{int w=0;
    while(~scanf("%d",&n)&&n)
    {
        init();
        w++;
        for(int i=1;i<=n;i++)
            scanf("%d",&t[i]);
        string str;
            int u,v;
            for(int i=1;i<=n;i++)
                addedge(0,i,0);
        while(cin>>str&&str!="#")
        {
        scanf("%d%d",&u,&v);
        if(str=="FAS")
            addedge(v,u,-t[u]);
        else if(str=="FAF")
            addedge(v,u,t[v]-t[u]);
        else if(str=="SAF")
            addedge(v,u,t[v]);
        else if(str=="SAS")
            addedge(v,u,0);

        }
        int minn=inf;
        printf("Case %d:\n",w);
        if(spfa())
        {
            for(int i=1;i<=n;i++)
                if(!dis[i])
                minn=min(minn,dis[i]);
        for(int i=1;i<=n;i++)
            printf("%d %d\n",i,dis[i]-minn);

    }
    else
        printf("impossible\n");
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/88087894