注意二分的上界可以达到1200000。。。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> //#include <cmath> #include <time.h> #define maxn 1005 #define maxm 3000005 #define eps 1e-10 #define mod 998244353 #define INF 999999999 #define lowbit(x) (x&(-x)) #define mp mark_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R //#pragma comment (linker,"/STACK:102400000,102400000") typedef long long LL; //typedef int LL; using namespace std; LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} //void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();} // head int h[maxn], next[maxm], v[maxm]; int low[maxn], dfn[maxn], ins[maxn]; int n, m1, m2, x1, y1, x2, y2; struct point { int x, y; }po[maxn]; struct node { int k1, k2; }p1[maxn], p2[maxn]; int d[2][maxn]; int flag[maxn]; stack<int> s; int cnt, dist, dfs_clock, c; void scanf(int &x) { x = 0; int ok = 0; char ch = getchar(); while(ch == ' ' || ch == '\n') ch = getchar(); if(ch == '-') ch = getchar(), ok = 1; while(ch >= '0' && ch <= '9') x = x*10 + ch - '0', ch = getchar(); if(ok) x = -x; } void addedges(int u, int vv) { next[cnt] = h[u], h[u] = cnt, v[cnt] = vv, cnt++; } void read(void) { scanf(x1), scanf(y1), scanf(x2), scanf(y2); for(int i = 1; i <= n; i++) scanf(po[i].x), scanf(po[i].y); for(int i = 1; i <= m1; i++) scanf(p1[i].k1), scanf(p1[i].k2); for(int i = 1; i <= m2; i++) scanf(p2[i].k1), scanf(p2[i].k2); } void handle(void) { for(int i = 1; i <= n; i++) { d[0][i] = abs(po[i].x - x1) + abs(po[i].y - y1); d[1][i] = abs(po[i].x - x2) + abs(po[i].y - y2); } } void init(void) { cnt = c = dfs_clock = 0; dist = abs(x1 - x2) + abs(y1 - y2); memset(h, -1, sizeof h); memset(ins, 0, sizeof ins); memset(low, 0, sizeof low); memset(dfn, 0, sizeof dfn); memset(flag, 0, sizeof flag); } inline int abs(int a, int b) { int tmp = a - b; if(tmp > 0) return tmp; else return -tmp; } void tarjan(int u) { low[u] = dfn[u] = ++dfs_clock; s.push(u), ins[u] = 1; for(int e = h[u]; ~e; e = next[e]) if(!dfn[v[e]]) { tarjan(v[e]); low[u] = min(low[u], low[v[e]]); } else if(ins[v[e]]) low[u] = min(low[u], dfn[v[e]]); if(low[u] == dfn[u]) { int tmp = s.top(); s.pop(), ins[tmp] = 0, ++c; while(tmp != u) { flag[tmp] = c; tmp = s.top(); s.pop(), ins[tmp] = 0; } flag[tmp] = c; } } bool check(int x) { init(); int dis; for(int i = 1; i <= m1; i++) { addedges(2*p1[i].k1, 2*p1[i].k2+1); addedges(2*p1[i].k1+1, 2*p1[i].k2); addedges(2*p1[i].k2, 2*p1[i].k1+1); addedges(2*p1[i].k2+1, 2*p1[i].k1); } for(int i = 1; i <= m2; i++) { addedges(2*p2[i].k1, 2*p2[i].k2); addedges(2*p2[i].k1+1, 2*p2[i].k2+1); addedges(2*p2[i].k2, 2*p2[i].k1); addedges(2*p2[i].k2+1, 2*p2[i].k1+1); } for(int i = 1; i <= n; i++) for(int j = i+1; j <= n; j++) { dis = d[0][i] + d[0][j]; if(dis > x) addedges(2*i, 2*j+1), addedges(2*j, 2*i+1); dis = d[1][i] + d[1][j]; if(dis > x) addedges(2*i+1, 2*j), addedges(2*j+1, 2*i); dis = d[0][i] + d[1][j] + dist; if(dis > x) addedges(2*i, 2*j), addedges(2*j+1, 2*i+1); dis = d[1][i] + d[0][j] + dist; if(dis > x) addedges(2*i+1, 2*j+1), addedges(2*j, 2*i); } for(int i = 2; i <= (n<<1) + 1; i++) if(!dfn[i]) tarjan(i); for(int i = 1; i <= n; i++) if(flag[2*i] == flag[2*i+1]) return false; return true; } void work(void) { int bot = 0, top = 12000000, mid, res = -1; while(top >= bot) { mid = (top+bot)>>1; if(check(mid)) top = mid-1, res = mid; else bot = mid+1; } if(~res) printf("%d\n", res); else printf("-1\n"); } int main(void) { while(scanf("%d%d%d", &n, &m1, &m2)!=EOF) { read(); handle(); work(); } return 0; }