#include<bits/stdc++.h>
using namespace std;
const int N1 = 509,N = N1 << 1,M1 = N * N,M = M1 * 3,INF = 0x3f3f3f3f;
int n,m,s,t;
pair<int,int> black[N],white[N];
struct edge{
int to,cap,cost,nex;
} e[M << 1];
int head[N],ecnt = 1;
void addedge(int u,int v, int c,int w){
ecnt++;
e[ecnt] = (edge){v,c,w,head[u]};
head[u] = ecnt;
}
int pre[N];
int flow[N],dis[N];
bool in_queue[N];
int SPFA(int s,int t){
for(int i = 0;i <= (n << 1) + 1;i++){
dis[i] = INF;
in_queue[i] = false;
}
flow[s] = INF;
dis[s] = 0;
queue<int> q;
q.push(s);
in_queue[N] = true;
while(!q.empty()){
int u = q.front();
q.pop();
in_queue[u] = false;
for(int i = head[u];i;i = e[i].nex){
int v = e[i].to,c = e[i].cap;
double w = e[i].cost;
if(!c)
continue;
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
flow[v] = min(flow[u],c);
pre[v] = i;
if(!in_queue[v]){
in_queue[v] = true;
q.push(v);
}
}
}
}
if(dis[t] == INF)
return 0;
return flow[t];
}
void update(int f){
int cur = t;
while(cur != s){
int i = pre[cur];
e[i].cap -= f;
e[i ^ 1].cap += f;
cur = e[i ^ 1].to;
}
}
int max_flow,max_cost;
int match[N];
double sq(double x){
return x * x;
}
double dist(pair<int,int> p1,pair<int,int> p2){
return sqrt(sq(p1.first - p2.first) + sq(p1.second - p2.second));
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n;
s = 0;t = (n << 1) + 1;
for(int i = 1;i <= n;i++)
cin >> black[i].first >> black[i].second;
for(int i = 1;i <= n;i++)
cin >> white[i].first >> white[i].second;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
addedge(i,j + n,1,dist(white[i],black[j]));
addedge(j + n,i,0,-dist(white[i],black[j]));
}
for(int i = 1;i <= n;i++){
addedge(s,i,1,0);
addedge(i,s,0,0);
}
for(int i = n + 1;i <= (n << 1);i++){
addedge(i,t,1,0);
addedge(t,i,0,0);
}
while(1){
int f = SPFA(s,t);
if(!f)
break;
max_flow += f;
max_cost += f * dis[t];
update(f);
}
for(int u = 1;u <= n;u++)
for(int i = head[u];i;i = e[i].nex)
if(e[i].cap == 0)
match[e[i].to] = u;
for(int i = n + 1;i <= (n << 1);i++)
cout << match[i] << '\n';
return 0;
}
hack 数据:
in:
2
0 0
0 1
1 1
1 0
out:
2
1