Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class PF {
- static int testcase = 0;
- static Scanner io = null;
- static ArrayList<String> from = new ArrayList<String>();
- static ArrayList<String> to = new ArrayList<String>();
- static ArrayList<Integer> w = new ArrayList<Integer>();
- static String start = "";
- static String end = "";
- static Stack<String> st = new Stack<String>();
- static Stack<String> sttmp = new Stack<String>();
- static int min = Integer.MAX_VALUE;
- public static void main(String[] args) throws FileNotFoundException {
- io = new Scanner(new File("f.in"));
- testcase = Integer.parseInt(io.nextLine());
- for (int tc = 0; tc < testcase; tc++) {
- //----------//
- System.out.println(tc+1);
- //----------//
- importData();
- tree(start, end);
- printSt(sttmp);
- System.out.println(" "+min);
- int tmpMin = min;
- String tmpc = start;
- start = end;
- end = tmpc;
- st.clear();
- min = Integer.MAX_VALUE;
- tree(start, end);
- printSt(sttmp);
- System.out.print(" "+min);
- tmpMin += min;
- System.out.print("\n"+tmpMin);
- //-----reset ------//
- from = new ArrayList<String>();
- to = new ArrayList<String>();
- w = new ArrayList<Integer>();
- start = "";
- end = "";
- st = new Stack<String>();
- min = Integer.MAX_VALUE;
- }
- }
- private static void tree(String s, String t) {
- if (st.isEmpty()) {
- st.push(s);
- }
- if (s.equals(t)) {
- int p = instack();
- if(p < min)
- {
- min = p;
- sttmp = (Stack)st.clone();
- }
- } else {
- for (int deep = 0; deep < from.size(); deep++) {
- if (from.get(deep).equals(s) && !st.contains(to.get(deep))) {
- st.push(to.get(deep));
- tree(st.peek(), t);
- }
- }
- }
- st.pop();
- return;
- }
- private static int instack() {
- int sum = 0;
- for (int c = 0; c < st.size() - 1; c++) {
- String stc1 = st.get(c);
- String stc2 = st.get(c + 1);
- for (int s = 0; s < from.size(); s++) {
- String fromc = from.get(s);
- String toc = to.get(s);
- if (stc1.equals(fromc) && stc2.equals(toc)) {
- sum += w.get(s);
- }
- }
- }
- return sum;
- }
- private static void printSt(Stack s)
- {
- for(int i=0;i<s.size();i++)
- {
- System.out.print(s.get(i) + ((s.size()-1 != i)? ">" : ""));
- }
- }
- private static void importData() {
- int path = Integer.parseInt(io.nextLine());
- StringTokenizer tmp = new StringTokenizer(io.nextLine());
- start = tmp.nextToken();
- end = tmp.nextToken();
- for (int c = 0; c < path; c++) {
- String tp = io.nextLine();
- if (tp.charAt(tp.indexOf("<")) == '<' && tp.charAt(tp.indexOf(">")) == '>') {
- from.add(tp.substring( 0,tp.charAt(tp.indexOf("<"))));
- to.add(tp.substring(tp.charAt(tp.indexOf(">"))));
- w.add(Integer.parseInt(tp.substring( tp.charAt(tp.indexOf("<")),tp.charAt(tp.indexOf(">"))))); // <<<<<<<<<<<<<<<<
- from.add(String.valueOf(tp.charAt(tp.length() - 1)));
- to.add(String.valueOf(tp.charAt(0)));
- w.add(Integer.parseInt(tp.substring(3, tp.length() - 1)));
- } else if (tp.charAt(1) == '-' && tp.charAt(2) == '>') {
- from.add(String.valueOf(tp.charAt(0)));
- to.add(String.valueOf(tp.charAt(tp.length() - 1)));
- w.add(Integer.parseInt(tp.substring(3, tp.length() - 1)));
- } else if (tp.charAt(1) == '<' && tp.charAt(2) == '-') {
- from.add(String.valueOf(tp.charAt(tp.length() - 1)));
- to.add(String.valueOf(tp.charAt(0)));
- w.add(Integer.parseInt(tp.substring(3, tp.length() - 1)));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement