int main(int argc, char *argv[]) { Point p; int hi; while(4 == scanf("%d%d%d%d",&N,&M,&S,&V)) { for(int i = 0; i < N; ++i) edges[i].clear(); v_left = VI(N + 1, -1); v_right = VI(M + 1, -1); gophers.clear(); holes.clear(); hi = (V*S)*(V*S); for(int i = 0; i < N; ++i) { scanf("%lf%lf",&p.first,&p.second); gophers.push_back(p); } for(int i = 0; i < M; ++i) { scanf("%lf%lf",&p.first,&p.second); holes.push_back(p); } for(int i = 0; i < N; ++i) { for(int j = 0; j < M; ++j) { if(dist(gophers[i],holes[j]) < hi) edges[i].push_back(j); } } printf("%d\n",match()); } return 0; }
VP solve(vector<Line> line) { sort(line.begin(), line.end(), cmp()); int n = unique(line.begin(), line.end(), kequal) - line.begin(); assert(n > 2); int head = 0, tail = 1; que[0] = line[0]; que[1] = line[1]; VP ret; for (int i = 2; i < n; i++) { if (fabs((que[tail].e - que[tail].s).det(que[tail - 1].e - que[tail - 1].s)) < eps || fabs((que[head].e - que[head].s).det(que[head + 1].e - que[head + 1].s)) < eps) { return ret; } while (head < tail && ((isLL(que[tail], que[tail - 1])[0] - line[i].s) .det(line[i].e - line[i].s)) > eps) tail--; while (head < tail && ((isLL(que[head], que[head + 1])[0] - line[i].s) .det(line[i].e - line[i].s)) > eps) head++; que[++tail] = line[i]; } while (head < tail && ((isLL(que[tail], que[tail - 1])[0] - que[head].s) .det(que[head].e - que[head].s)) > eps) tail--; while (head < tail && ((isLL(que[head], que[head + 1])[0] - que[tail].s) .det(que[tail].e - que[tail].s)) > eps) head++; if (tail <= head + 1) return ret; for (int i = head; i < tail; i++) { ret.push_back(isLL(que[i], que[i + 1])[0]); } if (head < tail + 1) ret.push_back(isLL(que[head], que[tail])[0]); return ret; }
VP ConvexCut(const VP &ps, L l) { VP Q; for (int i = 0; i < (int)ps.size(); i++) { P A = ps[i], B = ps[(i+1)%ps.size()]; if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0) Q.push_back(is_ll((L){A, B}, l)); } return Q; }
void split_route(vector<Segment> &vs,VR &nodes,int idx, int n1, int n2){ ParamEdge e=vs[idx].edge; debugline(e.from(),e.to(),255,0,0,true); double minp=DBL_MAX; int minidx=-1; for (int j=0,n=nodes.size();j<n;j++){ if (j==n1 || j==n2) continue; if (e.cross(nodes[j])){ if (nodes[j].contains(e.from())) continue; if (nodes[j].contains(e.to())) continue; double p=e.cross_param_smallest(nodes[j]); if (p<minp){ minp=p; minidx=j; } } } if (minidx<0) return ; Point dc=e.dist_vec(nodes[minidx].center()); if (dc.is_null()){ dc=to_left(e.unit(),PI/2); // minidxust choose a side } VP pts; Point r=e.dist_vec(nodes[minidx].TL()); if (scalar(r,dc)<0) pts.push_back(nodes[minidx].TL()); r=e.dist_vec(nodes[minidx].TR()); if (scalar(r,dc)<0) pts.push_back(nodes[minidx].TR()); r=e.dist_vec(nodes[minidx].BL()); if (scalar(r,dc)<0) pts.push_back(nodes[minidx].BL()); r=e.dist_vec(nodes[minidx].BR()); if (scalar(r,dc)<0) pts.push_back(nodes[minidx].BR()); if (pts.size()==0) { printf("Ups, no points on smaller side of edge/node cut area"); return; } if (pts.size()>2 ) throw "expected 1 or 2 points"; vector<Segment> vsnew; int idxlast=idx+1; if (pts.size()==1){ vsnew.push_back(Segment(ParamEdge(e.from(),pts[0]),vs[idx].first,false)); vsnew.push_back(Segment(ParamEdge(pts[0],e.to()),false,vs[idx].last)); } else if (pts.size()==2) { if (norm(pts[0]-e.from())>norm(pts[1]-e.from())){ // do nearest point first swap(pts[0],pts[1]); } vsnew.push_back(Segment(ParamEdge(e.from(),pts[0]),vs[idx].first,false)); vsnew.push_back(Segment(ParamEdge(pts[0],pts[1]),false,false)); vsnew.push_back(Segment(ParamEdge(pts[1],e.to()),false,vs[idx].last)); idxlast++; } vs.erase(vs.begin()+idx); vs.insert(vs.begin()+idx,vsnew.begin(),vsnew.end()); split_route(vs,nodes,idxlast,minidx,n2); split_route(vs,nodes,idx,n1,minidx); // new overlaps could be introduced after makeing a kink into the line }
void cut_convex(VP& a, Point p1, Point p2) { VP b; repn(i, sz(a)) { Point a1 = a[i], a2 = a[(i + 1) % a.size()]; double m1 = mult(p1, p2, a1); double m2 = mult(p1, p2, a2); if(sgn(m1) * sgn(m2) < 0) { Point tmp; tmp.x = (a1.x * m2 - a2.x * m1) / (m2 - m1); tmp.y = (a1.y * m2 - a2.y * m1) / (m2 - m1); b.push_back(tmp); } if(sgn(m2) >= 0) b.push_back(a2); }
VP isLL(CP p1, CP p2, CP q1, CP q2){ //二直线交点,无则返回空vector //返回空时有共线与相离的区别,用(p2 - p1).det(q1 - p1) == 0判断 VP ret; T d = (q2 - q1).det(p2 - p1); if (fabs(d) < eps) return ret; ret.push_back(p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / d)); return ret; }
VP isCL(CP c, double r, CP p1, CP p2) { //返回值按到p1的距离从小到大排列 double x = (p1 - c).dot(p2 - p1); double y = (p2 - p1).abs2(); double d = x * x - y * ((p1 - c).abs2() - r * r); if (d < -eps) return VP(0); if (d < 0) d = 0; Point q1 = p1 - (p2 - p1) * (x / y); Point q2 = (p2 - p1) * (sqrt(d) / y); VP ret; ret.push_back(q1 - q2); ret.push_back(q1 + q2); return ret; }
int calcPos(VI &data,VVI &g,int node) { int ret=-1; VI wc(n , 0); VI pos(n , -1); int curNode = data[n-1]; for(int i = sz(data)-2 ; i>=0 ; i--) { if(g[curNode][data[i]]) { wc[curNode]++; } else { wc[data[i]]++; curNode = data[i]; } } for(int i = 0 ; i<n ; i++) cout<<i <<" wc "<<wc[i]<<endl; pos[curNode] = 1; VP slist; int idx = 2; for(int i = 0 ; i<n ; i++) { if(i==curNode) continue; slist.push_back({ wc[i] , i }); } sort(slist.begin() , slist.end()); for(int i = sz(slist)-1 ; i>=0 ; i--) { if(i==(sz(slist)-1) || (slist[i].x!=slist[i+1].x)) pos[slist[i].y] = idx++; else pos[slist[i].y] = pos[slist[i+1].y]; } for(int i = 0 ; i<n ; i++) { cout<<i<<" pos "<<pos[i]<<endl; } return pos[node]; }
int main() { int tcase,cas=1; cin>>tcase; while(tcase--) { cin>>n; VVI g(n , VI(n , 0)); for(auto &x: g) for(auto &y: x) cin>> y; VP slist; for(int i = 0 ; i<n ; i++) { int cnt = 0; for(int j =0 ; j<n ; j++) cnt+= g[i][j]; slist.push_back({cnt , i}); } sort(slist.begin() , slist.end()); VP sol; for(int i = 0 ; i<n ; i++) { int mx = posMax(g , slist , i); int mn = posMin(g , slist , i); cout<<i<<" "<<mx<<" "<<mn<<endl; } } return 0; }
int main(){ srand(time(NULL)); //没有这个每次的运行结果将相同 int N,T,S; double pc,pm; printf("注意: 此系统数据都是随机生成的,所以每次运行的城市坐标可能不同,结果也可能不同!!\n\n"); printf("请输入TSP问题城市数目,GA算法迭代次数,种群大小,交叉概率,变异概率\n"); while(cin>>N>>T>>S>>pc>>pm){ clock_t Time=clock(); city.clear(); printf("城市坐标如下:\n"); for(int i=0;i<N;i++){ //坐标随机生成 double a=rand()%5000, b=rand()%5000; city.push_back(Point(a,b)); printf("(%5.0lf,%5.0lf)\n",a,b); } double mi=1000000000.0; //记录最小距离和 Population p(N,S,pc,pm); //产生种群 for(int i=0;i<T;i++){ //迭代T次 p.getNext(); mi=min(mi,p.getBest().dis); //更新最小距离和 } permut=p.getBest().v; //终止状态种群的最佳个体 printf("路径为: "); for(int i=0;i<permut.size();i++){ if( i!=0 ) printf("-->"); printf("%d",permut[i]); if( i==permut.size()-1 ) puts(""); } printf("计算中出现过的最小距离和为: %.1lf 最终距离和为: %.1lf\n",mi,p.getBest().dis); printf("计算耗时: %.3lf 秒\n",(clock()-Time)/1000.0); OpenGLInit(); glutDisplayFunc(draw); glutMainLoop(); } }