【牛客 - 283F】出装方案(最小费用最大流)

题干:
 

众所周知,在各种对抗类游戏里装备都是很重要的一环,不同的出装方案会给玩家带来不同的强度。

dalao手里有N件装备,现在dalao要把装备分给N个队友,每个队友只能分一件装备,而每个队友穿上不同的装备会有不同程度的强度提升。

现在给出每个队友对每件装备的强度提升的值,请问dalao的所有分配方案里,最多能让团队的强度提升多少呢?

输入描述:

 

第一行有一个整数T,表示数据的组数(不会超过150组)

每组数据第一行包含一个整数N,接下来会有N行,每行有N个整数,其中第 a 行的第 b 个数字表示第 a 个队友穿上第 b 件装备的强度提升。任何队员穿任何装备的强度提升都不会超过20000。

 

输出描述:

对于每组数据在一行内输出一个整数表示强度能够提升的最大值

示例1

输入

复制

2
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
1 3 5
2 4 6
7 9 11

输出

复制

34
16

解题报告:

   不难发现其实就是个二分最优匹配,可以选择KM算法或者费用流来解决这个问题。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;const int MAXN = 70000;
const int MAXM = 100005;
const int INF = 0x3f3f3f3f;
struct Edge {int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;
void init(int n) {N = n;tol = 0;memset(head, -1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost) {edge[tol].to = v;edge[tol].cap = cap;edge[tol].cost = cost;edge[tol].flow = 0;edge[tol].next = head[u];head[u] = tol++;edge[tol].to = u;edge[tol].cap = 0;edge[tol].cost = -cost;edge[tol].flow = 0;edge[tol].next = head[v];head[v] = tol++;
}
bool spfa(int s,int t) {queue<int>q;for(int i = 0; i <= N; i++) {dis[i] = INF;vis[i] = false;pre[i] = -1;}dis[s] = 0;vis[s] = true;q.push(s);while(!q.empty()) {int u = q.front();q.pop();vis[u] = false;for(int i = head[u]; i !=-1; i = edge[i].next) {int v = edge[i].to;if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost ) {dis[v] = dis[u] + edge[i].cost;pre[v] = i;if(!vis[v]) {vis[v] = true;q.push(v);}}}}if(pre[t] ==-1)return false;else return true;
}
int minCostMaxflow(int s,int t,int &cost) {int flow = 0;cost = 0;while(spfa(s,t)) {int Min = INF;for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {if(Min > edge[i].cap-edge[i].flow)Min = edge[i].cap-edge[i].flow;}for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {edge[i].flow += Min;edge[i^1].flow-= Min;cost += edge[i].cost * Min;}flow += Min;}return flow;
}
int main()
{int n;int t;cin>>t;while(t--) {scanf("%d",&n);//int st=0,ed=n1+n2+1;int st = 0,ed=n*2+1;init(ed+1);int a,b,w;for(int i = 1; i<=n; i++) {for(int j = n+1; j<=2*n; j++) {scanf("%d",&w);addedge(i,j,1,-w);}}for(int i = 1; i<=n; i++) addedge(st,i,1,0);for(int i = n+1; i<=2*n; i++) addedge(i,ed,1,0);int cost;int ans = minCostMaxflow(st,ed,cost);printf("%d\n",-cost);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441131.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql瓶颈分析_网站瓶颈分析—MYSQL性能分析

一、关于慢查询设置和分析查找慢查询参数mysql> show variables like long%;---------------------------| Variable_name | Value |---------------------------| long_query_time | 10.000000 |---------------------------mysql> show variables like slow%;---------…

【CodeForces - 289C】Polo the Penguin and Strings (水题,字符串,思维构造,有坑)

题干&#xff1a; Little penguin Polo adores strings. But most of all he adores strings of length n. One day he wanted to find a string that meets the following conditions: The string consists of n lowercase English letters (that is, the strings length e…

【CodeForces - 289D】Polo the Penguin and Houses (带标号的无根树,Cayley定理,Prufer编码)

题干&#xff1a; Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≤ pi ≤ n). Little penguin Po…

离散信号的抽取和内插例题_《数字信号处理》学习指导与题解 2011年版

《数字信号处理》学习指导与题解出版时间&#xff1a;2011年版内容简介《〈数字信号处理〉学习指导与题解》对“数字信号处理”教学大纲要求的所有知识点进行了提纲挈领的阐述&#xff0c;对于教材《数字信号处理》(吴瑛等主编&#xff0c;2009年8月由西安电子科技大学 出版发行…

【CodeForces - 289E 】Polo the Penguin and XOR operation (数学,异或,贪心)

题干&#xff1a; Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive. For permutation p  p0, p1, ..., pn, Polo has defined its beauty — number . Expression means applying the operation …

龙果学院mysql分布式集群代码_龙果学院-MySQL大型分布式集群解决方案

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼1 课程概述2 课程背景3 纵观大型网站架构发展&#xff0c;总结持久化部分需要应对的问题4 操作系统安装以及配置5 在CentOS上通过yum安装mysql5.76 mysql初次见面-mysql5.7的用户以及安全策略7 mysql初次见面续-mysql基本操作8 认识…

【SPOJ - TOURS 387】Travelling tours (最小费用最大流,拆点)

题干&#xff1a; In Hanoi, there are N beauty-spots (2 < N < 200), connected by M one-way streets. The length of each street does not exceed 10000. You are the director of a travel agency, and you want to create some tours around the city which sati…

python问题化教学设计_基于IPO的Python教学设计

冯艳茹 陈平摘要&#xff1a;程序设计基础课程是培养大学生解决计算问题的思维和能力的课程&#xff0c;使用Python作为大学生的首门编程语言课程&#xff0c;可操作性强&#xff0c;入门容易&#xff0c;上手快。该文提出了基于IPO的教学设计新思维&#xff0c;使教学活动和教…

【SPOJ - SCITIES】Selfish Cities (二分图最优匹配,最大费用流)

题干&#xff1a; Far, far away there is a world known as Selfishland because of the nature of its inhabitants. Hard times have forced the cities of Selfishland to exchange goods among each other. C1 cities are willing to sell some goods and the other C2 c…

将方孔分段的lisp_AutoLisp编写工程地质剖面图小工具

AutoLisp编写工程地质剖面图小工具朱红雷李健民 (浙江省水利水电勘测设计院杭州 310002)在我院应用的CAD工程地质制图系统中&#xff0c;通常采用的各种高级语言编制的程序&#xff0c;一般是通过编制数据文件&#xff0c;生成CAD图形数据交换文件(一般为*.SCR或*.DXF)达到绘制…

【CodeForces - 922B 】Magic Forest (数学,异或,暴力,水题,三元组问题)

题干&#xff1a; Imp is in a magic forest, where xorangles grow (wut?) A xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the …

【牛客 -2A】矩阵(二分,字符串哈希)

题干&#xff1a; 给出一个n * m的矩阵。让你从中发现一个最大的正方形。使得这样子的正方形在矩阵中出现了至少两次。输出最大正方形的边长。 输入描述: 第一行两个整数n, m代表矩阵的长和宽&#xff1b; 接下来n行&#xff0c;每行m个字符&#xff08;小写字母&#xff09…

java生产者消费者代码_Java实现Kafka生产者消费者代码实例

Kafka的结构与RabbitMQ类似&#xff0c;消息生产者向Kafka服务器发送消息&#xff0c;Kafka接收消息后&#xff0c;再投递给消费者。生产者的消费会被发送到Topic中&#xff0c;Topic中保存着各类数据&#xff0c;每一条数据都使用键、值进行保存。每一个Topic中都包含一个或多…

java dom创建xml文件_Java 如何使用dom方式读取和创建xml文件

Java 如何使用dom方式读取和创建xml文件发布时间&#xff1a;2020-11-11 17:08:31来源&#xff1a;亿速云阅读&#xff1a;101作者&#xff1a;Leah本篇文章给大家分享的是有关Java 如何使用dom方式读取和创建xml文件&#xff0c;小编觉得挺实用的&#xff0c;因此分享给大家学…

【CodeForces - 304B】Calendar (前缀和,水题)

题干&#xff1a; Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendars scheme of le…

java 刷新jtextarea_Java JTextArea不能实时刷新的问题

相信JTextArea用法都了解吧&#xff0c;JTextArea textArea new JTextArea();生成一块空白的区域&#xff0c; 我的需求就是点击发送邮件按钮后&#xff0c;后台的执行日志能输出到textArea中。但是我点击发送按钮的时候&#xff0c;由于邮件的附件要好久&#xff0c;界面一直…

【CodeForces - 312C】The Closest Pair (思维)

题干&#xff1a; Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Excee…

java request 封装对象_java通过request自动封装复杂对象

参考&#xff1a;Jfinal源码&#xff0c;在上面基础上改的&#xff0c;然后分享出来适用JAVAEE平台[Java]代码/*** 实现深层封装对象的实例 从request封装对象* 举例&#xff1a;* House.class 属性有三个 ID:id 名称&#xff1a;name 门类&#xff1a;Door doorDoor类: id nam…

【UVA - 10020 】Minimal coverage (贪心,区间覆盖问题)

题干&#xff1a;&#xff08;Uva题不给题干了&#xff09; t组样例&#xff0c;每组首先给出一个M&#xff0c;然后给出一些线段&#xff08;0 0结束&#xff09;&#xff0c;然后问怎么取能使得最少的线段覆盖区间[0, M]。 Sample Input 2 1 -1 0 -5 -3 2 5 0 0 1 -1 0 0 1 …

java邮箱地址正则表达式_Java 中用正则表达式修改 Email 地址

需求系统中有一列会用来存储 email 地址&#xff0c;现在需要对输入的字符串进行过滤&#xff0c;要求是&#xff0c;把无效的地址过滤掉。有一些需要说明的是这些地址是通过图像识别得到的&#xff0c;有些是用户自己输入的已有历史记录已经存在了脏数据&#xff0c;需要替换这…