hdoj1534Schedule Problem【差分约束】

来源:互联网 发布:淘宝网络兼职可信吗 编辑:程序博客网 时间:2024/06/06 02:42

Schedule Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1559    Accepted Submission(s): 681
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
3234SAF 2 1FAF 3 2#3111SAF 2 1SAF 3 2SAF 1 3#0
 

Sample Output
Case 1:1 02 23 1Case 2:impossible

#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#define inf 0x3f3f3f3f#define eps 1e-8using namespace std;const int maxn=10010;struct Node{int from,to,value;int next;}A[maxn<<2];int num[maxn];int cnt[maxn];int dist[maxn];bool vis[maxn];int head[maxn],node;void init(){node=0;memset(cnt,0,sizeof(cnt));memset(dist,-0x3f,sizeof(dist));memset(vis,false,sizeof(vis));memset(head,-1,sizeof(head));}void add(int u,int v,int c){A[node].from=u;A[node].to=v;A[node].value=c;A[node].next=head[u];head[u]=node++;}bool spfa(int n){int i,j,k,u=0;queue<int>Q;vis[u]=true;dist[u]=0;Q.push(u);cnt[u]=1;while(!Q.empty()){u=Q.front();Q.pop();vis[u]=false;for(k=head[u];k!=-1;k=A[k].next){int v=A[k].to;if(dist[v]<dist[u]+A[k].value){dist[v]=dist[u]+A[k].value;if(!vis[v]){vis[v]=true;cnt[v]++;if(cnt[v]>n)return false;Q.push(v);}}}}return true;}int main(){int n,m,i,j,k=1;char str[10];while(scanf("%d",&n),n){for(i=1;i<=n;++i){scanf("%d",&num[i]);}int a,b;init();while(scanf("%s",str),str[0]!='#'){scanf("%d%d",&a,&b);//s[a]-s[b]>=-num[a];if(strcmp(str,"FAS")==0){add(b,a,-num[a]);}//s[a]+num[a]>=s[b]+num[b];else if(strcmp(str,"FAF")==0){add(b,a,num[b]-num[a]);}//s[a]-s[b]>=num[b]else if(strcmp(str,"SAF")==0){add(b,a,num[b]);}//s[a]-s[b]>=0;else if(strcmp(str,"SAS")==0){add(b,a,0);}}for(i=1;i<=n;++i){add(0,i,0);}printf("Case %d:\n",k++);if(!spfa(n)){printf("impossible\n");}else {for(i=1;i<=n;++i){printf("%d %d\n",i,dist[i]);}}printf("\n");}return 0;} 


0 0
原创粉丝点击