HDU 3315 My Brute(考虑优先次序的费用流)@


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!


      这里用费用流再做一遍,首先我们求出任意Si与Xj决斗时,你能获得的分值Wij. 下面网络流建图:

       源点s编号0, S1到Sn编号1到n, X1到Xn编号n+1到2*n, 汇点t编号2*n+1.

       源点s到任意Si点有边 (s, i, 1, 0)

       任意Xi点到汇点t有边 (i+n, t, 1, 0)

       如果Si与Xj决斗的解过为Wij分值,那么有下面两种情况:

       i==j时, 有边(i ,j+n, 1, -Wij*(n+1)-1) (注意这里Wij取负数且乘以(n+1)且减一,取负数,是因为最终结果取反是你能获得的最大分数.减一是使得该原始决斗顺序能够得以保留.乘以(n+1)是因为把权值扩大n+1倍之后再+1最终的权值就算是+n然后除以(n+1)还是能得到正真的分数值 )

       i!=j时,有边(i,j+n,1,-Wij*(n+1) )

       最终我们求最小费用的负数X即可. X%(n+1)就是我们保持原先决斗顺序的个数,X/(n+1)就是我们能获得的最终分数.,这题要注意最大值的给定,1<<30, 小了会超时


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 1e6+10;
const int M = 1000;
int head[M], cnt;
struct node
{
    int from, to, cap, cost, next;
}p[N];

void add(int u,int v,int w,int z)
{
    p[cnt].from=u, p[cnt].to=v, p[cnt].cap=w, p[cnt].cost=z, p[cnt].next=head[u], head[u]=cnt++;
    p[cnt].from=v, p[cnt].to=u, p[cnt].cap=0, p[cnt].cost=-z, p[cnt].next=head[v], head[v]=cnt++;
    return ;
}
int d[M], pre[M], vis[M];
const int inf = 1<<30;
int min_cost_flow(int s,int t,int f)
{
    int res=0;
    while(f>0)
    {
        queue<int>q;
        q.push(s);
        for(int i=0;i<=t;i++) d[i]=inf;
        memset(pre,-1,sizeof(pre));
        memset(vis,0,sizeof(vis));
        vis[s]=1, d[s]=0;
        while(!q.empty())
        {
            int u=q.front();q.pop();
            for(int i=head[u];i!=-1;i=p[i].next)
            {
                int v=p[i].to;
                if(d[v]>d[u]+p[i].cost&&p[i].cap>0)
                {
                    d[v]=d[u]+p[i].cost;
                    pre[v]=i;
                    if(!vis[v])
                    {
                        q.push(v);
                        vis[v]=1;
                    }
                }
            }
            vis[u]=0;
        }
        if(d[t]==inf) return 0;
        int flow=f;
        for(int i=pre[t];i!=-1;i=pre[p[i].from])
        {
            flow=min(flow,p[i].cap);
        }
        res+=flow*d[t];
        for(int i=pre[t];i!=-1;i=pre[p[i].from])
        {
            p[i].cap-=flow, p[1^i].cap+=flow;
        }
        f-=flow;
    }
    return res;
}

int v[N], h[N], pi[N], a[N], b[N];
int get(int x,int y)
{
    int s1=h[x], s2=pi[y];
    while(1)
    {
        s2-=a[x];
        if(s2<=0) return v[x];
        s1-=b[y];
        if(s1<=0) return -v[x];
    }
}

int main()
{
    printf("%d\n",0x3f3f3f3f);
    int n;
    while(scanf("%d", &n)!=EOF&&n!=0)
    {
        memset(head,-1,sizeof(head));
        cnt=0;
        for(int i=1;i<=n;i++) scanf("%d", &v[i]);
        for(int i=1;i<=n;i++) scanf("%d", &h[i]);
        for(int i=1;i<=n;i++) scanf("%d", &pi[i]);
        for(int i=1;i<=n;i++) scanf("%d", &a[i]);
        for(int i=1;i<=n;i++) scanf("%d", &b[i]);
        int s=0, t=2*n+2;
        for(int i=1;i<=n;i++)
        {
            add(s,i,1,0);
            add(n+i,t,1,0);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int s=get(i,j);
                if(i==j) add(i,n+j,1,-s*(n+1)-1);
                else add(i,n+j,1,-s*(n+1));
            }
        }
        int x=-min_cost_flow(s,t,n);
        int cnt1=(x%(n+1));
        x/=(n+1);
        double px=(1.0*cnt1)/(n);
        if(x>0) printf("%d %.3lf%%\n",x,100*px);
        else printf("Oh, I lose my dear seaco!\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值