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.

<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 m20 <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

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 210
#define INF 0xfffffff struct node
{
int x,y,next,p;
}t[*Maxn];int len; int first[Maxn],n; void ins(int x,int y,int p)
{
t[++len].x=x;t[len].y=y;t[len].p=p;
t[len].next=first[x];first[x]=len;
} int mymax(int x,int y) {return x>y?x:y;}
int mymin(int x,int y) {return x<y?x:y;} bool init()
{
int x,y;
char c;
len=;
memset(first,,sizeof(first));
n=;
while()
{
scanf("%d",&x);getchar();
if(x==) break;
n=x;
while()
{
scanf("%d",&y);
n=mymax(n,y);
if(y==) {getchar();break;}
scanf("%c",&c);
if(c=='u'||c=='d')
{
if(c=='d') ins(x,y,),ins(y,x,-);
else ins(y,x,),ins(x,y,-);
scanf("%c",&c);
}
else {ins(x,y,);ins(y,x,);}
}
}
if(n==) return ;
return ;
} int f[Maxn],g[Maxn]; struct hp
{
int x,y;
}a[Maxn]; bool cmp(hp x,hp y) {return x.x<y.x;}
bool cmp2(hp x,hp y) {return x.y<y.y;} bool dp(int x,int fa,int now)
{
f[x]=g[x]=INF;
int mxf=-,mxg=-;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
dp(y,x,now);
if(f[y]>=INF) return ;
}
int cnt=;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
if(t[i].p==)
{
a[++cnt].x=f[y];
a[cnt].y=g[y];
}
else if(t[i].p==-) mxf=mymax(mxf,f[y]);
else mxg=mymax(mxg,g[y]);
}
if(cnt==)
{
if(mxf+mxg+<=now)
f[x]=mymin(f[x],mxf+),g[x]=mymin(g[x],mxg+);
}
else
{
sort(a+,a++cnt,cmp);
int pg=mxg;
for(int i=cnt;i>=;i--)
{
int nx=mymax(a[i].x,mxf);
if(nx+mxg+<=now) f[x]=nx+;
mxg=mymax(mxg,a[i].y);
}
// if(mxg+mxf<=now) f[x]=mxf+1;
sort(a+,a++cnt,cmp2);
mxg=pg;
for(int i=cnt;i>=;i--)
{
int nx=mymax(a[i].y,mxg);
if(nx+mxf+<=now) g[x]=nx+;
mxf=mymax(mxf,a[i].x);
}
// if(mxf+2<=now) g[x]=mxg+1;
}
return f[x]<INF;
} int ffind(int l,int r)
{
while(l<r)
{
int mid=(l+r)>>;
if(dp(,,mid)) r=mid;
else l=mid+;
}
printf("%d\n",l);
} int main()
{
a[].x=a[].y=-;
while()
{
bool z=init();
if(z==) break;
ffind(,n);
}
return ;
}

[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

【UVA 1380】 A Scheduling Problem (树形DP)的更多相关文章

  1. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  2. UVA 1380 A Scheduling Problem

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  4. UVA 10253 Series-Parallel Networks (树形dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Series-Parallel Networks Input: standard ...

  5. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  6. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  7. hdu5293 Tree chain problem 树形dp+线段树

    题目:pid=5293">http://acm.hdu.edu.cn/showproblem.php?pid=5293 在一棵树中,给出若干条链和链的权值.求选取不相交的链使得权值和最 ...

  8. UVa 12186 - Another Crisis(树形DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVa 1218 - Perfect Service(树形DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. 转:oracle ebs po模块一揽子采购协议小结

    转自:http://yedward.net/?id=193 oracle ebs po模块一揽子采购协议小结 本文总结oracle ebs采购订单(po)模块一揽子采购协议的相关知识,总结如下: 1. ...

  2. redis resque消息队列

    Resque 目前正在学习使用resque .resque-scheduler来发布异步任务和定时任务,为了方便以后查阅,所以记录一下. resque和resque-scheduler其优点在于功能比 ...

  3. Atom编辑器入门到精通(一) 安装及使用基础

    为什么选择使用Atom Atom是GitHub推出的一款编辑器,被称为21世纪的黑客编辑器,主要的特点是现代,易用,可定制.我之前用过多款编辑器,现在来总结一下个人对各编辑器的看法: Vim是我用的时 ...

  4. Sql触发器脚本

    ALTER Trigger [dbo].[test] --新建触发器 On [dbo].[test1] --在test1表中创建触发器 for insert --触发条件 As --事件触发后所要做的 ...

  5. 经典SQL语句大全(绝对的经典)

    ”,start为起始位置,length为字符串长度,实际应用中以len(expression)取得其长度3,right(char_expr,int_expr) 返回字符串右边第int_expr个字符, ...

  6. JavaScript高级程序设计(一):JavaScript简介

    一.JavaScript实现 1.一个完整的JavaScript包含三个部分组成: 1)ECMAScript 核心 2)DOM文档对象模型 3)BOM浏览器对象模型 2.文档对象模型(DOM) 文档对 ...

  7. Python问题记录:如何处理中文网页中的多余空格

    在制作Epub电子书的时候,因为有从网络上下载的格式比较混乱的电子书,现在打算自己用Pythonc处理一下. 1.如何删除掉网页(html)中的多余空额.尤其是包含在tag(标签:span.p)当中的 ...

  8. AJAX 一些常用方法

    abort() 停止当前请求getAllResponseHeaders() 返回包含HTTP请求的所有响应头信息,其中响应头包括Content-Length,Date,URI等内容.getRespon ...

  9. SSL Programming Tutorial

    SSL Programming Tutorial � Table of Contents [ � Index       This section demonstrates the implement ...

  10. crontab与环境变量

    一个shell脚本,直接执行能成功,但是加在crontab后确怎么也执行不成功. 问题的原因是:crontab的环境变量与直接执行用户的环境变量不一样. export PATH=$PATH:/sbin ...