Against Mammoths
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 316 Accepted Submission(s): 90
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
nships 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.
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 containing A 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.
2 1 2 3 0 3 2 2 2 2 0 0
6
#include<stdio.h> #include<iostream> #include<string.h> #include<vector> #include<algorithm> using namespace std; typedef struct nn { __int64 s,p; }planets; typedef struct nnn { __int64 hum,alien,fightyear; }Fight; __int64 HN,AN,fn; Fight fight[260*260]; planets humans[260],aliens[260]; int set_fightyear()//计算每个人类星球所要的最少年数 { __int64 y,k,p,i,j,alie[260]={0},ak=0; fn=0; for( i=1;i<=HN;i++) scanf("%I64d%I64d",&humans[i].s,&humans[i].p); for(i=1;i<=AN;i++) scanf("%I64d%I64d",&aliens[i].s,&aliens[i].p); for( i=1;i<=HN;i++) for( j=1;j<=AN;j++) { scanf("%I64d",&y); if(HN<AN)continue;//一定不能打完所有外星球 k=aliens[j].s+y*aliens[j].p; k-=humans[i].s; fight[fn+1].hum=i; fight[fn+1].alien=j; if(k<=0) { fight[++fn].fightyear=y; if(alie[fight[fn].alien]==0)ak++,alie[fight[fn].alien]=1; } else { p=humans[i].p-aliens[j].p; if(p>0) { if(k%p==0)fight[++fn].fightyear=k/p+y; else fight[++fn].fightyear=k/p+1+y; if(alie[fight[fn].alien]==0)ak++,alie[fight[fn].alien]=1; } } } if(ak<AN)return 0;//存在外星球没有匹配的边 else return 1; } __int64 map[260][260],mk[260],match[260],vist[260],hu,al; __int64 find(int u)//二分匹配 { for(__int64 v=1;v<=mk[u];v++) if(!vist[map[u][v]]) { vist[map[u][v]]=1; if(match[map[u][v]]==0||find(match[map[u][v]])) { match[map[u][v]]=u; return 1; } } return 0; } int cmp(Fight a,Fight b)//从小到大排序 { return a.fightyear<b.fightyear; } __int64 answear() { __int64 i,j,ok=0,flag,alie; sort(fight+1,fight+1+fn,cmp); memset(mk,0,sizeof(mk)); memset(match,0,sizeof(match)); for( i=1;i<=fn;i++)//建图 map[fight[i].alien][++mk[fight[i].alien]]=fight[i].hum; for(i=1;i<=AN;i++)//找增广路 { for( j=1;j<=HN;j++)vist[j]=0; if(find(i)==0)return -1;//外星球没有匹配的边直接退出 } for( i=fn;i>0;i--) { alie=fight[i].alien; mk[alie]--;//去掉大边 if(match[map[alie][mk[alie]+1]]!=alie)continue;//不是匹配边 match[map[alie][mk[alie]+1]]=0; for( j=1;j<=HN;j++) vist[j]=0; flag=find(alie); if(flag==0)return fight[i].fightyear;//找不到增广路这条边就是答案 } } int main() { __int64 flag; while(scanf("%I64d%I64d",&HN,&AN)>0&&(HN||AN)) { flag=-1; int f=set_fightyear(); if(HN>=AN&&f)flag=answear(); if(flag==-1)printf("IMPOSSIBLE\n"); else printf("%I64d\n",flag); } }