I - Magic Potion ( 网络最大流 )

I - Magic Potion ( 网络最大流 )

题意:n个人, m个怪兽 ,k个buff。 接下来n行代表第i个人可以杀的怪兽编号, 每个人只能杀一只怪兽,但是如果有buff就可以杀两只怪兽,但是每个人只能用一次buff。 问最多能杀多少只怪兽。

思路:网络流, s源点连接n个人流量为1(代表每个人只能杀一只怪兽),每只怪兽连接汇点t流量为1( 代表每只怪兽只能被杀一次 ), 人和其能杀死的怪兽连接流量为1 , 跑一遍denic,得到不用buff的答案。 我们再假设这里的每个人都拿到了一个buff,所以s到相邻点的流量都变成1, 再跑一遍dinic, 这样如果跑出来的二次答案大于buff的数量那么所有buff都能贡献一个答案, 如果小于, 那么答案就是一次答案加二次答案。

代码:

#include <iostream>  // 网络最大流
#include <queue>
#include <stdio.h>
#include <cstring>
#include <map>
#define inf 0x3f3f3f3f

using namespace std;

struct node {
    int to,w,nxt;
};
const int maxn = 2e5+10;
node e[maxn];
int n,m,s,t,k;
int head[maxn];
int dep[maxn];
int cnt;

void addage( int u, int v, int w )
{
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

int bfs(int node) // bfs分层
{
    memset(dep,0,sizeof(dep)); // dep 层的深度
    dep[node] = 1;
    queue <int> Q;
    Q.push(node);
    while ( !Q.empty() ) {
        int x = Q.front();Q.pop();
        for ( int i=head[x]; i!=-1; i=e[i].nxt ) {
            int y = e[i].to,w = e[i].w;
            if ( !dep[y] && w ) {
                dep[y] = dep[x] + 1; // 编号++
                Q.push(y);
            }
        }
    }
    return dep[t]; // return dep[t] , 如果是0那么说明没有增广路了,退出while
}

int dfs( int x, int flow ) // dfs找增广路
{
    if ( x==t ) {
        return flow;
    }
    int sum = 0;
    for ( int i=head[x]; i!=-1; i=e[i].nxt ) {
        int y = e[i].to, w = e[i].w;
        if ( w && dep[y]==dep[x]+1 ) {
            int t = dfs(y,min(flow,w));// 注意dfs里面的是min(flow,w)
            flow -= t;      // 当前的flow减t
            sum+=t;         // sum加t,就是上面减去的t。
            e[i].w -= t;    // 正向边减t
            e[i^1].w += t;  // 反向边加t
        }
    }
    if ( sum==0 ) { // 如果sum==0,那么这个点之前没有增广路,深度清零
        dep[x] = 0;
    }
    return sum;
}

int main()
{
    int i,j,x;
    memset(head,-1,sizeof(head));
    cnt=0;
    // 人 1-500, 怪兽500+i,
    s = 0; t = 1010;
    scanf("%d %d %d",&n,&m,&k);
    for ( i=1; i<=n; i++ ) {
        addage(s,i,1);
        addage(i,s,0);
        int tt;
        scanf("%d",&tt);
        for ( j=1; j<=tt; j++ ) {
            scanf("%d",&x);
            addage(i,500+x,1);
            addage(500+x,i,0);
        }
    }
    for ( i=1; i<=m; i++ ) {
        addage( 500+i,t,1 );
        addage( t,500+t,0 );
    }

    int ans = 0;
    while ( bfs(s) ) { // 如果还存在增广路继续
        ans += dfs(s,0x3f3f3f3f); // 加一条增广路的值
    }

    for ( i=head[s]; i!=-1; i=e[i].nxt ) {
        e[i].w = 1;
    }
    int tot = 0;
    while ( bfs(s) ) { // 如果还存在增广路继续
        tot += dfs(s,0x3f3f3f3f); // 加一条增广路的值
    }
    if ( tot>=k ) {
        ans += k;
    }
    else {
        ans += tot;
    }

    printf("%d\n",ans);

    return 0;
}

/*
5 10 2
2 3 10
5 1 3 4 6 10
5 3 4 6 8 9
3 1 9 10
5 1 3 6 7 10
*/

 

好的,以下是制作背包的基本思路: 1. 创建一个背包界面,包含物品的图标、名称、数量等信息。 2. 创建一个物品类,用来存储每个物品的信息,包括编号、名称、图标、数量等信息。 3. 在游戏启动时读取text文件中的物品信息,将每个物品的信息存储到物品类中,并将物品类存储到一个数组中。 4. 点击背包界面中的物品图标时,根据物品编号在物品数组中查找对应的物品信息,并在背包界面中显示该物品的详细信息。 5. 可以在背包界面中拖拽物品图标进行交换位置,或者丢弃物品。 下面是一个示例代码,可以参考一下: ```csharp using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; // 物品类 public class Item { public int id; // 物品编号 public string name; // 物品名称 public Sprite icon; // 物品图标 public int count; // 物品数量 public int price; // 物品价格 public int hp; // 恢复生命值 public int mp; // 恢复魔法值 // 从text文件中读取物品信息 public static Item Load(string line) { string[] data = line.Split(','); Item item = new Item(); item.id = int.Parse(data[0]); item.name = data[1]; item.icon = Resources.Load<Sprite>(data[2]); item.count = int.Parse(data[3]); item.price = int.Parse(data[4]); item.hp = int.Parse(data[5]); item.mp = int.Parse(data[6]); return item; } } // 背包界面 public class Backpack : MonoBehaviour { public Image itemIcon; // 物品图标 public Text itemName; // 物品名称 public Text itemCount; // 物品数量 public Text itemPrice; // 物品价格 public Text itemHp; // 恢复生命值 public Text itemMp; // 恢复魔法值 public Transform content; // 背包物品容器 private List<Item> items = new List<Item>(); // 物品列表 // 游戏启动时加载物品信息 private void Start() { TextAsset asset = Resources.Load<TextAsset>("items"); string[] lines = asset.text.Split('\n'); foreach (string line in lines) { Item item = Item.Load(line); items.Add(item); } } // 点击物品图标时显示该物品的详细信息 public void OnItemClick(int id) { Item item = items.Find(x => x.id == id); itemIcon.sprite = item.icon; itemName.text = item.name; itemCount.text = "数量:" + item.count; itemPrice.text = "价格:" + item.price; itemHp.text = "恢复生命值:" + item.hp; itemMp.text = "恢复魔法值:" + item.mp; } // 向背包中添加物品 public void AddItem(int id, int count) { Item item = items.Find(x => x.id == id); if (item != null) { item.count += count; UpdateItem(item); } } // 从背包中移除物品 public void RemoveItem(int id, int count) { Item item = items.Find(x => x.id == id); if (item != null) { item.count -= count; UpdateItem(item); } } // 更新物品信息 private void UpdateItem(Item item) { foreach (Transform child in content) { ItemSlot slot = child.GetComponent<ItemSlot>(); if (slot.itemId == item.id) { slot.UpdateItem(item); break; } } } } // 背包中的物品槽 public class ItemSlot : MonoBehaviour { public Image itemIcon; // 物品图标 public Text itemCount; // 物品数量 public int itemId; // 物品编号 public int itemAmount; // 物品数量 // 更新物品信息 public void UpdateItem(Item item) { itemId = item.id; itemAmount = item.count; itemIcon.sprite = item.icon; itemCount.text = item.count.ToString(); } } ``` 使用方法: 1. 创建一个UI界面,添加上述代码中的Backpack组件。 2. 创建一个TextAsset资源,命名为items,并将物品信息按照示例格式填入。 3. 在UI界面中添加一个ItemSlot预制体,用来显示每个物品的图标和数量。 4. 在UI界面中添加一个拖拽组件,用来实现物品的拖拽和交换。 5. 在代码中调用Backpack组件的AddItem和RemoveItem方法,可以向背包中添加或移除物品。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值