Description Back to year 3024, humans finally developed a new technology that enables them to conquer the alien races. The new technology made it possible to produce huge spaceships known as Saber Tooth spaceships as powerful as the aliens' defending mammoths. At that time, humans ruled several planets while some others were under control of the aliens. Using Saber Tooth ships, humans finally defeated aliens and this became the first Planet War in history. Our goal is to run a simulation of the ancient war to verify some historical hypotheses. Producing each spaceship takes an amount of time which is constant for each planet but may vary among different planets. We call the number of spaceships each planet can produce in a year, the production rate of that planet. Note that each planet has a number of spaceships in it initially (before the simulation starts). The planets start producing ships when the simulation starts, so if a planet has n ships initially, and has the production rate p, it will have n + p ships at the beginning of year 1, and n + i × p ships at the beginning of year i (years are started from zero). Bradley Bennett, the commander in chief of the human armies, decided a strategy for the war. For each alien planet A, he chooses a corresponding human planet P, and produces spaceships in P until a certain moment at which he sends all spaceships in P to invade the planet A. No alien planet is invaded by two human planets and no human planet sends its spaceships to two different alien planets. The defense power of the alien planets comes from their powerful mammoths. Each alien planet contains a number of mammoths initially and produces a number of mammoths each year (called the production rate of the planet). When a fight between spaceships and mammoths takes place, the side having the greater number of troops is the winner. If the spaceships win, the alien planet is defeated. In case the number of mammoths and spaceships are equal, the spaceships win. The difficulty with planning this strategy is that it takes some time for the spaceships to reach the alien planets, and during this time, the aliens produce mammoths. The time required for spaceships to travel from each human planet to each alien planet is known. The ships can leave their planets only at the beginning of years (right after the ships are produced) and reach the alien planets at the beginning of years too (right after the mammoths are produced). As an example, consider a human planet with two initial spaceships and production rate three invading an alien planet with two initial mammoths and production rate two. The time required to travel between the two planets is two years and the ships are ordered to leave at year one. In this case, five ships leave the human planet. When they reach the alien planet, they confront eight mammoths and will be defeated during the fight. Bennett decided to prepare a plan that destroys every alien planet in the shortest possible time. Your task is to write a program to generate such a plan. The output is the shortest possible time (in years) in which every alien planet is defeated. Input There are multiple test cases in the input. The first line of each test case contains two numbers H and A which are the number of planets under the control of humans and aliens respectively (both between 1 and 250). The second line of the test case contains H non-negative integers n1 m1 n2 m2 … nH mH. The number ni is the initial number of Saber Tooth spaceships in the ith human planet and mi is the production rate of that planet. The third line contains A non-negative integers which specify the initial number of mammoths and the production rate of the alien planets in the same format as the second line. After the third line, there are H lines each containingA positive integers. The jth number on the ith line shows how many years it takes a spaceship to travel from the ith human planet to the jth alien planet. The last line of the input contains two zero numbers. Every number in the input except H and A is between 0 and 40000. Output The output for each test case contains a single integer which is the minimum time in which all alien planets can be defeated. If it is impossible to destroy all alien planets, the output should be IMPOSSIBLE. Sample Input 2 1 2 3 0 3 2 2 2 2 0 0 Sample Output 6 Source |
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const __int64 N=260;
struct node{
__int64 a,b,len;
}str[N*N]; //记录人类星球和外星球的联系;
__int64 vis[N],link[N];
struct point{
__int64 a,b;
}p1[N],p2[N];
__int64 n,m,len,map[N][N],mk[N];
bool cmp(node aa,node bb) //按从小到大排序
{
return aa.len<bb.len;
}
__int64 solve()
{
len=0;
int avis[N]; //记录是否每个外星球都有人类星期到达
memset(avis,0,sizeof(avis));
__int64 cnt=0;
for(int i=1;i<=n;i++) //赋值
for(int j=1;j<=m;j++)
{
__int64 k;
scanf("%I64d",&k);
if(n<m)
continue;
if(p2[j].a+k*p2[j].b-p1[i].a<=0)
{
str[len].a=i;
str[len].b=j;
str[len++].len=k;
if(!avis[str[len-1].b])
{
cnt++;
avis[str[len-1].b]=1;
}
continue;
}
if(p2[j].b>=p1[i].b)
continue;
str[len].a=i;
str[len].b=j;
if((p2[j].a+k*p2[j].b-p1[i].b)%(p1[i].b-p2[j].b)!=0)
{
str[len++].len=(p2[j].a+k*p2[j].b-p1[i].a)/(p1[i].b-p2[j].b)+1+k;
}
else
{
str[len++].len=(p2[j].a+k*p2[j].b-p1[i].a)/(p1[i].b-p2[j].b)+k;
}
if(!avis[str[len-1].b])
{
cnt++;
avis[str[len-1].b]=1;
}
}
if(cnt<m) //有一个或多个外星球没有人类星球到达
return 0;
else
return 1;
}
__int64 find(__int64 u) //二分匹配
{
for(__int64 v=1;v<=mk[u];v++)
{
if(!vis[map[u][v]])
{
vis[map[u][v]]=1;
if(!link[map[u][v]]||find(link[map[u][v]]))
{
link[map[u][v]]=u;
return 1;
}
}
}
return 0;
}
int main()
{
while(scanf("%I64d%I64d",&n,&m),n||m)
{
for(int i=1;i<=n;i++)
scanf("%I64d%I64d",&p1[i].a,&p1[i].b);
for(int i=1;i<=m;i++)
scanf("%I64d%I64d",&p2[i].a,&p2[i].b);
__int64 flag=solve();
if(n<m)
{
printf("IMPOSSIBLE\n");
continue;
}
if(!flag)
{
printf("IMPOSSIBLE\n");
continue;
}
memset(map,0,sizeof(map));
memset(link,0,sizeof(link));
memset(mk,0,sizeof(mk));
sort(str,str+len,cmp);
for(int i=0;i<len;i++) //建图,mk用来记录每个外星球所连接的人类星球
{
map[str[i].b][++mk[str[i].b]]=str[i].a;
}
__int64 vag=0;
for(int i=1;i<=m;i++)
{
memset(vis,0,sizeof(vis));
if(!find(i))
{
vag=1;
break;
}
}
if(vag)
{
printf("IMPOSSIBLE\n");
continue;
}
__int64 tt;
for(int i=len-1;i>=0;i--) //从最大的开始删边
{
mk[str[i].b]--;
if(link[map[str[i].b][mk[str[i].b]+1]]!=str[i].b) continue; //如果不是匹配边,continue;
link[map[str[i].b][mk[str[i].b]+1]]=0; //如果是匹配的,记得删边
memset(vis,0,sizeof(vis));
__int64 s=find(str[i].b);
if(s==0) //如果找不到匹配边,就是我们所求的最小年;
{
tt=str[i].len;
break;
}
}
printf("%I64d\n",tt);
}
}