public static void Main(string[] args) { var N = Console.Read(); Console.ReadLine(); var G = new EdgeWeightedDigraph(2 * N + 2); var s = 2 * N; var t = 2 * N + 1; for (int i = 0; i < N; i++) { var a = Console.ReadLine().Split(' '); var duation = double.Parse(a[0]); G.AddEdge(new DirectedEdge(i, i + N, duation)); G.AddEdge(new DirectedEdge(s, i, 0.0)); G.AddEdge(new DirectedEdge(i + N, t, 0.0)); for (int j = 1; j < a.Length; j++) { var successor = int.Parse(a[j]); G.AddEdge(new DirectedEdge(i + N, successor, 0.0)); } } var lp = new AcyclicLP(G, s); Console.WriteLine("Start times: "); for (int i = 0; i < N; i++) { Console.Write("{0} : {1:f1}\n", i, lp.DistToW(i)); } Console.WriteLine("Finish time: {0:f1}", lp.DistToW(t)); }
private void FindNegativeCycle() { var V = EdgeTo.Length; var spt = new EdgeWeightedDigraph(V); for (int v = 0; v < V; v++) { if (EdgeTo[v] != null) { spt.AddEdge(EdgeTo[v]); } } var cf = new EdgeWeightedCycleFinder(spt); Cycle = cf.Cycle; }
public static void Main(string[] args) { //using (var reader = new StreamReader("rates.txt")) { using (var reader = new StreamReader(args[0])) { var V = int.Parse(reader.ReadLine().Split(' ')[0]); var name = new string[V]; var G = new EdgeWeightedDigraph(V); for (int v = 0; v < V; v++) { var line = reader.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); name[v] = line[0]; for (int w = 0; w < V; w++) { var rate = double.Parse(line[w + 1]); if (rate != 1) { var e = new DirectedEdge(v, w, -Math.Log(rate)); Console.WriteLine("{0}-->{1} : {2}", v, w, -Math.Log(rate)); G.AddEdge(e); } } } var spt = new BellmanFordSP(G, 0); if (spt.HasNegativeCycle()) { var stake = 1000.0; foreach (var e in spt.NegativeCycle()) { Console.Write("{0:f5} {1} ", stake, name[e.From()]); stake *= Math.Exp(-e.Weight); Console.WriteLine("= {0:f5} {1}", stake, name[e.To()]); } } else { Console.WriteLine("No arbitrage opportunity"); } //Console.Read(); } }