01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package algs44;
import stdlib.*;
/* ***********************************************************************
* Compilation: javac CPM.java
* Execution: java CPM < input.txt
* Dependencies: EdgeWeightedDigraph.java AcyclicDigraphLP.java StdOut.java
* Data files: http://algs4.cs.princeton.edu/44sp/jobsPC.txt
*
* Critical path method.
*
* % java CPM < jobsPC.txt
* job start finish
* --------------------
* 0 0.0 41.0
* 1 41.0 92.0
* 2 123.0 173.0
* 3 91.0 127.0
* 4 70.0 108.0
* 5 0.0 45.0
* 6 70.0 91.0
* 7 41.0 73.0
* 8 91.0 123.0
* 9 41.0 70.0
* Finish time: 173.0
*
*************************************************************************/
public class CPM {
public static void main(String[] args) {
StdIn.fromFile ("data/jobsPC.txt");
// number of jobs
int N = StdIn.readInt();
// source and sink
int source = 2*N;
int sink = 2*N + 1;
// build network
EdgeWeightedDigraph G = new EdgeWeightedDigraph(2*N + 2);
for (int i = 0; i < N; i++) {
double duration = StdIn.readDouble();
G.addEdge(new DirectedEdge(source, i, 0.0));
G.addEdge(new DirectedEdge(i+N, sink, 0.0));
G.addEdge(new DirectedEdge(i, i+N, duration));
// precedence constraints
int M = StdIn.readInt();
for (int j = 0; j < M; j++) {
int precedent = StdIn.readInt();
G.addEdge(new DirectedEdge(N+i, precedent, 0.0));
}
}
// compute longest path
AcyclicLP lp = new AcyclicLP(G, source);
// print results
StdOut.println(" job start finish");
StdOut.println("--------------------");
for (int i = 0; i < N; i++) {
StdOut.format("%4d %7.1f %7.1f\n", i, lp.distTo(i), lp.distTo(i+N));
}
StdOut.format("Finish time: %7.1f\n", lp.distTo(sink));
}
}
|