HDU3315 费用流

My Brute

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


Problem Description
Seaco is a beautiful girl and likes play a game called “My Brute”. Before Valentine’s Day, starvae and xingxing ask seaco if she wants to spend the Valentine’s Day with them, but seaco only can spend it with one of them. It’s hard to choose from the two excellent boys. So there will be a competition between starvae and xingxing. The competition is like the game “My Brute”.


Now starvae have n brutes named from S1 to Sn and xingxing’s brutes are named from X1 to Xn. A competition consists of n games. At the beginning, starvae's brute Si must versus xingxing’s brute Xi. But it’s hard for starvae to win the competition, so starvae can change his brutes’ order to win more games. For the starvae’s brute Si, if it wins the game, starvae can get Vi scores, but if it loses the game, starvae will lose Vi scores. Before the competition, starvae’s score is 0. Each brute can only play one game. After n games, if starvae’s score is larger than 0, we say starvae win the competition, otherwise starvae lose it. 

It’s your time to help starvae change the brutes’ order to make starvae’s final score be the largest. If there are multiple orders, you should choose the one whose order changes the least from the original one. The original order is S1, S2, S3 … Sn-1, Sn, while the final order is up to you.

For starvae’s brute Si (maybe this brute is not the original brute Si, it is the ith brute after you ordered them) and xingxing’s brute Xi, at first Si has Hi HP and Xi has Pi HP, Si’s damage is Ai and Xi’s is Bi, in other words, if Si attacks, Xi will lose Ai HP and if Xi attacks, Si will lose Bi HP, Si attacks first, then it’s Xi’s turn, then Si… until one of them’s HP is less than 0 or equal to 0, that, it lose the game, and the other win the game.

Come on, starvae’s happiness is in your hand!
 

Input
First line is a number n. (1<=n<=90) Then follows a line with n numbers mean V1 to Vn. (0<Vi<1000) Then follows a line with n numbers mean H1 to Hn. (1<=Hi<=100)Then follows a line with n numbers mean P1 to Pn. (1<=Pi<=100) Then follows a line with n numbers mean A1 to An.(1<=Ai<=50) Then follows a line with n numbers mean B1 to Bn. (1<=Bi<=50) A zero signals the end of input and this test case is not to be processed.
 

Output
For each test case, if starvae can win the competition, print the largest score starvae can get, and then follow a real percentage means the similarity between the original order and the final order you had changed, round it to three digits after the decimal point. If starvae can’t win the competition after changing the order, please just print “Oh, I lose my dear seaco!” Maybe the sample can help you get it.
 

Sample Input
  
  
3 4 5 6 6 8 10 12 14 16 7 7 6 7 3 5 3 4 5 6 6 8 10 12 14 16 5 5 5 5 5 5 0
 

Sample Output
  
  
7 33.333% Oh, I lose my dear seaco!
 

Author
starvae
 

Source
 

Recommend
wxl
 
主要是第二个要求不知道怎么弄~~

二分图的最优匹配。图很容易建立。再处理相似度的时候。把每个权值扩大100倍。然后再对i==j时 特殊标记。使他们的权值再++1。后面选择的时候就很容易挑出。按原匹配

匹配的个数。 100*(double)(res%100)/n。即可得到第二问。


#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
#define inf 0x3f3f3f3f
#define MAXN 3000
#define MAXM 30000
using namespace std;
struct node
{
    int u,v,f,c;
};
node e[MAXM];
int first[MAXN],next[MAXM],cc;
int inq[MAXN],pre[MAXN],preedge[MAXN],d[MAXN];
inline void add_edge(int u,int v,int f,int c)
{
    e[cc].u=u;
    e[cc].v=v;
    e[cc].f=f;
    e[cc].c=c;
    next[cc]=first[u];
    first[u]=cc;
    cc++;

    e[cc].v=u;
    e[cc].u=v;
    e[cc].f=0;
    e[cc].c=-c;
    next[cc]=first[v];
    first[v]=cc;
    cc++;
}
int SPFA(int s,int t)
{
    memset(inq,0,sizeof(inq));
    int i;
    for(i=0;i<=t;i++)
        d[i]=-inf;
    memset(pre,-1,sizeof(pre));
    memset(preedge,-1,sizeof(preedge));
    d[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=0;
        int i;
        for(i=first[u];i!=-1;i=next[i])
        {
            int v=e[i].v;
            if(e[i].f)
            {
                if(d[v]<d[u]+e[i].c)
                {
                    d[v]=d[u]+e[i].c;
                    pre[v]=u;
                    preedge[v]=i;
                    if(!inq[v])
                    {
                        inq[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    if(d[t]==-inf)
        return 0;
    else
        return 1;
}
int Min_Cost_Flow(int s,int t)
{
    int ans_flow=0,ans_cost=0,mm,tmp;
    while(SPFA(s,t))
    {
        mm=inf;
        int u=t;
        while(pre[u]!=-1)
        {
            tmp=preedge[u];
            mm=min(mm,e[tmp].f);
            u=pre[u];
        }
        u=t;
        while(pre[u]!=-1)
        {
            tmp=preedge[u];
            e[tmp].f-=mm;
            e[tmp^1].f+=mm;
            u=pre[u];
        }
        ans_flow+=mm;
        ans_cost+=mm*d[t];
    }
    return ans_cost;
}
int V[100],H[100],P[100],A[100],B[100];
int win(int i,int j)
{
    int x,y;
    if(P[j]%A[i]==0)
        x=P[j]/A[i];
    else
        x=P[j]/A[i]+1;
    if(H[i]%B[j]==0)
        y=H[i]/B[j];
    else
        y=H[i]/B[j]+1;
    if(x<=y)
        return 1;
    else
        return 0;
}
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        memset(first,-1,sizeof(first));
        memset(next,-1,sizeof(next));
        cc=0;
        int i;
        for(i=1;i<=n;i++)
            scanf("%d",&V[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&H[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&P[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&A[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&B[i]);
        int s=0,t=2*n+1,j;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(win(i,j))
                {
                    if(i==j)
                        add_edge(i,n+j,1,V[i]*100+1);
                    else
                        add_edge(i,n+j,1,V[i]*100);
                }
                else
                {
                    if(i==j)
                        add_edge(i,n+j,1,-V[i]*100+1);
                    else
                        add_edge(i,n+j,1,-V[i]*100);
                }
            }
        }
        for(i=1;i<=n;i++)
        {
            add_edge(0,i,1,0);
            add_edge(n+i,t,1,0);
        }
        int res=Min_Cost_Flow(s,t);
        if(res/100<=0)
            printf("Oh, I lose my dear seaco!\n");
        else
            printf("%d %.3f%%\n",res/100,100*1.0*(res%100)/n);
    }

    return 0;
}
试了试也可以搞成二维的:

#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
#define inf 0x3f3f3f3f
#define MAXN 3000
#define MAXM 30000
using namespace std;
struct node
{
    int u,v,f,c;
    int c1;
};
node e[MAXM];
int first[MAXN],next[MAXM],cc,ss;
int inq[MAXN],pre[MAXN],preedge[MAXN],d[MAXN],d1[MAXN];
inline void add_edge(int u,int v,int f,int c,int c1)
{
    e[cc].u=u;
    e[cc].v=v;
    e[cc].f=f;
    e[cc].c=c;
    e[cc].c1=c1;
    next[cc]=first[u];
    first[u]=cc;
    cc++;

    e[cc].v=u;
    e[cc].u=v;
    e[cc].f=0;
    e[cc].c=-c;
    e[cc].c1=-c1;
    next[cc]=first[v];
    first[v]=cc;
    cc++;
}
int SPFA(int s,int t)
{
    memset(inq,0,sizeof(inq));
    int i;
    for(i=0;i<=t;i++)
        d1[i]=d[i]=-inf;
    memset(pre,-1,sizeof(pre));
    memset(preedge,-1,sizeof(preedge));
    d[s]=0;
    d1[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=0;
        int i;
        for(i=first[u];i!=-1;i=next[i])
        {
            int v=e[i].v;
            if(e[i].f)
            {
                if(d[v]<d[u]+e[i].c||(d[v]==d[u]+e[i].c&&d1[v]<d1[u]+e[i].c1))
                {
                    d[v]=d[u]+e[i].c;
                    d1[v]=d1[u]+e[i].c1;
                    pre[v]=u;
                    preedge[v]=i;
                    if(!inq[v])
                    {
                        inq[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    if(d[t]==-inf)
        return 0;
    else
        return 1;
}
int Min_Cost_Flow(int s,int t)
{
    int ans_flow=0,ans_cost=0,mm,tmp;
    ss=00;
    while(SPFA(s,t))
    {
        mm=inf;
        int u=t;
        while(pre[u]!=-1)
        {
            tmp=preedge[u];
            mm=min(mm,e[tmp].f);
            u=pre[u];
        }
        u=t;
        while(pre[u]!=-1)
        {
            tmp=preedge[u];
            e[tmp].f-=mm;
            e[tmp^1].f+=mm;
            u=pre[u];
        }
        ans_flow+=mm;
        ans_cost+=mm*d[t];
        ss+=d1[t];
    }
    return ans_cost;
}
int V[100],H[100],P[100],A[100],B[100];
int win(int i,int j)
{
    int x,y;
    if(P[j]%A[i]==0)
        x=P[j]/A[i];
    else
        x=P[j]/A[i]+1;
    if(H[i]%B[j]==0)
        y=H[i]/B[j];
    else
        y=H[i]/B[j]+1;
    if(x<=y)
        return 1;
    else
        return 0;
}
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        memset(first,-1,sizeof(first));
        memset(next,-1,sizeof(next));
        cc=0;
        int i;
        for(i=1;i<=n;i++)
            scanf("%d",&V[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&H[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&P[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&A[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&B[i]);
        int s=0,t=2*n+1,j;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(win(i,j))
                {
                    if(i==j)
                        add_edge(i,n+j,1,V[i],1);
                    else
                        add_edge(i,n+j,1,V[i],0);
                }
                else
                {
                    if(i==j)
                        add_edge(i,n+j,1,-V[i],1);
                    else
                        add_edge(i,n+j,1,-V[i],0);
                }
            }
        }
        for(i=1;i<=n;i++)
        {
            add_edge(0,i,1,0,0);
            add_edge(n+i,t,1,0,0);
        }
        int res=Min_Cost_Flow(s,t);
        if(res<=0)
            printf("Oh, I lose my dear seaco!\n");
        else
            printf("%d %.3f%%\n",res,100*1.0*(ss)/n);
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值