-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.java
More file actions
230 lines (211 loc) · 6.58 KB
/
BFS.java
File metadata and controls
230 lines (211 loc) · 6.58 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package traintransfer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BFS {
private boolean[] isVisited;
public int[] parent;
public int[] parTrainId;
public BFSStation bfsRes;
public void bfs(Graph g, int startV, int arrV, int maxTransCnt, int maxResNum,
String avoidTrainType,boolean isFuzzyDepSt,boolean isFuzzyArrSt) {
isVisited = new boolean[g.Nv];
parent = new int[g.Nv];
parTrainId = new int[g.Nv];
for (int i = 0; i < isVisited.length; i++)
isVisited[i] = false;
for (int i = 0; i < parent.length; i++)
parent[i] = i;
for (int i = 0; i < parTrainId.length; i++)
parTrainId[i] = -1;
MyQueue<VIdAndTransCnt> que = new MyQueue<>();
que.push(new VIdAndTransCnt(startV, 0));
if(isFuzzyDepSt) {
for(int vi:ReadStations.stationsArr[startV].sameStationsId) {
if(vi==-404)continue;
que.push(new VIdAndTransCnt(vi, 0));
isVisited[vi]=true;
}
}
isVisited[startV] = true;
// parent[startV]=-1;
int transCnt, resCnt = 0;
int currV;
int[] arrVs;
Heap<BFSStation> heap = new Heap<BFSStation>(5000, new BFSStation(-1, -1, null, -1,-1)) {
@Override
public int compare(BFSStation a, BFSStation b) {
if(a.hadTransTimes!=b.hadTransTimes)return a.hadTransTimes>b.hadTransTimes?1:-1;
if (a.weight == b.weight)
return 0;
return a.weight > b.weight ? 1 : -1;
}
};
if(isFuzzyArrSt) {
arrVs=new int[ReadStations.stationsArr[arrV].sameStationsId.length+1];
for(int i=0;i<ReadStations.stationsArr[arrV].sameStationsId.length;i++) {
arrVs[i]=ReadStations.stationsArr[arrV].sameStationsId[i];
}
arrVs[ReadStations.stationsArr[arrV].sameStationsId.length]=arrV;
}else {
arrVs=new int[1];
arrVs[0]=arrV;
}
if(isArrStation(startV, arrVs)) {
return;
}
int currTransCnt = 0;
while (!que.isEmpty()) {
currV = que.front().vId;
transCnt = que.front().transCnt;
//System.out.println(transCnt+" "+maxTransCnt+" "+resCnt+" "+maxResNum+" "+transCnt+" "+currTransCnt);
if (transCnt > maxTransCnt || (resCnt > maxResNum && transCnt > currTransCnt))
break;
currTransCnt = transCnt;
que.pop();
Trains tmpE = (Trains) g.v[currV].E;
while (tmpE != null) {
// System.out.println(que.size);
// System.out.println(tmpE.depStName+" "+tmpE.arrStName);
// if(transCnt > maxTransCnt&&resCnt>=maxResNum)break;
if (!RailNet.isTheTypeOfTrainSuitable(tmpE, avoidTrainType)) {
tmpE = (Trains) tmpE.nextEdge;
continue;
}
int v2 = tmpE.v2;
if (isArrStation(v2,arrVs)) {
// System.out.println(currTransCnt);
// System.out.println(1);
// System.out.println(currV+" "+v2);
if (!heap.insert(new BFSStation(currV, tmpE.trainCodeId, this, v2,transCnt)))
break;
resCnt++;
tmpE = (Trains) tmpE.nextEdge;
continue;
}
if (!isVisited[v2]) {
isVisited[v2] = true;
que.push(new VIdAndTransCnt(v2, transCnt + 1));
parent[v2] = currV;
parTrainId[v2] = tmpE.trainCodeId;
}
tmpE = (Trains) tmpE.nextEdge;
}
}
bfsRes=createBFSRes(heap,maxResNum);
}
private boolean isArrStation(int v,int[] Vs) {
if(v<0) {
System.out.println("except BFS class: vId <0");
return false;
}
for(int vi:Vs) {
if(vi==v)return true;
}
return false;
}
private BFSStation createBFSRes(Heap<BFSStation> heap, int resNum) {
BFSStation head=null,tail;
if (heap.isEmpty())
return null;
for (int i = 0; i < resNum; i++) {
if(heap.isEmpty())break;
if (head == null) {
head = heap.pop();
head.next = null;
} else {
head.insert(heap.pop());
}
}
for(tail=head;tail.next!=null;tail=tail.next);
tail.next=head;
BFSStation res=head.next;
head.next=null;
/*生成一个倒序链表*/
return res;
}
public static void main(String[] args) {
}
}
class VIdAndTransCnt {
int vId, transCnt;
public VIdAndTransCnt(int vId, int transCnt) {
super();
this.vId = vId;
this.transCnt = transCnt;
}
}
class BFSStation {
/* 保存距到达站最后一站的id和前往最终目的地的车次id */
int vId;
int trainCodeId;
int weight;
int arrV;
int hadTransTimes;
BFSStation next = null;
public BFSStation(int vId, int trainCodeId, BFS bfs, int arrV,int hadTransTimes) {
super();
this.hadTransTimes=hadTransTimes;
this.vId = vId;
this.trainCodeId = trainCodeId;
if (vId >= 0)
weight = getWeight(vId, trainCodeId, bfs, arrV);
else
weight = -100000000;
}
private int getWeight(int vId, int trainCodeId, BFS bfs, int arrV) {
this.arrV=arrV;
bfs.parent[arrV] = vId;
bfs.parTrainId[arrV] = trainCodeId;
if (vId == arrV) {
return 0;
}
int resWeight = 0;
String aTrainBasicDescInf;
String pattern = ".+(\\d{2}:\\d{2})~(\\d{2}:\\d{2}).*";
Pattern r = Pattern.compile(pattern);
int depTime, arrTime;
for (int i = arrV; bfs.parent[i] != i; i = bfs.parent[i]) {
// System.out.println(i);
// System.out.println(ReadStations.stationsArr[bfs.parent[i]].name);
// System.out.println(ReadStations.stationsArr[i].name);
aTrainBasicDescInf = Trains.getATrainBasicDescInf(bfs.parTrainId[i],
ReadStations.stationsArr[bfs.parent[i]].name, ReadStations.stationsArr[i].name);
aTrainBasicDescInf = aTrainBasicDescInf.replace(" ", "").replace(":", ":").replace("(", "(").replace(")",
")");
Matcher m = r.matcher(aTrainBasicDescInf);
// System.out.println(r.pattern());
// System.out.println(aTrainBasicDescInf);
// System.out.println(m.find());
if (m.find()) {
// System.out.println(m.groupCount());
// System.out.println(m.group(0));
// System.out.println(aTrainBasicDescInf);
depTime = Trains.strTimeToInt(m.group(1), 0);
int index=aTrainBasicDescInf.indexOf('+');
int day;
if(index>0) {
day=aTrainBasicDescInf.charAt(index+1)-'0';
}else {
day=0;
}
arrTime = Trains.strTimeToInt(m.group(2), day);
// System.out.println(m.group(1)+" "+m.group(2)+" "+day);
// System.out.println(depTime+" "+arrTime);
// if (m.groupCount() == 4) {
// System.out.println(m.group(1)+" "+m.group(2)+" "+m.group(3)+" "+m.group(4));
// arrTime = Trains.strTimeToInt(m.group(2), Integer.parseInt(m.group(3)));
// } else {
// System.out.println(m.group(1)+" "+m.group(2)+" "+m.group(3));
// arrTime = Trains.strTimeToInt(m.group(2), 0);
// }
resWeight += (arrTime - depTime);
}
}
// System.out.println("total: "+resWeight);
return resWeight;
}
void insert(BFSStation li) {
li.next = this.next;
this.next = li;
}
}