HDOJ1534差分约束+SPFA

Posted MekakuCityActor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ1534差分约束+SPFA相关的知识,希望对你有一定的参考价值。

http://acm.hdu.edu.cn/showproblem.php?pid=1534

Schedule Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2196    Accepted Submission(s): 994
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
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
题目大意:给一堆工作所需花费的时间,然后给出工作的顺序【即某些工作要在另一些工作开始之后才能开始,有些工作在另一些工作结束之后才能开始,有些工作在另一些工作结束之后结束,有些工作要在另一些工作开始之后结束..】求怎样安排每个工作的开始时间可以使每件工作尽早结束。
题目分析:定义D【I】表示工作 I 的开始时间,T【I】为 工作 I 的花费时间,则可以根据这些先后顺序列出不等式,然后建图,确保连通,跑一遍SPFA即可
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<queue>
  5 using namespace std;
  6 struct edge{
  7     int to;
  8     int next;
  9     int len;
 10 }qwq[100005];
 11 queue<int>pq;
 12 int edge_cnt=0,    n,t[10005],head[100005],in[100005],stk[100005],dist[100005];
 13 bool spfa()
 14 {
 15     while(!pq.empty())
 16     {
 17         pq.pop();
 18     }
 19     pq.push(0);
 20     in[0]++;
 21     stk[0]=1;
 22     while(!pq.empty())
 23     {
 24         int qaq=pq.front();pq.pop();
 25         stk[qaq]=0;
 26         for(int i = head[qaq];i!=-1;i=qwq[i].next)
 27         {
 28             int v=qwq[i].to;
 29             if(dist[v]<dist[qaq]+qwq[i].len)
 30             {
 31                 dist[v]=dist[qaq]+qwq[i].len;
 32                 if(!stk[v])
 33                 {
 34                     pq.push(v);
 35                     in[v]++;
 36                     stk[v]=1;
 37                     if(in[v]>n+1){
 38                         return false;
 39                     }
 40                 }
 41             }
 42         }
 43     }
 44     return true;
 45 }
 46 void add(int x,int y,int z)
 47 {
 48     qwq[edge_cnt].to=y;
 49     qwq[edge_cnt].next=head[x];
 50     qwq[edge_cnt].len=z;
 51     head[x]=edge_cnt++; 
 52 }
 53 int main()
 54 {
 55     scanf("%d",&n);
 56     int case1=1;
 57     while(n)
 58     {
 59         memset(head,-1,sizeof(head));
 60         memset(dist,-1,sizeof(dist));
 61         dist[0]=0;
 62         memset(in,0,sizeof(in));
 63         memset(stk,0,sizeof(stk));
 64         edge_cnt=0;
 65         for(int i = 1 ;i <= n ; i++)
 66         {
 67             scanf("%d",&t[i]);
 68         }
 69         char ss[50];
 70         int a,c;
 71         scanf("%s",ss);
 72         while(ss[0]!=#)//FAS, FAF, SAF and SAS. 
 73         {
 74             scanf("%d%d",&a,&c);
 75             if(ss[0]==S)
 76             {
 77                 if(ss[2]==S)
 78                 {
 79                     add(c,a,0);
 80                 //    cout << c << a << "0\n";
 81                     //cout << ss[6]<<"   "<<ss[4]-‘0‘ <<  "0" <<endl;
 82                 }
 83                 else
 84                 {
 85                     add(c,a,t[c]);
 86                 //    cout << c << a <<t[c]<<endl;
 87                 //    cout << ss[6]<<"   "<<ss[4]-‘0‘ << t[ss[6]-‘0‘]  <<endl;
 88                 }
 89             }
 90             else 
 91             {
 92                 if(ss[2]==S)
 93                 {
 94                     add(c,a,-t[a]);
 95                     //cout << c << a <<-t[a]<<endl;
 96                 //    cout << ss[6]<<"   "<<ss[4]-‘0‘ << -t[ss[4]-‘0‘]  <<endl;    
 97                 }
 98                 else
 99                 {
100                     add(c,a,-t[a]+t[c]);
101                 //    cout << c<<a <<-t[a]+t[c]<<endl;
102                     //cout << ss[6]<<"   "<<ss[4]-‘0‘ << -t[ss[4]-‘0‘]+t[ss[6]-‘0‘] <<endl;    
103                 }
104             }
105             for(int i = 1 ; i <= n ; i++)
106             {
107                 add(0,i,0);
108             }
109             scanf("%s",ss);
110         }
111         printf("Case %d:\n",case1++);
112         if(!spfa())
113         printf("impossible\n");
114         else
115         for(int i = 1 ; i <= n ;i++)
116         {
117             printf("%d %d\n",i,dist[i]);
118         }
119         printf("\n");
120         scanf("%d",&n);
121     }
122     return 0;
123  } 

 

以上是关于HDOJ1534差分约束+SPFA的主要内容,如果未能解决你的问题,请参考以下文章

[luoguP1993] 小 K 的农场(差分约束 + spfa 判断负环)

ZOJ 2770 差分约束+SPFA

HDOJ题目3440 House Man(差分约束)

HDOJ4109拓扑OR差分约束求关键路径

poj 1201 Intervals差分约束+spfa

POJ 3159 Candies(差分约束+spfa+链式前向星)