Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class PF {
  5.     static int testcase = 0;
  6.     static Scanner io = null;
  7.     static ArrayList<String> from = new ArrayList<String>();
  8.     static ArrayList<String> to = new ArrayList<String>();
  9.     static ArrayList<Integer> w = new ArrayList<Integer>();
  10.     static String start = "";
  11.     static String end = "";
  12.     static Stack<String> st = new Stack<String>();
  13.     static Stack<String> sttmp = new Stack<String>();
  14.     static int min = Integer.MAX_VALUE;
  15.  
  16.     public static void main(String[] args) throws FileNotFoundException {
  17.         io = new Scanner(new File("f.in"));
  18.         testcase = Integer.parseInt(io.nextLine());
  19.         for (int tc = 0; tc < testcase; tc++) {
  20.             //----------//
  21.             System.out.println(tc+1);
  22.             //----------//
  23.             importData();
  24.             tree(start, end);
  25.             printSt(sttmp);
  26.             System.out.println(" "+min);
  27.             int tmpMin = min;
  28.             String tmpc = start;
  29.             start = end;
  30.             end = tmpc;
  31.             st.clear();
  32.             min = Integer.MAX_VALUE;
  33.             tree(start, end);
  34.             printSt(sttmp);
  35.             System.out.print(" "+min);
  36.             tmpMin += min;
  37.             System.out.print("\n"+tmpMin);
  38.            
  39.             //-----reset ------//
  40.             from = new ArrayList<String>();
  41.             to = new ArrayList<String>();
  42.             w = new ArrayList<Integer>();
  43.             start = "";
  44.             end = "";
  45.             st = new Stack<String>();
  46.             min = Integer.MAX_VALUE;
  47.         }
  48.     }
  49.  
  50.     private static void tree(String s, String t) {
  51.         if (st.isEmpty()) {
  52.             st.push(s);
  53.         }
  54.         if (s.equals(t)) {
  55.             int p = instack();
  56.             if(p < min)
  57.             {
  58.                 min =  p;
  59.                 sttmp = (Stack)st.clone();
  60.             }
  61.         } else {
  62.             for (int deep = 0; deep < from.size(); deep++) {
  63.                 if (from.get(deep).equals(s) && !st.contains(to.get(deep))) {
  64.                     st.push(to.get(deep));
  65.                     tree(st.peek(), t);
  66.                 }
  67.             }
  68.         }
  69.         st.pop();
  70.         return;
  71.     }
  72.  
  73.     private static int instack() {
  74.         int sum = 0;
  75.         for (int c = 0; c < st.size() - 1; c++) {
  76.             String stc1 = st.get(c);
  77.             String stc2 = st.get(c + 1);
  78.             for (int s = 0; s < from.size(); s++) {
  79.                 String fromc = from.get(s);
  80.                 String toc = to.get(s);
  81.                 if (stc1.equals(fromc) && stc2.equals(toc)) {
  82.                     sum += w.get(s);
  83.                 }
  84.             }
  85.         }
  86.         return sum;
  87.     }
  88.     private static void printSt(Stack s)
  89.     {
  90.         for(int i=0;i<s.size();i++)
  91.         {
  92.             System.out.print(s.get(i) + ((s.size()-1 != i)? ">" : ""));
  93.         }
  94.     }
  95.     private static void importData() {
  96.         int path = Integer.parseInt(io.nextLine());
  97.         StringTokenizer tmp = new StringTokenizer(io.nextLine());
  98.         start = tmp.nextToken();
  99.         end = tmp.nextToken();
  100.         for (int c = 0; c < path; c++) {
  101.             String tp = io.nextLine();
  102.             if (tp.charAt(tp.indexOf("<")) == '<' && tp.charAt(tp.indexOf(">")) == '>') {
  103.                 from.add(tp.substring( 0,tp.charAt(tp.indexOf("<"))));
  104.                 to.add(tp.substring(tp.charAt(tp.indexOf(">"))));
  105.                 w.add(Integer.parseInt(tp.substring( tp.charAt(tp.indexOf("<")),tp.charAt(tp.indexOf(">"))))); // <<<<<<<<<<<<<<<<
  106.  
  107.                 from.add(String.valueOf(tp.charAt(tp.length() - 1)));
  108.                 to.add(String.valueOf(tp.charAt(0)));
  109.                 w.add(Integer.parseInt(tp.substring(3, tp.length() - 1)));
  110.  
  111.             } else if (tp.charAt(1) == '-' && tp.charAt(2) == '>') {
  112.                 from.add(String.valueOf(tp.charAt(0)));
  113.                 to.add(String.valueOf(tp.charAt(tp.length() - 1)));
  114.                 w.add(Integer.parseInt(tp.substring(3, tp.length() - 1)));
  115.             } else if (tp.charAt(1) == '<' && tp.charAt(2) == '-') {
  116.                 from.add(String.valueOf(tp.charAt(tp.length() - 1)));
  117.                 to.add(String.valueOf(tp.charAt(0)));
  118.                 w.add(Integer.parseInt(tp.substring(3, tp.length() - 1)));
  119.             }
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement