二分答案,对于当前答案Ans,求出某些人类可打败某些外星人的对应边,建图后求是否有完备匹配。
//#include <cmath> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <fstream> #include <iostream> #define rep(i, l, r) for(int i=l; i<=r; i++) #define down(i, l, r) for(int i=l; i>=r; i--) #define N 259 #define MAX 1<<30 #define ll long long using namespace std; int read() { int x=0, f=1; char ch=getchar(); while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); } while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } struct edge{int y, n;} e[N*N]; int fir[N], en; int na, nb, ma[N], mb[N], ra[N], rb[N], now, k[N], b[N], d[N][N], l, r; ll t; void Add(int x, int y) { en++, e[en].y=y, e[en].n=fir[x], fir[x]=en; } bool Find(int x) { int o=fir[x], y=e[o].y; while (o) { if (b[y]!=now) { b[y]=now; if (!k[y] || Find(k[y])) { k[y]=x; return true; } } o=e[o].n, y=e[o].y; } return false; } int main() { while(scanf("%d %d", &na, &nb) && (na || nb)) { rep(i, 1, na) ma[i]=read(), ra[i]=read(); rep(i, 1, nb) mb[i]=read(), rb[i]=read(); rep(i, 1, na) rep(j, 1, nb) d[i][j]=read(); l=0, r=MAX; while (l!=r) { t=(l+r)/2; bool can=true; rep(i, 1, nb) fir[i]=0; en=0; rep(i, 1, na) rep(j, 1, nb) { if(d[i][j] > t) continue; ll s1 = ma[i]+ra[i]*(t-d[i][j]); ll s2 = mb[j]+rb[j]*t; if(s1 >= s2) Add(j, i); } rep(i, 1, na) k[i]=b[i]=0; rep(i, 1, nb) if (!Find(now=i)) { can=false; break; } if (can) r=t; else l=t+1; } if (l==MAX) printf("IMPOSSIBLE "); else printf("%d ", l); } return 0; }