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