UVa452 - Project Scheduling(AOE问题)

来源:互联网 发布:淘宝订单险坑卖家钱 编辑:程序博客网 时间:2024/06/05 09:13

 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

2A 5B 3 AD 2 AC 2 BF 2 CEE 4 DCA 5B 3 AD 2 AC 2 BF 2 CEE 4 DC

Sample Output

1616
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 0
原创粉丝点击