A Scheduling Problem UVA - 1380

有点难的一道题目,看题解+代码终于过了......

对有向边和无向边进行分开存放,然后dfs,找出最长的有向边的长度。接着找出“根节点”(该节点没有父节点,并且该节点存在孩子节点),然后从该节点开始进行动态规划:首先判断该节点的孩子节点是否为零,如果为零,那么该节点的up变量值和down变量值均为零(up和down的定义和紫书的f、g一样)。否则,依次遍历该节点的孩子节点,这里要多加一层判断,注意不能“走回头路”(见代码注释)。同时递归计算该节点的孩子节点的up和down的值,同时注意该节点和孩子节点的边的性质,如果该边是无向边,要注意保存,便于后续的判断。如果该边是指向该节点的注意要更新up的值,如果是该边是指向孩子节点的注意要更新该节点的down的值。在这一轮的操作全部结束的时候,要判断与该节点相连接的无向边的个数,如果与该节点相连的无向边的个数为0,那么就可以保存前面计算出的up和down的值,同时判断up和down之和是否超过了目前已知最长的无向边的长度,如果超过返回false,表示要多用一天的时间,否则直接返回false,同时注意在返回false之前要将up和down的值置为无穷大,便于上一层递归的判断。如果当前的节点的确存在和其他节点的无向边,那么就按照紫书上的思路,将所有无项边连接的子节点按照down从小到大的排序进行一轮处理,然后按照up的值从小到大的排序进行下一轮的处理,得出最终的结果,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;

struct edge{
	int u, v, d;//d=0,undirect;d=1,down;d=2,up
	edge(int a = 0, int b = 0, int c = 0){
		u = a; v = b; d = c;
	}
};

struct child{
	int id,upp, downn;
	child(int a = 0, int b = 0, int c = 0){
		id = a, upp = b, downn = c;
	}
};

bool compare_upp(const child a,const child b){
	return a.upp < b.upp;
}

bool compare_down(const child a, const child b){
	return a.downn < b.downn;
}

const int INF = 1000000000;

vector<edge> v[210];
int down[210];
int up[210];
int maxlen,root;
int total;
int parent[210];

bool init(){
	for (int i = 0; i < 210; i++) v[i].clear();
	memset(parent, 0, sizeof(parent));
	memset(down, 0, sizeof(down));
	memset(up, 0, sizeof(up));
	int a;
	bool last = true;
	while (cin >> a&&a){
		string s;
		total = max(total, a);
		last = false;
		while (cin >> s&&s != "0"){
			int l = s.size();
			if (s[l - 1] >= 'a'&&s[l - 1] <= 'z'){
				string s2 = s.substr(0,l-1);
				stringstream is(s2);
				int t;
				is >> t;
				total = max(total,t);
				if (s[l - 1] == 'd'){
					v[a].push_back(edge(a, t, 1));
					v[t].push_back(edge(t, a, 2));
					parent[t] = 1;
				}
				else{
					v[a].push_back(edge(a, t, 2));
					v[t].push_back(edge(t, a, 1));
					parent[t] = 1;
				}
			}
			else{
				stringstream is(s);
				int t;
				is >> t;
				total = max(total, t);
				v[a].push_back(edge(a,t,0));
				parent[t] = 1;
			}
		}
	}
	for (int i = 1; i <= total; i++){
		if (parent[i] == 0 && v[i].size() != 0){
			root = i;
			break;
		}
	}
	if (last) return false;
	return true;
}

int dfs(int start){
	int ans = 0;
	for (int i = 0; i < v[start].size(); i++){
		int ind = v[start][i].v;
		if (v[start][i].d == 1) ans = max(ans, dfs(ind) + 1);
	}
	return ans;
}

bool dp(int start,int pre){
	if (v[start].size() == 0){
		up[start] = down[start] = 0;
		return true;
	}
	vector<child> children;
	int up_s = 0, down_s = 0;
	for (int i = 0; i < v[start].size(); i++){
		int ind = v[start][i].v;
		if (ind == pre) continue;
		dp(ind, start);
		int d = v[start][i].d;
		if (d == 0) children.push_back(child(ind, up[ind], down[ind]));
		else if (d == 2) up_s = max(up_s, up[ind]+1);
		else down_s = max(down_s, down[ind]+1);
	}
	if (children.empty()){
		up[start] = up_s, down[start] = down_s;
		if (up_s + down_s > maxlen) up[start] = down[start] = INF;
		return up[start] < INF;
	}
	up[start] = INF, down[start] = INF;
	int amount = children.size();

	sort(children.begin(),children.end(),compare_upp);
	int max_d[210];
	max_d[amount - 1] = children[amount - 1].downn;
	for (int i = amount - 2; i >= 0; i--){
		max_d[i] = max(children[i].downn, max_d[i + 1]);
	}
	for (int i = 0; i <= amount; i++){
		int t1 = up_s, t2 = down_s;
		if (i>0) t1 = max(t1, children[i - 1].upp+1);
		if (i < amount) t2 = max(t2, max_d[i]+1);
		if (t1 + t2 <= maxlen) up[start] = min(up[start], t1);
	}

	sort(children.begin(), children.end(), compare_down);
	int max_u[210];
	max_u[amount - 1] = children[amount - 1].upp;
	for (int i = amount - 2; i >= 0; i--){
		max_u[i] = max(children[i].upp,max_u[i+1]);
	}
	for (int i = 0; i <= amount; i++){
		int t1 = up_s, t2 = down_s;
		if (i<amount) t1 = max(t1, max_u[i] + 1);
		if (i>0) t2 = max(t2, children[i-1].downn + 1);
		if (t1 + t2 <= maxlen) down[start] = min(down[start], t2);
	}
	
	return up[start] < INF;
}

int main(){
	total = 0;
	while (init()){
		maxlen = 0;
		for (int i = 1; i <= total; i++){
			maxlen = max(maxlen, dfs(i));
		}
		if (dp(root, -1)) cout << maxlen + 1 << endl;
		else cout << maxlen + 2 << endl;
		total = 0;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Practice 1 Date: Monday, March 18th, 2013 We highly encourage being environment friendly and trying all problems on your own. Implement exercise 2.3-7. Implement priority queue. Implement Quicksort and answer the following questions. (1) How many comparisons will Quicksort do on a list of n elements that all have the same value? (2) What are the maximum and minimum number of comparisons will Quicksort do on a list of n elements, give an instance for maximum and minimum case respectively. Give a divide and conquer algorithm for the following problem: you are given two sorted lists of size m and n, and are allowed unit time access to the ith element of each list. Give an O(lg m + lgn) time algorithm for computing the kth largest element in the union of the two lists. (For simplicity, you can assume that the elements of the two lists are distinct). Practice 2 Date: Monday, April 1st, 2013 We highly encourage being environment friendly and trying all problems on your own. Matrix-chain product. The following are some instances. Longest Common Subsequence (LCS). The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Longest Common Substring. The following are some instances. X: xzyzzyx Y: zxyyzxz X:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD Y:MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG Max Sum. The following is an instance. (-2,11,-4,13,-5,-2) Shortest path in multistage graphs. Find the shortest path from 0 to 15 for the following graph.   A multistage graph is a graph (1) G=(V,E) with V partitioned into K >= 2 disjoint subsets such that if (a,b) is in E, then a is in Vi , and b is in Vi+1 for some subsets in the partition; and (2) | V1 | = | VK | = 1.     Practice 3 Date: Monday, April 15th, 2013 We highly encourage being environment friendly and trying all problems on your own. Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem both as fractional knapsack and 0/1 knapsack. A simple scheduling problem. We are given jobs j1, j2… jn, all with known running times t1, t2… tn, respectively. We have a single processor. What is the best way to schedule these jobs in order to minimize the average completion time. Assume that it is a nonpreemptive scheduling: once a job is started, it must run to completion. The following is an instance. (j1, j2, j3, j4) : (15,8,3,10) Single-source shortest paths. The following is the adjacency matrix, vertex A is the source.  A B C D E A -1 3 B 3 2 2 C D 1 5 E -3 All-pairs shortest paths. The adjacency matrix is as same as that of problem 3.(Use Floyd or Johnson’s algorithm)     Practice 4 Date: Monday, May 8th, 2013 We highly encourage being environment friendly and trying all problems on your own. 0/1 Knapsack Problem. There are 5 items that have a value and weight list below, the knapsack can contain at most 100 Lbs. Solve the problem using back-tracking algorithm and try to draw the tree generated. Solve the 8-Queen problem using back-tracking algorithm.    

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值