Schedule Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2076 Accepted Submission(s): 941
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
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.
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<bits/stdc++.h> int n,t[10002],tol,head[10002],en,d[10002],anc[10002],d1[10002],tt; bool vis[10002]; using namespace std; struct node { int to,cost,next; }rode[200003]; void add(int a,int b,int c) { rode[tol].to=b; rode[tol].cost=c; rode[tol].next=head[a]; head[a]=tol++; } bool spfa() { int i,j; for(i=0;i<=10000;i++) d[i]=-1e9; memset(vis,0,sizeof(vis)); memset(anc,0,sizeof(anc)); queue<int> P; while(!P.empty())P.pop(); P.push(0);d[0]=0;vis[0]=1; while(!P.empty()) { int v=P.front();P.pop();vis[v]=0; for(i=head[v];i!=-1;i=rode[i].next) { node e=rode[i]; if(d[e.to]<d[v]+e.cost) { d[e.to]=d[v]+e.cost; if(!vis[e.to]) { P.push(e.to); vis[e.to]=1; anc[e.to]++; if(anc[e.to]>n)return 0; } } } } return 1; } int main() { int cas=0; while(~scanf("%d",&n)) { if(!n)break; memset(head,-1,sizeof(head)); int i,j;tol=0; for(i=1;i<=n;i++) scanf("%d",&t[i]); while(1) { char q[6];scanf("%s",q); if(q[0]=='#')break; int a,b;scanf("%d%d",&a,&b); if(q[0]=='S') { if(q[2]=='F') add(b,a,t[b]); else add(b,a,0); } else { if(q[2]=='F') add(b,a,t[b]-t[a]); else add(b,a,-t[a]); } } for(i=1;i<=n;i++) add(0,i,0);//把0设为超级源点,此题没有终点,即从0 //出发更新完所有点即可 printf("Case %d:\n",++cas); if(spfa()) { for(i=1;i<=n;i++) printf("%d %d\n",i,d[i]); } else printf("impossible\n"); printf("\n"); } return 0; }