HDU 2413 & POJ 3343 Against Mammoths【最大匹配+二分】

153 篇文章 0 订阅
55 篇文章 1 订阅

Against Mammoths

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 552    Accepted Submission(s): 182


Problem 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  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.
 

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 n i is the initial number of Saber Tooth spaceships in the ith human planet and m i 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.
 

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


原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2413


题意:星球大战,地球人拥有n个星球,外星人拥有m个星球。每个星球都有初始的飞船数量和生产飞船的能力(每年)。用邻接举阵输入每个星球之间的距离。

问你地球人最少要多少时间才能消灭完外星人。

消灭的条件:

1.地球人的飞船数量大于等于外星人的。

2.一个地球人的飞船只能攻击一个外星人的星球,一个外星人的星球只能被一个地球人统治的星球攻击。

3.当然,生产飞船只能在地球上进行。


做法:二分完成时间。


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const LL INF=0x3f3f3f3f;
const int maxn=250+5;
int n,m;
int h1[maxn],h2[maxn];
int w1[maxn],w2[maxn];
int link[maxn];
int t[maxn][maxn];
bool vis[maxn];
vector<int>G[maxn];
bool Find(int x)
{
    for(int i=0; i<G[x].size(); i++)
    {
        int v=G[x][i];
        if(!vis[v])
        {
            vis[v]=true;
            if(link[v]==-1||Find(link[v]))
            {
                link[v]=x;
                return true;
            }
        }
    }
    return false;
}
int Hungery()
{
    int ans=0;
    memset(link,-1,sizeof(link));
    for(int i=1; i<=n; i++)
    {
        memset(vis,false,sizeof(vis));
        if(Find(i))
            ans++;
    }
    return ans;
}
bool MaxMatch(int mid)
{
    for(int i=1; i<=n; i++)
        G[i].clear();
    //所有攻击情况
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            LL t1=h1[i]+(LL)(mid-t[i][j])*h2[i];
            LL t2=w1[j]+(LL)mid*w2[j];
            //攻击可以获胜
            if(t1>=t2)
                G[i].push_back(j);
        }
    }
    return Hungery()==m;
}
void solve()
{
    int left=0,right=INF;
    int mid,ans=INF;
    //对完成时间进行二分
    while(left<=right)
    {
        mid=(left+right)/2;
        if(MaxMatch(mid))
        {
            ans=mid;
            right=mid-1;
        }
        else
            left=mid+1;
    }
    if(ans==INF)
        cout<<"IMPOSSIBLE"<<endl;
    else
        cout<<ans<<endl;
}
int main()
{
    while(cin>>n>>m,n+m)
    {
        for(int i=1; i<=n; i++)
            cin>>h1[i]>>h2[i];
        for(int i=1; i<=m; i++)
            cin>>w1[i]>>w2[i];
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
                cin>>t[i][j];
        }
        solve();
    }
    return 0;
}


尊重原创,转载请注明出处:http://blog.csdn.net/hurmishine



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值