Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class FastScanner {
- public BufferedReader br;
- public StringTokenizer st;
- public FastScanner(InputStream is) throws IOException {
- br = new BufferedReader(new InputStreamReader(is), 32768);
- st = null;
- }
- public String next() {
- while (st == null || !st.hasMoreTokens()) {
- try {
- st = new StringTokenizer(br.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return st.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public double nextDouble() {
- return Double.parseDouble(next());
- }
- public String nextLine() {
- String str = "";
- try {
- str = br.readLine();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return str;
- }
- }
- class Edge {
- private int fromNodeIndex;
- private int toNodeIndex;
- private int length;
- public Edge(int fromNodeIndex, int toNodeIndex, int length) {
- this.fromNodeIndex = fromNodeIndex;
- this.toNodeIndex = toNodeIndex;
- this.length = length;
- }
- public int getFromNodeIndex() {
- return fromNodeIndex;
- }
- public int getToNodeIndex() {
- return toNodeIndex;
- }
- public int getLength() {
- return length;
- }
- public int getNeighbourIndex(int nodeIndex) {
- if (this.fromNodeIndex == nodeIndex) {
- return this.toNodeIndex;
- } else {
- return this.fromNodeIndex;
- }
- }
- public String toString() {
- return fromNodeIndex + " " + toNodeIndex + " " + length;
- }
- }
- class Node {
- private int distanceFromSource = Integer.MAX_VALUE;
- private boolean visited;
- private ArrayList<Edge> edges = new ArrayList<Edge>();
- public int getDistanceFromSource() {
- return distanceFromSource;
- }
- public void setDistanceFromSource(int distanceFromSource) {
- this.distanceFromSource = distanceFromSource;
- }
- public boolean isVisited() {
- return visited;
- }
- public void setVisited(boolean visited) {
- this.visited = visited;
- }
- public ArrayList<Edge> getEdges() {
- return edges;
- }
- public void setEdges(ArrayList<Edge> edges) {
- this.edges = edges;
- }
- }
- class Graph {
- private Node[] nodes;
- private int noOfNodes;
- private Edge[] edges;
- private int noOfEdges;
- public Graph(Edge[] edges) {
- this.edges = edges;
- this.noOfNodes = calculateNoOfNodes(edges);
- this.nodes = new Node[this.noOfNodes];
- for (int n = 0; n < this.noOfNodes; n++) {
- this.nodes[n] = new Node();
- }
- this.noOfEdges = edges.length;
- for (int edgeToAdd = 0; edgeToAdd < this.noOfEdges; edgeToAdd++) {
- this.nodes[edges[edgeToAdd].getFromNodeIndex()].getEdges().add(edges[edgeToAdd]);
- this.nodes[edges[edgeToAdd].getToNodeIndex()].getEdges().add(edges[edgeToAdd]);
- }
- }
- private int calculateNoOfNodes(Edge[] edges) {
- int noOfNodes = 0;
- for (Edge e : edges) {
- if (e.getToNodeIndex() > noOfNodes)
- noOfNodes = e.getToNodeIndex();
- if (e.getFromNodeIndex() > noOfNodes)
- noOfNodes = e.getFromNodeIndex();
- }
- noOfNodes++;
- return noOfNodes;
- }
- public void calculateShortestDistances() {
- this.nodes[0].setDistanceFromSource(0);
- int nextNode = 0;
- for (int i = 0; i < this.nodes.length; i++) {
- ArrayList<Edge> currentNodeEdges = this.nodes[nextNode].getEdges();
- for (int joinedEdge = 0; joinedEdge < currentNodeEdges.size(); joinedEdge++) {
- int neighbourIndex = currentNodeEdges.get(joinedEdge).getNeighbourIndex(nextNode);
- if (!this.nodes[neighbourIndex].isVisited()) {
- int tentative = this.nodes[nextNode].getDistanceFromSource()
- + currentNodeEdges.get(joinedEdge).getLength();
- if (tentative < nodes[neighbourIndex].getDistanceFromSource()) {
- nodes[neighbourIndex].setDistanceFromSource(tentative);
- }
- }
- }
- nodes[nextNode].setVisited(true);
- nextNode = getNodeShortestDistanced();
- }
- }
- private int getNodeShortestDistanced() {
- int storedNodeIndex = 0;
- int storedDist = Integer.MAX_VALUE;
- for (int i = 0; i < this.nodes.length; i++) {
- int currentDist = this.nodes[i].getDistanceFromSource();
- if (!this.nodes[i].isVisited() && currentDist < storedDist) {
- storedDist = currentDist;
- storedNodeIndex = i;
- }
- }
- return storedNodeIndex;
- }
- public int getDist(){
- return nodes[nodes.length - 1].getDistanceFromSource();
- }
- public Node[] getNodes() {
- return nodes;
- }
- public int getNoOfNodes() {
- return noOfNodes;
- }
- public Edge[] getEdges() {
- return edges;
- }
- public int getNoOfEdges() {
- return noOfEdges;
- }
- }
- public class Main {
- public static void main(String[] args) throws IOException {
- InputStream is = new FileInputStream("pump.in");
- PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("pump.out")));
- FastScanner sc = new FastScanner(is);
- int N = sc.nextInt();
- int M = sc.nextInt();
- Edge[] edges = new Edge[M];
- int n1, n2, f, c;
- ArrayList<Integer> flows = new ArrayList<Integer>();
- for (int i = 0; i < M; i++) {
- n1 = sc.nextInt();
- n2 = sc.nextInt();
- c = sc.nextInt();
- f = sc.nextInt();
- edges[i] = new Edge(n1 - 1, n2 - 1, c);
- flows.add(f);
- }
- for (int i = 0; i < edges.length; i++) {
- System.out.println(edges[i]);
- }
- Graph g = new Graph(edges);
- g.calculateShortestDistances();
- int cost = g.getDist();
- int flow = min(flows);
- int out = (1000000 * flow)/cost;
- System.out.println(out);
- pw.write(out);
- }
- public static int min(ArrayList<Integer> arr){
- int ret = 10000;
- for(int i: arr){
- if(i < ret){
- ret = i;
- }
- }
- return ret;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement