hdu1534 Schedule Problem(差分约束+spfa)
Schedule Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 641 Accepted Submission(s): 245
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
#
3
1 2 2
FAF 1 2
0
Sample Output
Case 1:
1 0
2 2
3 1
Case 2:
impossible
Case 3:
1 1
2 0
3 0
分析:有n个作业,第 i 个作业所需的时间是 a[i];
现在给定一些约束条件:
SAS u v 表示 v开始后 u 才能开始;f(u)>=f(v);
SAF u v 表示 v结束后 u 才能开始;f(u)+a[u]>=f(v);
FAF u v 表示 v结束后 u 才能结束;f(u)+a[u]>=f(v)+a[v];
FAS u v 表示 v开始后 u 才能结束;f(u)+a[u]>=f(v);
约束条件很明显了,差分约束,我使用的是SPFA;
注意:图有可能不连通
spfa的模板(邻接表)

1 #include<iostream> 2 #include<string.h> 3 #include<string> 4 #include<queue> 5 #define MAX 100000 6 7 using namespace std; 8 9 struct edge 10 { 11 int x,next,value; 12 }e[20*MAX]; 13 int cnt,head[MAX],c[MAX],a[MAX],d[MAX]; 14 bool visited[MAX],flag[MAX]; 15 16 void add(int u,int v,int w) 17 { 18 e[cnt].x=v; 19 e[cnt].value=w; 20 e[cnt].next=head[u]; 21 head[u]=cnt++; 22 } 23 24 bool SPFA(int s,int n) 25 { 26 int tnext,tx,temp; 27 memset(visited,false,sizeof(visited)); 28 memset(c,0,sizeof(c)); 29 queue<int>Q; 30 while(!Q.empty()) Q.pop(); 31 Q.push(s); 32 c[s]++; 33 d[s]=0; 34 visited[s]=true; 35 flag[s]=true; 36 while(!Q.empty()) 37 { 38 temp=Q.front();Q.pop(); 39 tnext=head[temp]; 40 while(tnext!=-1) 41 { 42 tx=e[tnext].x; 43 if(d[tx]<d[temp]+e[tnext].value) 44 { 45 d[tx]=d[temp]+e[tnext].value; 46 if(!visited[tx]) 47 { 48 Q.push(tx); 49 flag[tx]=true; 50 c[tx]++; 51 if(c[tx]>n) return false; 52 visited[tx]=true; 53 } 54 } 55 tnext=e[tnext].next; 56 } 57 visited[temp]=false; 58 } 59 return true; 60 } 61 62 int main() 63 { 64 int n,i,u,v,k=1; 65 string str; 66 while(cin>>n&&n) 67 { 68 cnt=0; 69 for(i=1;i<=n;i++)cin>>a[i]; 70 for(i=0;i<MAX;i++) head[i]=-1; 71 memset(flag,false,sizeof(flag)); 72 while(cin>>str&&str!="#") 73 { 74 cin>>u>>v; 75 if(str=="SAS") add(v,u,0); 76 if(str=="SAF") add(v,u,a[v]); 77 if(str=="FAF") add(v,u,a[v]-a[u]); 78 if(str=="FAS") add(v,u,-a[u]); 79 } 80 cout<<"Case "<<k++<<":"<<endl; 81 bool yes; 82 memset(d,0,sizeof(d)); 83 for(i=1;i<=n;i++) 84 { 85 if(!flag[i]) 86 { 87 yes=SPFA(i,n); 88 } 89 if(!yes) break; 90 } 91 if(yes) 92 { 93 for(i=1;i<=n;i++) 94 { 95 cout<<i<<" "<<d[i]<<endl; 96 } 97 } 98 else 99 { 100 cout<<"impossible"<<endl; 101 } 102 cout<<endl; 103 } 104 return 0; 105 }