|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | +import java.util.*; |
| 4 | +/* |
| 5 | + * |
| 6 | + * |
| 7 | + * Improvement for cowroute |
| 8 | + */ |
| 9 | +public class cowroute { |
| 10 | + |
| 11 | + |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + StringTokenizer st; |
| 14 | + BufferedReader f= new BufferedReader(new FileReader("6.in")); |
| 15 | + st=new StringTokenizer(f.readLine()); |
| 16 | + |
| 17 | + int[] route= {0,0,0}; |
| 18 | + ArrayList<int[]> aroutes=new ArrayList<int[]>(); |
| 19 | + ArrayList<int[]> broutes=new ArrayList<int[]>(); |
| 20 | + int A=Integer.parseInt(st.nextToken()); |
| 21 | + int B=Integer.parseInt(st.nextToken()); |
| 22 | + int N=Integer.parseInt(st.nextToken()); |
| 23 | + /* |
| 24 | + * A routes and B routes item structure |
| 25 | + * First item-price |
| 26 | + * 1-Index |
| 27 | + * 2-Which route |
| 28 | + */ |
| 29 | + |
| 30 | + boolean Before; |
| 31 | + boolean after; |
| 32 | + @SuppressWarnings("unchecked") |
| 33 | + ArrayList<Integer>[] data=new ArrayList[N]; |
| 34 | + int price; |
| 35 | + int M,id; |
| 36 | + for(int i=0;i<N;i++) { |
| 37 | + data[i]=new ArrayList<Integer>(); |
| 38 | + st=new StringTokenizer(f.readLine()); |
| 39 | + price=Integer.parseInt(st.nextToken()); |
| 40 | + M=Integer.parseInt(st.nextToken()); |
| 41 | + st=new StringTokenizer(f.readLine()); |
| 42 | + for(int j=0;j<M;j++) { |
| 43 | + id=Integer.parseInt(st.nextToken()); |
| 44 | + if(id==A) { |
| 45 | + route[0]=j; |
| 46 | + route[1]=i; |
| 47 | + route[2]=price; |
| 48 | + aroutes.add(route.clone()); |
| 49 | + }else if(id==B) { |
| 50 | + route[0]=j; |
| 51 | + route[1]=i; |
| 52 | + route[2]=price; |
| 53 | + broutes.add(route.clone()); |
| 54 | + } |
| 55 | + data[i].add(id); |
| 56 | + } |
| 57 | + data[i].add(0, price); |
| 58 | + } |
| 59 | + //System.out.println("Finished first loop Debug status A:"+A+" B:"+B+" N:"+N); |
| 60 | + int low=-1; |
| 61 | + boolean k=true; |
| 62 | + int r,x; |
| 63 | + int price2=0,index,index2,count; |
| 64 | + ArrayList<Integer> a,b; |
| 65 | + a=new ArrayList<Integer>();b=new ArrayList<Integer>(); |
| 66 | + //System.out.println("====Entering main loop==="); |
| 67 | + |
| 68 | + for(int[] stuff:aroutes) { |
| 69 | + a.clear(); |
| 70 | + |
| 71 | + price=stuff[2];//Get price |
| 72 | + index=stuff[0]; |
| 73 | + r=stuff[1]; |
| 74 | + count=0; |
| 75 | + after=false; |
| 76 | + for(int i:data[r]) {//For every item in the A route |
| 77 | + |
| 78 | + if(i==A) { |
| 79 | + after=true; |
| 80 | + continue; |
| 81 | + }else if(i==B) {// If B is also in the route and we are past "A" |
| 82 | + if((price)<low||k) { |
| 83 | + k=false; |
| 84 | + //System.out.println("New low cost by all in one"); |
| 85 | + System.out.println("Debug Info index:"+index+"- count:"+count); |
| 86 | + low=price; |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + count++; |
| 91 | + |
| 92 | + if(after) {a.add(i);} |
| 93 | + } |
| 94 | + for(int[] dat:broutes) { |
| 95 | + |
| 96 | + b.clear(); |
| 97 | + |
| 98 | + price2=dat[2];//Get price |
| 99 | + index2=dat[0]; |
| 100 | + //r2=stuff[1]; |
| 101 | + Before=true; |
| 102 | + for(int m=0;m<data[r].size();m++) { |
| 103 | + x=data[r].get(m); |
| 104 | + if(x==B) { |
| 105 | + Before=false; |
| 106 | + continue; |
| 107 | + } |
| 108 | + if(Before) { |
| 109 | + |
| 110 | + |
| 111 | + b.add(x); |
| 112 | + } |
| 113 | + } |
| 114 | + a.sort(null); |
| 115 | + b.sort(null); |
| 116 | + for(int i=0;i<a.size();i++) { |
| 117 | + System.out.println("Info I:"+i); |
| 118 | + count=a.get(i); |
| 119 | + |
| 120 | + if(Arrays.binarySearch(b.toArray(),(a.get(i)))>-1) {//Flight overlap |
| 121 | + if(((price2+price)<low||low==-1)) { |
| 122 | + System.out.println("Debug for new overlap: The same value is:"+a.get(i)+" The first price is price:"+price+" and price2:"+price2); |
| 123 | + System.out.println("Debug count:"+count+" index2:"+index2); |
| 124 | + System.out.println("New low cost by overlap"); |
| 125 | + low=price2+price; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + } |
| 135 | + PrintWriter pw=new PrintWriter(new FileWriter("cowroute.out")); |
| 136 | + pw.println(low); |
| 137 | + pw.close(); |
| 138 | + f.close(); |
| 139 | + |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments