【UVA 1380】 A Scheduling Problem (树形DP)

 

Description

There is a set of jobs, say x1x2,..., xn <tex2html_verbatim_mark>, to be scheduled. Each job needs one day to complete. Your task is to schedule the jobs so that they can be nished in a minimum number of days. There are two types of constraints: Conflict constraints and Precedence constraints.

Conflict constraints: Some pairs of jobs cannot be done on the same day. (Maybe job xi <tex2html_verbatim_mark>and job xj <tex2html_verbatim_mark>need to use the same machine. So they must be done in different dates).

Precedence constraints: For some pairs of jobs, one needs to be completed before the other can start. For example, maybe job xi <tex2html_verbatim_mark>cannot be started before job xj <tex2html_verbatim_mark>is completed.

The scheduling needs to satisfy all the constraints.

To record the constraints, we build a graph G <tex2html_verbatim_mark>whose vertices are the jobs: x1x2,..., xn <tex2html_verbatim_mark>. Connect xi <tex2html_verbatim_mark>and xj <tex2html_verbatim_mark>by an undirected edge if xi <tex2html_verbatim_mark>and xj <tex2html_verbatim_mark>cannot be done on the same day. Connect xi <tex2html_verbatim_mark>and xj <tex2html_verbatim_mark>by a directed edge from xi <tex2html_verbatim_mark>to xj <tex2html_verbatim_mark>if xi <tex2html_verbatim_mark>needs to be completed before xj <tex2html_verbatim_mark>starts.

If the graph is complicated, the scheduling problem is very hard. Now we assume that for our problems, the constraints are not very complicated: The graph G <tex2html_verbatim_mark>we need to consider are always trees (after omitting the directions of the edges). Your task is to nd out the number of days needed in an optimal scheduling for such inputs. You can use the following result:

If G <tex2html_verbatim_mark>is a tree, then the number of days needed is either k <tex2html_verbatim_mark>or k + 1 <tex2html_verbatim_mark>, where k <tex2html_verbatim_mark>is the maximum number of vertices contained in a directed path of G <tex2html_verbatim_mark>, i.e., a path P = (x1x2,..., xk) <tex2html_verbatim_mark>, where for each i = 1, 2,..., k - 1 <tex2html_verbatim_mark>, there is a directed edge from xi <tex2html_verbatim_mark>to xi+1 <tex2html_verbatim_mark>.

Figure 1 below is such an example. There are six jobs: 1, 2, 3, 4, 5, 6. From this figure, we know that job 1 and job 2 must be done in different dates. Job 1 needs to be done before job 3, job 3 before job 5, job 2 before job 4 and job 4 before job 6. It is easy to verify that the minimum days to finish all the jobs is 4 days. In this example, the maximum number k <tex2html_verbatim_mark>of vertices contained in a directed path is 3.

\epsfbox{p3683.eps}<tex2html_verbatim_mark>
Figure 1: Example

Input 

The input consists of a number of trees (whose edges may be directed or undirected), say T1T2,..., Tm <tex2html_verbatim_mark>, where m$ \le$20 <tex2html_verbatim_mark>. Each tree has at most 200 vertices. We represent each tree as a rooted tree (just for convenience of presentation, the root is an arbitrarily chosen vertex). Information of each of the trees are contained in a number of lines. Each line starts with a vertex (which is a positive integer) followed by all its sons (which are also positive integers), then followed by a 0. Note that 0 is not a vertex, and it indicates the end of that line. Now some of the edges are directed. The direction of an edge can be from father to son, and can also be from son to father. If the edge is from father to son, then we put a letter `` d" after that son (meaning that it is a downward edge). If the edge is from son to father, then we put a letter `` u" after that son (meaning that it is an upward edge). If the edge is undirected then we do not put any letter after the son.

The first case of the sample input below is the example in Figure 1.

Consecutive vertices (numbers or numbers with a letter after it) in a line are separated by a single space. A line containing a single 0 means the end of that tree. The next tree starts in the next line. Two consecutive lines of single 0 means the end of the input.

Output 

The output contains one line for each test case. Each line contains a number, which is the minimum number of days to finish all the jobs in that test case.

Sample Input 

1 2 3d 0
2 4d 0
3 5d 0
4 6d 0
0 
1 2d 3u 4 0
0 
1 2d 3 0
2 4d 5d 10 0
3 6d 7d 11 0
6 8d 9 12 0
0 
1 2 3 4 0
2 5d 0
3 6d 0
4 7d 0
5 8d 0
6 9d 0
7 10d 0
0 
0

Sample Output 

4 
3 
4 
3
 
【题意】
  有n个恰好需要一天完成的任务,要求用最少时间做完。任务可以并行完成并行完成,但必须满足一些约束。约束是给一个图,A-B表示A、B不能同一天完成。A->B表示先做A才能做B。输入保证约束图是一棵树某些边定向而成的,问完成所有任务的最少时间。

【分析】
  

  紫书上的题,挺难的。
  二分答案x,判断是否可以给无向边定向,使得最长链点数不超过x。
  f[i]表示以i为根的子树内全部边定向后,最长链点数不超过x的前提下,形如“后代到i”的最长链的最小值,同理定义g[i]为形如“i到后代”的最长链的最小值。
  设y为i的某个孩子。

  转化无向边的过程分为两种情况:

  a. 如果是有向边,则经过子树的根结点的最值贡献为f+g+1。

  b. 如果一个结点a的子结点存在无向边,求f[i]时,目的是f[i]+g[i]+1<=x的前提下f[i]最小。首先把所有没有定向的f(y)排序,假设把第p小的定位向上,那么比p小的也定为向上百利而无一害。然后剩下的变成向下,判断这样的f是否成立,不成立f为INF。

  这种方法时间复杂度:n^2*logn

 

代码如下:

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 using namespace std;
  7 #define Maxn 210
  8 #define INF 0xfffffff
  9 
 10 struct node
 11 {
 12     int x,y,next,p;
 13 }t[2*Maxn];int len;
 14 
 15 int first[Maxn],n;
 16 
 17 void ins(int x,int y,int p)
 18 {
 19     t[++len].x=x;t[len].y=y;t[len].p=p;
 20     t[len].next=first[x];first[x]=len;
 21 }
 22 
 23 int mymax(int x,int y) {return x>y?x:y;}
 24 int mymin(int x,int y) {return x<y?x:y;}
 25 
 26 bool init()
 27 {
 28     int x,y;
 29     char c;
 30     len=0;
 31     memset(first,0,sizeof(first));
 32     n=0;
 33     while(1)
 34     {
 35         scanf("%d",&x);getchar();
 36         if(x==0) break;
 37         n=x;
 38         while(1)
 39         {
 40             scanf("%d",&y);
 41             n=mymax(n,y);
 42             if(y==0) {getchar();break;}
 43             scanf("%c",&c);
 44             if(c=='u'||c=='d')
 45             {
 46                 if(c=='d') ins(x,y,1),ins(y,x,-1);
 47                 else ins(y,x,1),ins(x,y,-1);
 48                 scanf("%c",&c);
 49             }
 50             else {ins(x,y,0);ins(y,x,0);}
 51         }
 52     }
 53     if(n==0) return 0;
 54     return 1;
 55 }
 56 
 57 int f[Maxn],g[Maxn];
 58 
 59 struct hp
 60 {
 61     int x,y;
 62 }a[Maxn];
 63 
 64 bool cmp(hp x,hp y) {return x.x<y.x;}
 65 bool cmp2(hp x,hp y) {return x.y<y.y;} 
 66 
 67 bool dp(int x,int fa,int now)
 68 {
 69     f[x]=g[x]=INF;
 70     int mxf=-1,mxg=-1;
 71     for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
 72     {
 73         int y=t[i].y;
 74         dp(y,x,now);
 75         if(f[y]>=INF) return 0;
 76     }
 77     int cnt=0;
 78     for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
 79     {
 80         int y=t[i].y;
 81         if(t[i].p==0)
 82         {
 83             a[++cnt].x=f[y];
 84             a[cnt].y=g[y];
 85         }
 86         else if(t[i].p==-1) mxf=mymax(mxf,f[y]);
 87         else mxg=mymax(mxg,g[y]);
 88     }
 89     if(cnt==0) 
 90     {
 91         if(mxf+mxg+3<=now)
 92           f[x]=mymin(f[x],mxf+1),g[x]=mymin(g[x],mxg+1);
 93     }
 94     else
 95     {
 96         sort(a+1,a+1+cnt,cmp);
 97         int pg=mxg;
 98         for(int i=cnt;i>=0;i--)
 99         {
100             int nx=mymax(a[i].x,mxf);
101             if(nx+mxg+3<=now) f[x]=nx+1;
102             mxg=mymax(mxg,a[i].y);
103         }
104         // if(mxg+mxf<=now) f[x]=mxf+1;
105         sort(a+1,a+1+cnt,cmp2);
106         mxg=pg;
107         for(int i=cnt;i>=0;i--)
108         {
109             int nx=mymax(a[i].y,mxg);
110             if(nx+mxf+3<=now) g[x]=nx+1;
111             mxf=mymax(mxf,a[i].x);
112         }
113         // if(mxf+2<=now) g[x]=mxg+1;
114     }
115     return f[x]<INF;
116 }
117 
118 int ffind(int l,int r)
119 {
120     while(l<r)
121     {
122         int mid=(l+r)>>1;
123         if(dp(1,0,mid)) r=mid;
124         else l=mid+1;
125     }
126     printf("%d\n",l);
127 }
128 
129 int main()
130 {
131     a[0].x=a[0].y=-1;
132     while(1)
133     {
134         bool z=init();
135         if(z==0) break;
136         ffind(1,n);
137     }
138     return 0;
139 }
[UVA1380]

 

来一份GDXB的详细题解:

http://www.cnblogs.com/KonjakJuruo/p/5969831.html

balabalabala...

 

2016-10-17 20:26:08

 


 

 

  然而,事实上,因为n很小,有更简单的方法!!!n^3过了。。。

  dp[i][j]表示计算i这棵树,且i在第j天完成,的最短时间。

  dp[i][j]=max(dp[i][j],min(dp[y][k])) [y是i的孩子,k是y的可行时间]

 呵呵,100年打第一种方法,第二种方法秒A。呵呵~~

2016-10-17 21:00:06

转载于:https://www.cnblogs.com/Konjakmoyu/p/5971078.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值