Against Mammoths
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 316 accepted submission (s): 90
Problem descriptionback 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 specified ent 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,
Production RateOf 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 + pShips at the beginning of year 1, and
N + I × PShips 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. 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.
Inputthere 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 N 2 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 parameter t h and A is between 0 and 40000.
Outputthe 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 shocould be impossible.
Sample Input
2 12 3 0 32 2220 0
Sample output
6
Source2006 Asia Regional Tehran: Man and the aliens Star Wars. There are h planets in total, and aliens have a planet. Now, people need to use a ship to launch an alien ship, each human planet is required to have only one external planet, and each planet begins to have a known number of ships, whether human or alien planet, and each planet has a production-like productivity p, it can produce only p flying every year. It takes a certain period of year for humans to fly to a planet. In this period of time, the external planet will also produce and fly like a human planet. To win an alien planet, the number of spacecraft must be no less than that of the foreign planet. Ask how many years does it take for a human star to win all alien planets, and output impossible if it cannot finish all aliens.
Solution: To win, you must first meet certain conditions: 1. h> = A. 2. Each planet must have a human planet to fight. 3. A human planet can only fight against one external planet, and the number of ships on this human planet must be greater than the number of foreign ships on the external planet. Every planet is defeated in a one-on-one war. Now we can think of binary matching based on a one-to-one relationship. So how to build edge? Through preprocessing, each human planet can win each planet within a limited year and establish an edge, and write down the time used by each edge. We can analyze it again, because what we want is to use the minimum number of years to finish all the external planets, then we can first sort all the sides by years from small to large, A planet other than us can be used as a side to match the Human Planet (Benefit: When an external planet cannot find an augmented path to match the Human Planet, we find that the external planet cannot be completed ), when all the external planets can match the Human Planet, they will be removed from the biggest side by one. If the matching side is not removed, there is no need to find the augmented path, then remove a large edge. If the matching edge is removed, find an augmented path from the external planet related to the edge, if it cannot be found, it means that the removed side is the minimum number of years required to finish all the external planets. You can get the answer directly. PS: according to the above 250 ms AC.
# 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 [260*260]; Planets humans [260], aliens [260]; int set_fightyear () // calculate the minimum number of years required for each human planet {_ 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; // you cannot finish all the external planets 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; // an external planet has no matching edge else return 1; }__ int64 map [260] [260], MK [260], Match [260], vist [260], Hu, Al ;__ int64 find (int u) // binary match {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) // sort from small to large {return. 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 ++) // create a map [fight [I]. alien] [++ MK [fight [I]. alien] = figh T [I]. hum; for (I = 1; I <= An; I ++) // find the augmented path {for (j = 1; j <= HN; j ++) vist [J] = 0; If (find (I) = 0) Return-1; // The external planet directly exits without matching edges} for (I = FN; i> 0; I --) {Alie = fight [I]. alien; MK [Alie] --; // remove the large edge if (Match [map [Alie] [MK [Alie] + 1]! = Alie) continue; // not matching edge 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; // The Edge of zengguang road cannot be found. This is the answer} 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 );}}