HDU 3315 My Brute 费用流

//author: CHC
//First Edit Time:	2014-10-11 18:53
//Last Edit Time:	2014-10-11 19:57
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1e+4;
const int MAXM=1e+5;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int vv[MAXN];
struct point {
    int hp,attack;
}s[MAXN],x[MAXN];
int check(int i,int j){
    int ts=s[i].hp/ x[j].attack;
    if(s[i].hp%x[j].attack)++ts;
    int tx=x[j].hp/ s[i].attack;
    if(x[j].hp%s[i].attack)++tx;
    //printf("ts:%d tx:%d\n",ts,tx);
    if(ts>=tx)return 1;
    else return 0;
}
struct Edge
{
    int from,to,ci,cost,next;
    Edge(){}
    Edge(int _from,int _to,int _ci,int _cost,int _next):from(_from),to(_to),ci(_ci),cost(_cost),next(_next){}
}e[MAXM];
int head[MAXN],tot;
int q[MAXM];
int dis[MAXN],pre[MAXN],rec[MAXN],vis[MAXN];
inline void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
inline void AddEdge1(int u,int v,int ci,int cost){
    e[tot]=Edge(u,v,ci,cost,head[u]);
    head[u]=tot++;
    e[tot]=Edge(v,u,0,-cost,head[v]);
    head[v]=tot++;
}
inline bool spfa(int S,int T,LL &cost,LL &flow){
    int i,h=0,t=0;
    for(i=0;i<=MAXN;i++){
        dis[i]=INF;
        vis[i]=false;
    }
    q[h]=S;
    dis[S]=0;
    vis[S]=true;
    while(h<=t){
        int u=q[h++];
        vis[u]=false;
        for(i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(e[i].ci>0&&dis[v]>dis[u]+e[i].cost){
                dis[v]=dis[u]+e[i].cost;
                pre[v]=u;
                rec[v]=i;
                if(!vis[v]){
                    vis[v]=1;
                    q[++t]=v;
                }
            }
        }
    }
    if(dis[T]==INF)return false;
    int minn=INF;
    for(i=T;i!=S;i=pre[i]){
        if(e[rec[i]].ci<minn)
            minn=e[rec[i]].ci;
    }
    for(i=T;i!=S;i=pre[i]){
        //cost+=minn*e[rec[i]].cost;
        e[rec[i]].ci-=minn;
        e[rec[i]^1].ci+=minn;
    }
    cost+=dis[T]*minn;
    flow+=minn;
    return true;
}
inline void mincostmaxflow(int S,int T,LL &cost,LL &flow){
    while(spfa(S,T,cost,flow));
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n){
        for(int i=1;i<=n;i++)scanf("%d",&vv[i]);
        for(int i=1;i<=n;i++)scanf("%d",&s[i].hp);
        for(int i=1;i<=n;i++)scanf("%d",&x[i].hp);
        for(int i=1;i<=n;i++)scanf("%d",&s[i].attack);
        for(int i=1;i<=n;i++)scanf("%d",&x[i].attack);
        init();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(check(i,j)){
                    AddEdge1(i,j+n,1,-vv[i]*1000-(i==j));
                    //printf("%d-->%d v:%d\n",i,j,-vv[i]*1000-(i==j));
                }
                else {
                    AddEdge1(i,j+n,1,vv[i]*1000-(i==j));
                    //printf("%d-->%d v:%d\n",i,j,vv[i]*1000-(i==j));
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            AddEdge1(0,i,1,0);
            AddEdge1(i+n,2*n+1,1,0);
        }
        LL flow=0,cost=0;
        mincostmaxflow(0,2*n+1,cost,flow);
        cost=-cost/1000;
        int cnt=0;
        for(int i=1;i<=n;i++){
            for(int j=head[i];j!=-1;j=e[j].next){
                if(((j&1)==0)&&e[j].ci==0){
                    //printf("%d-->%d\n",i,e[j].to-n);
                    if(e[j].to==i+n) cnt++;
                    break;
                }
            }
        }
        //printf("cnt:%d\n",cnt);
        if(cost<=0)puts("Oh, I lose my dear seaco!");
        else printf("%I64d %.3lf%%\n",cost,1.0*cnt/n*100);
    }
    return 0;
}

你可能感兴趣的:(费用流)