Description
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).
Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.
Input
Line 1: Three space-separated integers: N, ML, and MD.
Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
Output
Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.
Sample Input
4 2 1 1 3 10 2 4 20 2 3 3
Sample Output
27
Hint
Explanation of the sample:
There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
Solution below . . .
/* * We can solve it by building a graph with the constraints as edges, * then finding a path through the graph. The "dislike" constraints * are represented as negative-weight edges. * * So in the sample input, we have three edges; * * 1 -> 3 with weight 10 * 2 -> 4 with weight 20 * 3 -> 2 with weight -3 (not 2 -> 3) * * The path from 1 to 4 is * * 1 -> 3 -> 2 -> 4 = 10 - 3 + 20 = 27 * * The Bellman-Ford path-finding algorithm gives us everything we need. * It handles negative edge weights and detects cycles. */ import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * The BellmanFord class represents a data type for solving * single-source shortest paths problem in edge-weighted digraphs. * * The edge weights can be positive, negative, or zero. * * This class finds either a shortest path from the source vertex s * to every other vertex or a negative cycle reachable from the * source vertex. */ class BellmanFord { private int[] distTo; private DirectedEdge[] edgeTo; private int n, m; private boolean cycle; private final int INF = 999999999; public BellmanFord(EdgeWeightedDigraph G, int s) { n = G.V(); m = G.E(); edgeTo = G.edges(); distTo = new int[n+1]; cycle = false; for (int i = 1; i <= n; i++) distTo[i] = INF; distTo[s] = 0; boolean flag = true; for (int i = 1; i < n; i++) { for (int j = 0; j < m; j++) { int u = edgeTo[j].from(); int v = edgeTo[j].to(); int t = edgeTo[j].weight(); if (distTo[v] > distTo[u] + t) { distTo[v] = distTo[u] + t; flag = false; } } } if (!flag) { // check for cycle for (int i = 0; i < m; i++) { if (distTo[edgeTo[i].to()] > distTo[edgeTo[i].from()] + edgeTo[i].weight()) cycle = true; } } } public boolean hasCycle() { return cycle; } public int distTo(int v) { return distTo[v]; } } /** * The EdgeWeightedDigraph class represents a edge-weighted * digraph of vertices 0 through V - 1, where each directed * edge is of type DirectedEdge. */ class EdgeWeightedDigraph { private final int V; private int E; private List<DirectedEdge> edges; public EdgeWeightedDigraph(int V) { this.V = V; this.E = 0; edges = new ArrayList<DirectedEdge>(); } public int V() { return V; } public int E() { return E; } public void addEdge(DirectedEdge e) { edges.add(e); E++; } public DirectedEdge[] edges() { return edges.toArray(new DirectedEdge[edges.size()]); } } /** * The DirectedEdge class represents a weighted edge in an * EdgeWeightedDigraph. */ class DirectedEdge { private final int v; private final int w; private final int weight; public DirectedEdge(int v, int w, int weight) { this.v = v; this.w = w; this.weight = weight; } public int from() { return v; } public int to() { return w; } public int weight() { return weight; } } public class Main { private static final int INF = 999999999; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N, ML, MD; N = sc.nextInt(); ML = sc.nextInt(); MD = sc.nextInt(); EdgeWeightedDigraph G = new EdgeWeightedDigraph(N); DirectedEdge e; int v, w, t; for (int i = 1; i <= ML; i++) { v = sc.nextInt(); w = sc.nextInt(); t = sc.nextInt(); e = new DirectedEdge(v, w, t); G.addEdge(e); } for (int i = 1; i <= MD; i++) { w = sc.nextInt(); v = sc.nextInt(); t = sc.nextInt(); e = new DirectedEdge(v, w, -t); G.addEdge(e); } sc.close(); BellmanFord sp = new BellmanFord(G, 1); if (sp.hasCycle()) System.out.println(-1); else if (sp.distTo(N) == INF) System.out.println(-2); else System.out.println(sp.distTo(N)); } }