UVa452 - Project Scheduling(AOE问题)

 Project Scheduling 

A project management technique called Pert involves breaking a largeproject into a number of tasks, estimating the time required to perform eachtask, and determining which tasks can not be started until others have beencompleted. The project is then summarized in chart form. For example, thechart (which corresponds to the sample input below)

indicates that tasks A, B, C, D, E and F each take 5, 3, 2, 2, 4, and 2 days respectively, that task E cannot complete until C and D are both completed, but that D can be performed in parallel with B and C.

Write a program that accepts a Pert chart and computes the amountof time required to complete a project.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Input will be from 1 to 27 lines, each corresponding to a different task. Each line will contain:

  1. A single upper case letter serving as the name of a task.
  2. An integer indicating the number of days required to complete that task.
  3. 0-26 additional uppercase letters, each indicating another task that must complete before this one can begin.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

The output is a single integer indicating the amount of time that will pass before all tasks can complete.

Sample Input

2

A 5
B 3 A
D 2 A
C 2 B
F 2 CE
E 4 DC

A 5
B 3 A
D 2 A
C 2 B
F 2 CE
E 4 DC

Sample Output

16

16
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.Arrays;

public class Main 
{
	public static final boolean DEBUG = false;
	public static final int N = 26;
	public BufferedReader cin;
	public PrintWriter cout;
	public HashMap<Character, Integer> chMap = new HashMap<Character, Integer>();
	public int[][] g = new int[N][N];
	public int[] t = new int[N];
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next()
	{ 
		try {
			return cin.readLine();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public void input() 
	{
		String s;
		StringTokenizer tokenizer;
		
		for (int i = 0; i < N; i++) Arrays.fill(g[i], 0);
		Arrays.fill(t, 0);
		
		chMap.clear();
		while (true) {
			s = next();
			if (s == null) break;
			else if (s.compareTo("") == 0) break;
			else {
				tokenizer = new StringTokenizer(s);
				int cnt = 0;
				int u = 0, v = 0;
				
				while (tokenizer.hasMoreTokens()) {
					String tmp = tokenizer.nextToken();
					char ch = tmp.charAt(0);
					if (Character.isLetter(ch)) {
						if (cnt == 0) {
							if (chMap.containsKey(ch)) {
								u = chMap.get(ch);
							} else {
								u = chMap.size();
								chMap.put(ch, u);
							}
						} else if (cnt == 2) {
							for (int i = 0, len = tmp.length(); i < len; i++) {
								ch = tmp.charAt(i);
								if (chMap.containsKey(ch)) {
									v = chMap.get(ch);
								} else {
									v = chMap.size();
									chMap.put(ch, v);
								}
								g[v][u] = 1;
							}
							
						}
					} else {
						t[u] = Integer.parseInt(tmp);
					}
					
					cnt++;
				}
			}
		}
		
	}

	
	
	public void solve(int cas) 
	{
		int size = chMap.size();
		int[] inDegree = new int[size];
		
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (g[i][j] > 0) inDegree[j]++;
			}
		}
		
		int[] topoSeq = new int[size];
		for (int i = 0; i < size; i++) {
			int j = 0;
			while (j < size && inDegree[j] != 0) j++;
			if (j >= size) break;
			topoSeq[i] = j;
			inDegree[j] = 0xff;
			for (int k = 0; k < size; k++) {
				if (g[j][k] > 0) inDegree[k]--;
			}
		}
		
		
		int[] b = new int[size], p = new int[size];
		for (int i = 0; i < size; i++) {
			b[topoSeq[i]] = t[topoSeq[i]];
			p[topoSeq[i]] = -1;
		}
		
		
		for (int i = 1; i < size; i++) {
			for (int j = 0; j < i; j++) {
				if (g[topoSeq[j]][topoSeq[i]] > 0) {
					if (b[topoSeq[j]] + t[topoSeq[i]] > b[topoSeq[i]]) {
						b[topoSeq[i]] = b[topoSeq[j]] + t[topoSeq[i]];
						p[topoSeq[i]] = topoSeq[j];
					}
				}
			}
		}
		
		
		
		int ans = Integer.MIN_VALUE;
		for (int i = 0; i < size; i++) {
			ans = Math.max(ans, b[i]);
		}
		cout.println(ans);
		
		if (cas != 0) cout.println();
		cout.flush();
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		int t = Integer.parseInt(solver.next());
		solver.next();
		while (t-- > 0) {
			solver.input();
			solver.solve(t);
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值