题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2413
思路:由于要求最少的时间,可以考虑二分,然后就是满足在limit时间下,如果地球战舰数目比外星战舰数目多,就连边,然后求最大匹配即可,判断匹配数目是否等于外星球数目,如果相等,说明可以占领,继续二分。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define MAXN 333
#define inf 1<<20
typedef long long ll;
vector<int>map[MAXN];
int mark[MAXN];
int ly[MAXN];
int hp[MAXN],hq[MAXN];
int ap[MAXN],aq[MAXN];
int tt[MAXN][MAXN];
int n,m; int dfs(int u)
{
for(int i=;i<map[u].size();i++){
int v=map[u][i];
if(!mark[v]){
mark[v]=true;
if(ly[v]==-||dfs(ly[v])){
ly[v]=u;
return ;
}
}
}
return ;
} bool MaxMatch(int limit)
{
for(int i=;i<=n;i++)map[i].clear();
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
ll t1=1ll+hp[i]+(ll)(limit-tt[i][j])*hq[i];
ll t2=1ll+ap[j]+(ll)limit*aq[j];
if(t1>=t2)map[i].push_back(j);
}
}
int res=;
memset(ly,-,sizeof(ly));
for(int i=;i<=n;i++){
memset(mark,false,sizeof(mark));
res+=dfs(i);
}
if(res==m)return true;
return false;
} int main()
{
// freopen("1.txt","r",stdin);
while(scanf("%d%d",&n,&m),(n+m)){
for(int i=;i<=n;i++)
scanf("%d%d",&hp[i],&hq[i]);
for(int i=;i<=m;i++)
scanf("%d%d",&ap[i],&aq[i]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&tt[i][j]);
int low=,high=inf,mid,ans=inf;
while(low<=high){
mid=(low+high)>>;
if(MaxMatch(mid)){
ans=mid;
high=mid-;
}else
low=mid+;
}
if(ans<inf){
printf("%d\n",ans);
}else
puts("IMPOSSIBLE");
}
return ;
}