Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package alg;
  7.  
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.Comparator;
  14.  
  15. /**
  16.  *
  17.  * @author marek
  18.  */
  19. public class Main {
  20.  
  21.     public static void main(String[] args) throws IOException {
  22.         initFILE("datapub/pub05.in");
  23. //        initSTND();
  24.  
  25.         int n = Reader.nextInt();
  26.         int m = Reader.nextInt();
  27.  
  28.         new Graph(n);
  29.         for (int i = 0; i < n; i++) {
  30.             Graph.nodes[i] = new Node(i);
  31.         }
  32.  
  33.         int c1, c2, c3;
  34.         for (int i = 0; i < m; i++) {
  35.  
  36.             c1 = Reader.nextInt();
  37.             c2 = Reader.nextInt();
  38.             c3 = Reader.nextInt();
  39.             Graph.nodes[c1].addEdge(c2, c3);
  40.             Graph.nodes[c2].addIn(c1);
  41.  
  42.             if (c3 > Graph.maxLength) {
  43.                 Graph.results = new ArrayList<>();
  44.  
  45.                 Graph.results.add(new Result(c1, c2));
  46.                 Graph.maxLength = c3;
  47.             } else if (c3 == Graph.maxLength) {
  48.                 Graph.results.add(new Result(c1, c2));
  49.             }
  50.         }
  51.  
  52.         Graph.start();
  53.  
  54.         printSortedResults();
  55.     }
  56.  
  57.     public static void printSortedResults() {
  58.         Collections.sort(Graph.results, new Comparator<Result>() {
  59.  
  60.             @Override
  61.             public int compare(Result o1, Result o2) {
  62.                 int i = o1.c1 - o2.c1;
  63.                 return (i == 0) ? o1.c2 - o2.c2 : i;
  64.             }
  65.         });
  66.  
  67.         for (Result result : Graph.results) {
  68.             System.out.println(result.c1 + " " + result.c2);
  69.         }
  70.     }
  71.  
  72.     public static void initSTND() throws IOException {
  73.         Reader.init(System.in);
  74.     }
  75.  
  76.     public static void initFILE(String file) throws FileNotFoundException, IOException {
  77.         Reader.init(new FileInputStream(file));
  78. //        Reader.init(new FileInputStream("datapub/private"));
  79.     }
  80. }
  81.  
  82.  
  83. ====================================================
  84.  
  85. package alg;
  86.  
  87. import java.util.ArrayList;
  88. import java.util.HashSet;
  89. import java.util.List;
  90.  
  91. /**
  92.  *
  93.  * @author marek
  94.  */
  95. public class Graph {
  96.  
  97.     static HashSet<Integer> miss = new HashSet<>();
  98.  
  99.     static Node[] nodes;
  100.  
  101.     static int maxLength = 0;
  102.  
  103.     static List<Result> results = new ArrayList<>();
  104.  
  105.     public Graph(int numberOfNodes) {
  106.         nodes = new Node[numberOfNodes];
  107.     }
  108.  
  109.     public static void start() {
  110.  
  111.         for (Node node : nodes) {
  112.             if (!miss.contains(node.localKey)) {
  113.                 node.evaluateOut();
  114.             }
  115.  
  116.         }
  117.  
  118.     }
  119. }
  120.  
  121. class Result  {
  122.  
  123.     int c1;
  124.     int c2;
  125.  
  126.     public Result(int c1, int c2) {
  127.         this.c1 = c1;
  128.         this.c2 = c2;
  129.     }
  130.  
  131. }
  132.  
  133. ================================================
  134.  
  135. package alg;
  136.  
  137. import java.util.ArrayList;
  138. import java.util.HashMap;
  139. import java.util.HashSet;
  140. import java.util.Map;
  141.  
  142. public class Node {
  143.  
  144.     static int i = 0;
  145.  
  146.     int localKey;
  147.  
  148.     int oneout;
  149.     int oneoutv;
  150.  
  151.     HashSet<Integer> in = new HashSet<>();
  152.     HashMap<Integer, Integer> values = new HashMap<>();
  153.     HashMap<Integer, Integer> out = new HashMap<>();
  154.  
  155.     public Node(int key) {
  156.         this.localKey = key;
  157.  
  158.     }
  159.  
  160.     public void addEdge(int key, int value) {
  161.         out.put(key, value);
  162.  
  163.         if (out.size() == 1) {
  164.             oneout = key;
  165.             oneoutv = value;
  166.         } else if (out.size() == 2) {
  167.             values.put(localKey, 0);
  168.         }
  169.     }
  170.  
  171.     public void addIn(int key) {
  172.         in.add(key);
  173.     }
  174.  
  175.     private void addValues(HashMap<Integer, Integer> map, int plusEdge) {
  176.         if (in.size() == 1 && out.size() == 1) {
  177.             Graph.miss.add(localKey);
  178.             Graph.nodes[oneout].addValues(map, plusEdge + oneoutv);
  179.         } else {
  180.             Integer key;
  181.             Integer value;
  182.             Integer v2;
  183.             int newValue;
  184.             for (Map.Entry<Integer, Integer> entrySet : map.entrySet()) {
  185.                 key = entrySet.getKey();
  186.                 value = entrySet.getValue();
  187.  
  188.                 newValue = value + plusEdge;
  189.                 if ((values.containsKey(key))) {
  190.  
  191.                     if (values.get(key) <= newValue) {
  192.                         values.put(key, newValue);
  193.  
  194.                     }
  195.  
  196.                 } else {
  197.                     values.put(key, newValue);
  198.  
  199.                 }
  200.  
  201.                 if (in.contains(key)) {
  202.  
  203.                     if (newValue >= Graph.maxLength) {
  204.                         if (newValue > Graph.maxLength) {
  205.                             Graph.maxLength = newValue;
  206.  
  207.                             Graph.results = new ArrayList<>();
  208.  
  209.                         }
  210. //                                System.out.println("add " + key + " + " + localKey);
  211.                         Graph.results.add(new Result(key, localKey));
  212.                     }
  213.                 }
  214.             }
  215.         }
  216.  
  217.     }
  218.  
  219.     public void evaluateOut() {
  220.  
  221.         for (Map.Entry<Integer, Integer> entrySet : out.entrySet()) {
  222.             Integer key = entrySet.getKey();
  223.             Integer value = entrySet.getValue();
  224.  
  225.             Graph.nodes[key].addValues(values, value);
  226.  
  227.         }
  228.     }
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement