-
Notifications
You must be signed in to change notification settings - Fork 0
/
katr-search.cpp
808 lines (727 loc) · 19.2 KB
/
katr-search.cpp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep2(i,a,b) for(int i=a;i>=b;i--)
#define sc(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pb push_back
#define ll long long
const double alpha=0.8;
const int N=3e5+5;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct Poi{
ll SD;
string key;
int keyid=-1;
int id;
ll r;
double x,y;
}_Poi[N];
vector<pair<int,int> >E[N];
bool operator <(const Poi &a,const Poi &b){
return a.SD>b.SD;
}
void printroad(Poi a,Poi b);
struct query{
unordered_map<string,int>t_id;
vector<string>t;
int k;
Poi vq;
int m;//t.size()
query(){
}
query(vector<string>t,Poi vq,int k){
m=t.size();
this->k=k;
this->vq=vq;
this->t.assign(t.begin(),t.end());
rep(i,0,t.size()-1){
this->t_id[t[i]]=i;
//编号从0开始
}
}
};
struct CP_set{
vector<Poi>choose_poi;
ll rm;
CP_set(){
}
CP_set(vector<Poi>choose_poi){
this->choose_poi.assign(choose_poi.begin(),choose_poi.end());
rm=0;
rep(i,0,this->choose_poi.size()-1){
rm += this->choose_poi[i].r;
}
}
}w;
double get_edis(Poi a,Poi b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Poi vq;int tot=0;
struct CP_route{
vector<Poi>choose_poi;
double edis;
ll D;
double SC;
ll rm;
CP_route(){
}
CP_route(vector<Poi>choose_poi){
this->choose_poi.assign(choose_poi.begin(),choose_poi.end());
rm=0;
D=0;
rm+=this->choose_poi[0].r;
rep(i,1,this->choose_poi.size()-1){
edis += get_edis(choose_poi[i-1],choose_poi[i]);
rm+=this->choose_poi[i].r;
}
if(vq.id!=choose_poi[0].id){
edis+=get_edis(vq,choose_poi[0]);
}
}
update(vector<Poi>choose_poi){
this->choose_poi.assign(choose_poi.begin(),choose_poi.end());
edis=0;
if(vq.id!=choose_poi[0].id){
edis+=get_edis(vq,choose_poi[0]);
}rep(i,1,this->choose_poi.size()-1){
edis += get_edis(choose_poi[i-1],choose_poi[i]);
// rm+=this->choose_poi[i].r;
}
}
void write(){
puts("关键词访问顺序:");
rep(i,0,this->choose_poi.size()-1){
printf("%d %c",this->choose_poi[i].id,i==this->choose_poi.size()-1?'\n':',');
}
puts("道路总距离:");
printf("%lld\n",D/1000);
puts("道路总喜爱程度:");
printf("%lld\n",rm/1000);
puts("道路总评分:");
cout<<SC/1000<<endl;
puts("具体路径:");
}
void write_road(){
if(choose_poi[0].id!=vq.id)printroad(vq,choose_poi[0]);
rep(i,1,choose_poi.size()-1)printroad(choose_poi[i-1],choose_poi[i]);
printf("%d\n",choose_poi[choose_poi.size()-1].id);
}
};
bool operator < (const CP_route &a,const CP_route &b){
return a.edis > b.edis;
}
int m;
vector<Poi>choose_poi;vector<Poi>P[25];vector<CP_set>CP_sets;int Isiz=0,k;
vector<CP_route>Base_CP_route;
void get_k_CP_set(int d){
if(Isiz>=k)return;
if(d==m){
Isiz++;
CP_sets.pb(CP_set(choose_poi));
return;
}
rep(i,0,P[d].size()-1){
choose_poi.pb(P[d][i]);
get_k_CP_set(d+1);
choose_poi.pop_back();
}
};
int vis2[25];vector<CP_route>Now_route;vector<Poi>Now_poi;
void get_cp_route(CP_set CPSi,int d){
if(d==CPSi.choose_poi.size()){
CP_route nowr = CP_route(Now_poi);
// nowr.write();
Now_route.pb(nowr);//如果fg=0,说明还不存在基准路线用来做筛选
return;
}
rep(i,0,CPSi.choose_poi.size()-1){
if(vis2[i])continue;
Now_poi.pb(CPSi.choose_poi[i]);
vis2[i]=1;
get_cp_route(CPSi,d+1);
vis2[i]=0;
Now_poi.pop_back();
}
}
map<pair<Poi,Poi>, ll>dist;
map<CP_route,ll>ms;
ll ddis[N];
int bel[N],Vis[N],p=0;
struct cmp1{
bool operator ()(Poi &a,Poi &b)const{
return ddis[a.id] > ddis[b.id];
}
};
void init(){
memset(Vis,0,sizeof(Vis));
memset(ddis,0x3f,sizeof(ddis));
p=1;
}
ll dijkstra(Poi s,Poi t){//return 0;
priority_queue<Poi,vector<Poi>,cmp1>Q;
Q.push(s);if(p==0)init();ddis[s.id]=0;
vector<int>cg;cg.pb(s.id);
while(!Q.empty()){
Poi u=Q.top();
Q.pop();
if(Vis[u.id])continue;
Vis[u.id]=1;
if(u.id==t.id){ll ans=ddis[u.id];rep(i,0,cg.size()-1)ddis[cg[i]]=1e18,Vis[cg[i]]=0;return ans;}
rep(i,0,E[u.id].size()-1)
{
Poi v=_Poi[E[u.id][i].first];
if(ddis[v.id]>ddis[u.id]+E[u.id][i].second){
cg.pb(v.id);
ddis[v.id]=ddis[u.id]+E[u.id][i].second;
Q.push(v);
}
}
}
}
void initdist(){//同一个子图内的特殊POI做dijkstra求出到彼此的距离
}
int tot1=0;
struct pair_hash {
template <class T1, class T2>
std::size_t operator() (const std::pair<T1, T2>& p) const {
auto hash1 = std::hash<T1>{}(p.first);
auto hash2 = std::hash<T2>{}(p.second);
return hash1 ^ (hash2 << 1); // 将两个哈希值组合
}
};
unordered_map<pair<int,int>,ll,pair_hash>distt;double total_time = 0.0;
ll get_dist(Poi u,Poi v){
// 在函数内部统计单次运行时间
if(u.id>v.id)swap(u,v);
if(!distt.count({u.id,v.id}))distt[{u.id,v.id}]=dijkstra(u,v);
/*if(bel[u.id]==bel[v.id]){
return dist[{u,v}];
}*/
return distt[{u.id,v.id}];
}
ll get_Routedist(CP_route r){
ll D=0;
rep(i,1,r.choose_poi.size()-1){
D += get_dist(r.choose_poi[i-1],r.choose_poi[i]);
}
if(r.choose_poi[0].id!=vq.id)D+=get_dist(vq,r.choose_poi[0]);
return D;
}
CP_route get_best_CP_route(CP_set CPSi){
priority_queue<CP_route>Qr;
Now_route.clear();
get_cp_route(CPSi,0);
rep(i,0,Now_route.size()-1){
Qr.push(Now_route[i]);
}
double GDs=1000000000;CP_route ans;
while(!Qr.empty()){
CP_route ru=Qr.top();
Qr.pop();
if(GDs<ru.edis){
break;
}
ll Gj = get_Routedist(ru);
ms[ru] = Gj;
if(Gj<GDs){
GDs=Gj;ans=ru;
}
}
ans.D=GDs;
ans.SC=-alpha*GDs+(1-alpha)*ans.rm;
//cout<<endl<<ans.D<<"?"<<" "<<" "<<ans.rm<<" "<<ans.SC<<endl;
return ans;
}
double SCmin;
void get_k_CP_route(){
SCmin=1e9;
rep(i,0,k-1){
CP_route nowr=get_best_CP_route(CP_sets[i]);
// cout<<i<<"!@!@!"<<" "<<CP_sets[i].choose_poi[1].id<<"!!"<<endl;
Base_CP_route.pb(nowr);
//nowr.write_road();
//nowr.write();
SCmin=min(SCmin,nowr.SC);
}
}
double Dub;
struct cmp2{
bool operator ()(const int &a,const int &b)const{
if(_Poi[a].r!=_Poi[b].r)return _Poi[a].r>_Poi[b].r;
else return a!=b;
}
};
ll ru;
set<int,cmp2>now_Poi[25];
ll dis2[N];int Vis2[N];
struct cmp3{
bool operator ()(const Poi &a,const Poi &b)const{
return dis2[a.id]>dis2[b.id];
}
};
//对每个关键词找一个初始的点
int total;
//大头!!!需要优化,占40%,200+ms
ll dijkstra2(Poi s){
priority_queue<Poi,vector<Poi>,cmp3>Q;
Q.push(s);memset(dis2,0x3f,sizeof(dis2));memset(Vis2,0,sizeof(Vis2));dis2[s.id]=0;
//cout<<total<<"!!"<<endl;
while(!Q.empty()){
Poi u=Q.top();
Q.pop();
if(Vis2[u.id])continue;
// cout<<dis2[3]<<" "<<dijkstra(_Poi[1],_Poi[3])<<endl;
Vis2[u.id]=1;
// if(u.id==1)cout<<"?!"<<endl;
if(u.keyid!=-1)
{
// if(u.id==1)cout<<"?!";
total--;
}
if(total==0)
{
break;//建立完所有的关键点了
}
rep(i,0,E[u.id].size()-1)
{
Poi v=_Poi[E[u.id][i].first];
if(dis2[v.id]>dis2[u.id]+E[u.id][i].second){
dis2[v.id]=dis2[u.id]+E[u.id][i].second;
Q.push(v);
}
}
}
// cout<<total<<"??"<<endl;
}
const double eps=1e-5;
int del[N];
const int SGN=2005;
vector<int>SG[SGN];
ll rmaxnow[25];
int SG_num;
query nowQ;
int delSG[SGN];
double lstSCmin;
void Safe_Region_establishment(){
int notchange=0;
double lstDub=-1;lstSCmin=SCmin;
//cout<<vq.id<<"??"<<dijkstra(_Poi[1],_Poi[3])<<endl;
if(!Vis2[vq.id])dijkstra2(vq);
while(!notchange)
{
ru=0;
rep(i,0,m-1){
auto Pnow=now_Poi[i].begin();
ru=ru+_Poi[(*Pnow)].r;
}
// cout<<ru<<"!!"<<endl;
//cout<<dis2[814]<<" "<<dis2[932]<<" "<<dis2[807]<<endl;
//cout<<Dub<<" !"<<SCmin<<" "<<ru<<endl;
//if(((1-alpha)*ru-SCmin)/alpha>0)
Dub=((1-alpha)*ru-SCmin)/alpha;
// cout<<ru<<" "<<SCmin<<"!!!!"<<Dub<<endl<<((1-alpha)*(ru+30000)-SCmin)/alpha<<"??"<<now_Poi[0].count(1107)<<"!!!!!!!!"<<endl;
//else break;
//cout<<Dub<<" "<<endl;
//cout<<Dub<<"????"<<SCmin<<endl;
if(abs(Dub-lstDub)<eps)
{
break;
}
set<Poi,cmp2>::iterator t;
rep(i,0,m-1){
while(1){
auto Pnow=now_Poi[i].begin();
//cout<<i<<" "<<now_Poi[i].size()<<" "<<Dub<<" "<<dis2[(*Pnow)]<<" "<<(*Pnow)<<" "<<SCmin<<" "<<_Poi[(*Pnow)].r<<" "<<now_Poi[0].count(1107)<<" "<<dis2[1107]<<endl;
if(dis2[(*Pnow)]<=Dub){//每个元素最多被删一次
//
// cout<<Dub<<" "<<dis2[(*Pnow)]<<"!!";
break;
}//cout<<(*Pnow)<<" "<<dis2[(*Pnow)]<<" "<<Dub<<endl;
now_Poi[i].erase(Pnow);
}
}//cout<<"?";
lstDub=Dub;
}
cout<<Dub<<"!!"<<endl;
}
void pruning_of_SG(){
ll rsub=0;
rep(i,0,m-1){
auto Pnow=now_Poi[i].begin();
rmaxnow[i]=_Poi[(*Pnow)].r;
rsub+=rmaxnow[i];
}
rep(i,0,SG_num-1)
{
ll shortest_dist=1e18;
ll rmx=0;
int fw=0;
rep(j,0,SG[i].size()-1){//复杂度约为遍历所有的点
Poi nw=_Poi[SG[i][j]];
if(!nowQ.t_id.count(nw.key))continue;
fw=1;
shortest_dist=min(shortest_dist,dis2[SG[i][j]]);
int t_id=nowQ.t_id[nw.key];//找到该点的关键词在询问结构体的下标
rmx=max(rmx,rsub+nw.r-rmaxnow[t_id]);
}
//cout<<fw<<" "<<"?"<<endl;
if((fw==0||-alpha*shortest_dist+rmx*(1-alpha)<=SCmin)){
delSG[i]=1;
}//将子图删掉
}
}
int vis3[N],dis3[N],V[N];
vector<CP_route>all_route;
CP_set nowCPSi;
vector<Poi>nPoi;
int fg;
CP_route nr;
void shuffle_route(int d,ll edis){
if(d==m){
if(!fg)nr=CP_route(nPoi);
else nr.update(nPoi);
fg=1;
if(nr.edis<((1-alpha)*nr.rm-SCmin)/alpha)
Now_route.pb(nr);
//CP_route(nPoi).write();
return;
}
if(edis>((1-alpha)*nowCPSi.rm-SCmin)/alpha)return;
rep(i,0,m-1){
if(V[i])continue;
V[i]=1;
nPoi.pb(nowCPSi.choose_poi[i]);
ll ed=0;if(d)ed=get_edis(nPoi[d-1],nowCPSi.choose_poi[i]);
shuffle_route(d+1,edis+ed);
nPoi.pop_back();V[i]=0;
}
}int tott=0;
priority_queue<double>SCC;
void get_batch_CP_route(CP_set CPSi){
priority_queue<CP_route>Qr;
nowCPSi=CPSi;ll rm=CPSi.rm;
Now_route.clear();
fg=0;//cout<<tot<<endl;
shuffle_route(0,0);//70ms!!!优先优化 已经优化
// cout<<tott<<endl;
if(!Now_route.size())return;
rep(i,0,Now_route.size()-1){
Qr.push(Now_route[i]);//50~70ms!!!优先优化 已优化
}
double GDs=1000000000;CP_route nowr;
// cout<<"!";
while(!Qr.empty()){
CP_route ru=Qr.top();
Qr.pop();
tott++;
double Dmx=((1-alpha)*rm-SCmin)/alpha;//超过Rmx的不可能成为答案
if(ru.edis>=Dmx){
break;
}
ll Gj = get_Routedist(ru);
ms[ru] = Gj;
ru.D=Gj;
ru.SC=-alpha*Gj+(1-alpha)*rm;SCC.push(-ru.SC);//变成小根堆
while(SCC.size()>k)SCC.pop();
if(SCC.size()>=k)
{
// ru.write();
// ru.write_road();
SCmin=max(SCmin,-SCC.top());//开始变大
// cout<<ru.SC<<" "<<-SCC.top()<<" "<<SCmin<<endl<<" "<<Gj<<endl<<endl;
}
//cout<<Gj<<" "<<rm<<" "<<ru.SC<<" "<<CPSi.rm<<endl;
all_route.pb(ru);
}//28ms可以优化
if(SCmin-lstSCmin>5000)//定义一个优化阙值
Safe_Region_establishment();
}
void get_CP_set(int d,int id,Poi vf){
if(d==m){
++tot;
CP_set nowset=(CP_set(choose_poi));
//rep(i,0,nowset.choose_poi.size()-1){cout<<nowset.choose_poi[i].id<<"?";}
get_batch_CP_route(nowset);
return;
}
if(id==d){
choose_poi.pb(vf);
get_CP_set(d+1,id,vf);
choose_poi.pop_back();
}
else
rep(i,0,P[d].size()-1){
choose_poi.pb(P[d][i]);
get_CP_set(d+1,id,vf);
choose_poi.pop_back();
}
}
void get_new_route(int id,Poi vf){
// 结束时间 auto end = std::chrono::high_resolution_clock::now(); // 结束时间
Now_route.clear();choose_poi.clear();
get_CP_set(0,id,vf);
}
struct cmp4{
bool operator ()(const Poi &a,const Poi &b)const{
return dis3[a.id]>dis3[b.id];
}
};
void get_ans(){
priority_queue<Poi,vector<Poi>,cmp4>Qu;
memset(dis3,0x3f,sizeof(dis3));memset(vis3,0,sizeof(vis3));
dis3[vq.id]=0;Qu.push(vq);
int e=0;rep(i,0,m-1)P[i].clear();
//cout<<del[3]<<" "<<delSG[bel[3]]<<" "<<bel[3]<<" "<<bel[4]<<endl;
while(!Qu.empty()){
Poi vf=Qu.top();
Qu.pop();
if(vis3[vf.id])continue;//被遍历过,剪枝点,剪枝安全距离
if(dis3[vf.id]>Dub)break;
//if(vf.id==1)
//cout<<vf.id<<"!!"<<" "<<del[vf.id]<<" "<<delSG[bel[vf.id]]<<" "<<nowQ.t_id.count(vf.key)<<" "<<vf.key<<endl;
vis3[vf.id]=1;//已遍历可以删
if(nowQ.t_id.count(vf.key)&&!(del[vf.id]||delSG[bel[vf.id]])){//被删的点和子图不考虑作为匹配点
//与现有关键词匹配
int id=nowQ.t_id[vf.key];//cout<<id<<"!!"<<endl;
if(!P[id].size())e++;
P[id].pb(vf);
if(e==m)get_new_route(id,vf);
}
rep(i,0,E[vf.id].size()-1){
int v = E[vf.id][i].first;
long long w = E[vf.id][i].second;
if(dis3[v] > dis3[vf.id] + w)dis3[v] = dis3[vf.id] + w;
Qu.push(_Poi[v]);
}
}
ll www=1;
//priority_queue<int>
//70ms,可以优化——可以直接用堆装,不需要完全加进来
sort(all_route.begin(),all_route.end(),[](CP_route a,CP_route b){if(a.SC!=b.SC)return a.SC>b.SC;else return a.choose_poi[0].id<b.choose_poi[0].id;});//最终找出SC较大的。
int sz=min((int)all_route.size(),max(k,5));
rep(i,0,sz-1){
all_route[i].write();
all_route[i].write_road();
}
}
void di_exploration(query Q){
nowQ=Q;
priority_queue<Poi>Qu;
ru=0;
int sz=0,findSCmin=0;Isiz=0;
m=Q.m;k=Q.k;memset(dis3,0x3f,sizeof(dis3));memset(vis3,0,sizeof(vis3));
vq=Q.vq;
Qu.push(Q.vq);
int e=0;
while(!Qu.empty()){
Poi vf=Qu.top();
Qu.pop();
if(vis3[vf.id])continue;//被删
vis3[vf.id]=1;//已遍历可以删
if(Q.t_id.count(vf.key)){//与现有关键词匹配
int id=Q.t_id[vf.key];
if(!P[id].size())
{
e++;
if(e==m){
sz=1;
for(int i=0;i<m;i++)
{
if(i!=id)sz=sz*P[i].size();
}
}
}
if(P[id].size()&&e==m)sz=sz*(P[id].size()+1)/P[id].size();
P[id].push_back(vf);
if(sz>=Q.k){
get_k_CP_set(0);
get_k_CP_route();
findSCmin=1;
pruning_of_SG();
Safe_Region_establishment();
break;
}
}
rep(i,0,E[vf.id].size()-1){
int v = E[vf.id][i].first;
long long w = E[vf.id][i].second;
if(dis3[v] > dis3[vf.id] + w)dis3[v] = dis3[vf.id] + w;
Qu.push(_Poi[v]);
}
}
//cout<<SCmin<<"!"<<endl;
}
void get_SG() {
string filename = "NY_2000.txt";
ifstream file(filename); // 打开文件
if (!file) { // 如果文件打开失败
cerr << "无法打开文件:" << filename << endl;
return; // 返回空的vector表示错误
}
int ai;
int tot=0;
// 按行读取文件
while (file >> ai) {
bel[++tot]=ai;
SG_num=max(SG_num,ai);
SG[ai].pb(tot);
}
file.close(); // 关闭文件
return;
}
void get_Coordinates() {
string filename = "USA-road-d.NY.co";
ifstream file(filename); // 打开文件
if (!file) { // 文件打开失败
cerr << "无法打开文件:" << filename << endl;
return;
}
string line;
// 跳过开头的注释部分
while (getline(file, line)) {
if (line[0] != 'v') continue; // 只处理以 'v' 开头的行
int id, x, y; // id: 节点编号, x: 经度, y: 纬度
sscanf(line.c_str(), "v %d %d %d", &id, &x, &y); // 读取格式化的节点坐标
_Poi[id].id=id;
_Poi[id].x=x;
_Poi[id].y=y;
}
file.close(); // 关闭文件
}
void get_Graph() {
string filename = "USA-road-d.NY.gr";
ifstream file(filename); // 打开文件
if (!file) { // 文件打开失败
cerr << "无法打开文件:" << filename << endl;
return;
}
string line;
// 跳过开头的注释部分
while (getline(file, line)) {
if (line[0] != 'a') continue; // 只处理以 'a' 开头的行
int u, v, w; // u: 起点, v: 终点, w: 权重
sscanf(line.c_str(), "a %d %d %d", &u, &v, &w); // 读取边的起点、终点和权重
E[u].pb({v,w});
E[v].pb({u,w});
//edges.push_back(make_tuple(u, v, w)); // 将边存储到边的列表
}
file.close(); // 关闭文件
}
void get_newGraph() {
string filename = "C:\\Users\\hp\\Desktop\\USA-road-t.NY-stripped-1000.gr";
ifstream file(filename); // 打开文件
if (!file) { // 文件打开失败
cerr << "无法打开文件:" << filename << endl;
return;
}
string line;
// 跳过开头的注释部分
while (getline(file, line)) {
if (line[0] != 'a') continue; // 只处理以 'a' 开头的行
int u, v, w; // u: 起点, v: 终点, w: 权重
sscanf(line.c_str(), "a %d %d %d", &u, &v, &w); // 读取边的起点、终点和权重
E[u].pb({v,w});
E[v].pb({u,w});
//edges.push_back(make_tuple(u, v, w)); // 将边存储到边的列表
}
file.close(); // 关闭文件
}
query que;
int lst[N];
void printroad(Poi s,Poi t){
priority_queue<Poi,vector<Poi>,cmp1>Q;
Q.push(s);if(p==0)init();ddis[s.id]=0;
vector<int>cg;cg.pb(s.id);
while(!Q.empty()){
Poi u=Q.top();
Q.pop();
if(Vis[u.id])continue;
Vis[u.id]=1;
if(u.id==t.id){int q=t.id;stack<int> st;while(q){st.push(q);q=lst[q];}while(!st.empty()){if(st.top()!=t.id)printf("%d->",st.top());st.pop();}rep(i,0,cg.size()-1)ddis[cg[i]]=1e18,Vis[cg[i]]=0,lst[cg[i]]=0;return;}
rep(i,0,E[u.id].size()-1)
{
Poi v=_Poi[E[u.id][i].first];
if(ddis[v.id]>ddis[u.id]+E[u.id][i].second){
cg.pb(v.id);
ddis[v.id]=ddis[u.id]+E[u.id][i].second;
Q.push(v);
lst[v.id]=u.id;
}
}
}
}
string ss[7]={"0","1","2","3","4","5","6"};//标签
void insert(int id)
{
if(!que.t_id.count(_Poi[id].key))return;
total++;
now_Poi[que.t_id[_Poi[id].key]].insert(id);
_Poi[id].keyid=que.t_id[_Poi[id].key];
//if(_Poi[id].id==1107)cout<<"??"<<" "<<_Poi[id].keyid<<" "<<" "<<_Poi[id].r<<endl;
} int cnt[7];
void gettype(){
string filename = "C:\\Users\\hp\\Desktop\\USA-road-t.NY-stripped-1000.poi";
ifstream file(filename); // 打开文件
if (!file.is_open()) { // 检查文件是否成功打开
cerr << "Error: Could not open the file!" << endl;
return;
}
string line;
vector<int>pp;
vector<int> thirdColumn, lastColumn;
//int totrr=0;
// 按行读取文件
int tots=0;
while (getline(file, line)) {
stringstream s(line);
vector<int> values;
int value;
// 将每行的数据读取到values向量中
while (s >> value) {
values.push_back(value);
}
// 提取第3列和最后一列数据
if (values.size() >= 4) {
// cout<<"sd";
// if(cnt[values[2]]>12)continue;
// cnt[values[2]]++;
//if(values[1]==1107)cout<<values[2]<<" "<<ss[values[2]]<<endl;
pp.pb(values[1]);//加入关键编号
_Poi[values[1]].key=ss[values[2]]; //关键词
//if(values[2]==2)totrr++;
_Poi[values[1]].r=values.back()*1000; // 积分
}
}//cout<<tots<<"!!"<<endl;
//cout<<totrr<<"!!"<<endl;
file.close(); // 关闭文件
// srand(time(0));
vector<string>t;//t.pb("2");
t.pb("1");t.pb("2");t.pb("5");t.pb("6");
//t.pb("6");
que=query(t,_Poi[810],1);
rep(i,0,pp.size()-1){
insert(pp[i]);
}
//rep(i,0,200)insert(i);//插入符
que.vq=_Poi[810];
}
// 计算这次运行的时间
int main(int argc, char** argv) {
get_SG();
get_Coordinates();
//get_Graph();
get_newGraph();
total=0;
gettype();//cout<<"Sd";
auto start = std::chrono::high_resolution_clock::now(); // 开始时间
di_exploration(que);
get_ans();
auto end = std::chrono::high_resolution_clock::now();std::chrono::duration<double, std::milli> duration = end - start; // 这里正确声明了 duration
total_time += duration.count();
// cout<<dijkstra(_Poi[810],_Poi[6415])<<" "<<dijkstra(_Poi[6415],_Poi[733])<<endl;
cout<<total_time<<" "<<tot<<" "<<tott<<" "<<dijkstra(_Poi[800],_Poi[996])<<endl;
//CP_route h= CP_route({_Poi[921] ,_Poi[932] ,_Poi[1107] ,_Poi[1103] ,_Poi[814] ,_Poi[1115]});
//h.write();
return 0;
}