【牛客 - 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,一经查实,立即删除!

相关文章

【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…

【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 …

【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 …

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

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

【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…

【牛客 - 272A】Phrase String(构造,水题)

题干&#xff1a; 给出v, k&#xff0c;请你找到最小的正整数n&#xff0c;满足&#xff1a; n的二进制表示下存在一个长度为v的回文串&#xff0c;该回文串首尾都是1且n的二进制表示中至少有k个1。保证v,k均为偶数&#xff01; 由于n可能很大&#xff0c;你只需要输出对取模的…

【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

题干&#xff1a; Constructing Roads In JGShinings Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29933 Accepted Submission(s): 8496 Problem Description JGShinings kingdom consists of 2n…

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐吃糖豆 (fIb博弈)

题干&#xff1a; 小乐乐是一个比较喜欢吃糖豆的小孩子&#xff0c;小乐乐的哥哥大乐乐也同样爱吃糖豆。 作为一个小孩子&#xff0c;他们永远觉得谁吃掉了最后一个糖豆&#xff0c;谁吃的糖豆最多。 为了公平起见小乐乐与大乐乐商量吃糖豆的规则如下&#xff1a; 1. …

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐切割方块(思维,水题)

题干&#xff1a; 小乐乐的作业本是2n*2n的方格本。 某天小乐乐的童鞋&#xff0c;想要考验一下小乐乐。 他将小乐乐的一张方格纸中的某个格子(x,y)涂成黑色&#xff0c; 小乐乐能否在将4*4的方格本沿着方格边缘且切割线与黑色方格不存在公共交点的情况下将方格本切割成两…

java web svn_如何搭建svnadmin,一个简单的svnWEB页面

Svn Admin是一个Java开发的管理Svn服务器的项目用户的web应用。安装好Svn服务器端好&#xff0c;把Svn Admin部署好&#xff0c;就可以通过web浏览器管理Svn的项目&#xff0c;管理项目的用户&#xff0c;管理项目的权限。使得管理配置Svn简便&#xff0c;再也不需要每次都到服…

java 递归 时间复杂度_递归到底是怎么实现的?它的时间复杂度怎么算?

递归到底是个啥&#xff1f;常听见的一句话就是&#xff1a;自己调用自己。按照这个说法&#xff0c;写个简单的递归自己推导一下的确可以&#xff0c;但是总是有点绕&#xff0c;推着推着自己把自己陷进去了。递归函数运行时&#xff0c;实际上会进行一个压栈(思考栈的特点&am…

Java 重定向 无法写入_java IO 文件读入,写入,重定向

Java代码 packagestar20110526;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io…

【CodeForces - 298C】Parity Game (思维,有坑)

题干&#xff1a; You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and &q…

java中能构成循环的语句_《编程导论(Java)#183;3.2.4 循环语句》

本文全然复制《编程导论(Java)3.2.4 循环语句》的内容。除【】中的说明文字。请阅读和比較其它编程教材。我知道。假设我是一个刚開始学习的人&#xff0c;《编程导论(Java)》非常不适合自学。建议同学们阅读时&#xff0c;一定选择一本其它的书同一时候看&#xff0c;或上网。…

*【CodeForces - 1088 ABC】套题比赛,A水题B模拟C构造D交互

A. Input The only line contains the integer xx (1≤x≤100)(1≤x≤100). Output You should output two integers aa and bb, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (wit…

算法讲解 -- 区间dp经典模型与优化(石子归并)

石子合并问题是最经典的DP问题。首先它有如下3种题型&#xff1a; PPT讲解&#xff1a;点击打开链接 (1)有N堆石子&#xff0c;现要将石子有序的合并成一堆&#xff0c;规定如下&#xff1a;每次只能移动任意的2堆石子合并&#xff0c;合并花费为新合成的一堆石子的数量。求将…

cpu影响matlab仿真速度吗,Proteus仿真速度很慢的分析

类型&#xff1a;行业软件大小&#xff1a;1.1M语言&#xff1a;中文 评分&#xff1a;6.5标签&#xff1a;立即下载这篇文章是我的个人实践经验&#xff1a;很多朋友在做Proteus硬件仿真的时候可能都碰上了仿真速度慢的问题&#xff0c;在点击了开始仿真之后&#xff0c;CPU过…

mysql 7天自动拒单功能,mysql查询最近7天的数据,没有数据自动补0

select DATE( createtime) date , createtime, count(1) as count from order表 where DATEDIFF( now(), createtime)<7group by date ;12 月九号没有;给他在关联一张日期的表;--生成从今天开始完整的7天日期DECLARE LastSevenDaytable(day date)DECLARE StartDaydate …