简直就是模板题啊!
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #define N 101 using namespace std; int n, m, cnt; int X1[N], Y1[N], X2[N], Y2[N], head[N], to[N * N], nex[N * N], belong[N], ans[N]; bool vis[N]; inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘; return x * f; } inline void add(int x, int y) { to[cnt] = y; nex[cnt] = head[x]; head[x] = cnt++; } inline double dis(double a, double b, double c, double d) { return sqrt((a - c) * (a - c) + (b - d) * (b - d)); } inline bool dfs(int u) { int i, v; for(i = head[u]; ~i; i = nex[i]) { v = to[i]; if(!vis[v]) { vis[v] = 1; if(!belong[v] || dfs(belong[v])) { belong[v] = u; return 1; } } } return 0; } inline int solve() { int i, ret = 0; for(i = 1; i < n; i++) { memset(vis, 0, sizeof(vis)); ret += dfs(i); } return ret; } int main() { int i, j; double x, y, z; n = read(); m = read(); memset(head, -1, sizeof(head)); for(i = 1; i <= n; i++) X1[i] = read(), Y1[i] = read(); for(i = 1; i <= m; i++) X2[i] = read(), Y2[i] = read(); for(i = 1; i < n; i++) { x = dis(X1[i], Y1[i], X1[i + 1], Y1[i + 1]); for(j = 1; j <= m; j++) { y = dis(X1[i], Y1[i], X2[j], Y2[j]); z = dis(X1[i + 1], Y1[i + 1], X2[j], Y2[j]); if(x * 2 + 1e-9 >= y + z) add(i, j); } } printf("%d\n", solve() + n); for(i = 1; i <= m; i++) ans[belong[i]] = i; for(i = 1; i <= n; i++) { printf("%d %d ", X1[i], Y1[i]); if(ans[i]) printf("%d %d ", X2[ans[i]], Y2[ans[i]]); } return 0; }