Gym - 101981I-MagicPotion-最大流

題目描述

There are n heroes and m monsters living in an island. The monsters became very vicious these days, so the heroes decided to diminish the monsters in the island. However, the i-th hero can only kill one monster belonging to the set Mi. Joe, the strategist, has k bottles of magic potion, each of which can buff one hero’s power and let him be able to kill one more monster. Since the potion is very powerful, a hero can only take at most one bottle of potion.
Please help Joe find out the maximum number of monsters that can be killed by the heroes if he uses the optimal strategy.
Input
The first line contains three integers n, m, k (1 ≤ n, m, k ≤ 500) — the number of heroes, the number of monsters and the number of bottles of potion.
Each of the next n lines contains one integer ti , the size of Mi, and the following ti integers Mi,j (1 ≤ j ≤ ti), the indices (1-based) of monsters that can be killed by the i-th hero (1 ≤ ti ≤ m, 1 ≤ Mi,j ≤ m).
Output
Print the maximum number of monsters that can be killed by the heroes.
Sample Input
sample input 1
3 5 2
4 1 2 3 5
2 2 5
2 1 2
sample input 2
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
Sample Output
sample output 1
4
sample output 2
7

題目分析

發現是匹配問題,思考將問題建圖后用最大流算法進行解決。

如果沒有k個藥水,則很明確,將問題轉化成一個二部圖,從源點到英雄,從英雄到怪獸,從怪獸到匯點,路徑上的容量都為1,則最大流就是殺死的最多的怪獸。

因為還有k個藥水,并且每一個英雄都只能拿一個藥水,我們從源點引出一個點,容量為k,再從這個點到每個英雄引一條路徑,每條路徑容量為1

AC代碼

#include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<cmath>using namespace std;typedef long long ll;
const int MAXN=1020;
const int INF=0x3f3f3f3f;struct Edge
{int from,to,cap,flow;Edge(int _from,int _to,int _cap,int _flow):from(_from),to(_to),cap(_cap),flow(_flow){}
};
struct Dinic
{int s,t;vector<Edge> edges;vector<int> G[MAXN];int d[MAXN]; bool vis[MAXN]; int cur[MAXN];void addEdge(int from,int to,int cap){edges.push_back((Edge){from,to,cap,0});edges.push_back((Edge){to,from,0,0});int tt=edges.size();G[from].push_back(tt-2);G[to].push_back(tt-1);}bool BFS(){memset(vis,0,sizeof(vis));queue<int>Q;Q.push(s);d[s]=0;vis[s]=1;while(!Q.empty()){int x=Q.front(); Q.pop();for(int i=0;i<G[x].size();++i){Edge &e =edges[G[x][i]];if(!vis[e.to] && e.cap>e.flow){vis[e.to]=1;d[e.to]=d[x]+1;Q.push(e.to);}}}return vis[t];}int DFS(int x,int a){if(x==t || a==0) return a;int flow=0,f;for(int&i=cur[x];i<G[x].size();++i){Edge& e=edges[G[x][i]];if(d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0){e.flow+=f;edges[G[x][i]^1].flow-=f;flow+=f;a-=f;if(a==0) break;}}return flow;}int MF(int s,int t){this->s=s; this->t=t;int flow=0;while(BFS()){memset(cur,0,sizeof(cur));flow+=DFS(s,INF);}return flow;}
}dinic;int n,m,k;int main()
{scanf("%d%d%d",&n,&m,&k);int tmp=n+m+1;int t=tmp+1;int s=0;dinic.addEdge(s,tmp,k);for(int i=1;i<=n;i++){dinic.addEdge(s,i,1); dinic.addEdge(tmp,i,1);int t,u; scanf("%d",&t);while(t--){scanf("%d",&u); dinic.addEdge(i,u+n,1);}}for(int i=1;i<=m;i++){dinic.addEdge(i+n,t,1);}printf("%d\n",dinic.MF(s,t));return 0;
}

經驗總結

第一次寫網絡流的題目。感覺就是在使用工具,如何正確地使用才是問題的關鍵,而不一定要求掌握工具怎么造出來的。這對當下我來說很重要。當然掌握工具的原理以后就能更好的使用工具。后面會花功夫具體去學習網絡流的原理的。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383744.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383744.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383744.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c++仿函數 functor

https://www.cnblogs.com/decade-dnbc66/p/5347088.html內容整理自國外C教材先考慮一個簡單的例子&#xff1a;假設有一個vector<string>&#xff0c;你的任務是統計長度小于5的string的個數&#xff0c;如果使用count_if函數的話&#xff0c;你的代碼可能長成這樣&#…

HDU4812-D Tree-樹分治

題目描述 There is a skyscraping tree standing on the playground of Nanjing University of Science and Technology. On each branch of the tree is an integer (The tree can be treated as a connected graph with N vertices, while each branch can be treated as a v…

成為C++高手之實戰項目

https://blog.csdn.net/niu_gao/article/details/51458721 在內存中模擬出一副牌&#xff0c;然后模擬洗牌&#xff0c;發牌等動作。 流程是這樣的&#xff1a;構建一副牌保存到一個數組中—洗牌—創建玩家—向玩家發牌–輸出每個玩家的牌。 #include <stdio.h> #include…

C++中String類的實現

https://www.cnblogs.com/zhizhan/p/4876093.html原文&#xff1a;http://noalgo.info/382.html String是C中的重要類型&#xff0c;程序員在C面試中經常會遇到關于String的細節問題&#xff0c;甚至要求當場實現這個類。只是由于時間關系&#xff0c;可能只要求實現構造函數、…

Ubuntu軟件更新失敗

剛安裝好Ubuntu以后需要將系統的軟件都更新一下&#xff0c;但是遇到一個問題就是下載倉庫信息失敗&#xff0c;大概是這個樣子的錯誤&#xff1a; 經國遇到這樣的問題可以試一下下面這個命令&#xff1a; sudo rm -rf /var/lib/apt/lists/* sudo apt-get update參考網址&…

getsockname函數與getpeername函數的使用

https://www.tuicool.com/articles/V3Aveygetsockname和getpeername函數 getsockname函數用于獲取與某個套接字關聯的本地協議地址 getpeername函數用于獲取與某個套接字關聯的外地協議地址 定義如下&#xff1a;[cpp] view plaincopy#include<sys/socket.h> int gets…

Ubuntu根目錄空間不足

自己在固態硬盤上安裝的Ubuntu&#xff0c;結果只用了一天就顯示磁盤空間不足。查看空間以后發現Ubuntu自己安裝的時候默認給根目錄分配的是10GB,然而我們下載的軟件以及環境等一般都安裝在根目錄空間下&#xff0c;尤其是/usr目錄所占的空間很大。 不得已我在網上查找了如何給…

Linux命令【一】基本命令

shell命令和bash命令相同&#xff0c;指的是命令解析器 快捷鍵 history 所有的歷史命令ctrl P 向上滾動命令 ctrl N 向下滾動命令 ctrlB將光標向前移動 ctrlF將光標向后移動 ctrlA移動到命令行頭部 ctrlE移動到命令行尾部 光標刪除操作&#xff1a;刪除光標前面字符ctrlh或…

The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

http://www.jb51.net/article/119654.htmThe MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 意思貌似MYSQL還運行在 --skip-grant-tables模式&#xff0c;如何讓他回到原來的模式 第一種方法&#xff1a;原來在mysql.ini文…

解決Ubuntu“下載額外數據文件失敗 ttf-mscorefonts-installer”的問題

參考博客&#xff1a;傳送門 下載[ttf-mscorefonts-installer.zip](https://pan.baidu.com/s/1i5rLfMH) 密碼: h76g 然后解壓到下載的目錄&#xff0c;在當前目錄執行命令&#xff1a; sudo dpkg-reconfigure ttf-mscorefonts-installer這條命令手動指定文件夾的位置,重新配置…

【C語言】單鏈表的相關熱點面試題(包括:從尾到頭打印,逆置,冒泡,尋找中間節點,倒數k節點)

https://blog.csdn.net/hanjing_1995/article/details/51539599從尾到頭打印單鏈表[cpp] view plaincopyvoid FromTailToHeadPrint(SListNode*& head) { stack<SListNode*> s; SListNode* cur head; while (cur) { s.push(cur); …

Linux命令【二】終端+Vim

需要先安裝net-tools ifconfig eth0 網卡&#xff0c;硬件地址為MAC 地址&#xff0c;網卡編號&#xff0c;絕對不會重復 lo 回環地址 測試兩臺主機之間能否通信&#xff1a;ping IP或域名 [-c 4//回饋四條信息 -i//每隔多少秒回饋一次] 得到域名對應的IPnslookup 域名得到域…

Linux如何將文件中內容放到粘貼板上

沒有找到如何在vim中將內容復制到粘貼板上&#xff0c;只找到了使用另一個軟件進行操作。 首先安裝xsel sudo apt-get install xsel # 將剪切板中的內容輸出到文件 echo $(xsel --clipboard) >> a.txt# 將文件的內容復制到剪切板 cat a.txt | xsel --clipboard

【C語言】str類與men庫函數的實現(如:strcpy,strcmp,strstr,strcat,memmove,memcpy)

https://blog.csdn.net/hanjing_1995/article/details/51539583strcpy拷貝源字符串到子字符串&#xff0c;包括‘\0’。代碼實現&#xff1a;[cpp] view plaincopychar* strcpy(char* dst,const char* src) { assert(src); char* ret dst; while (*src) …

【筆試常考】C語言:深度剖析strlen,sizeof

https://blog.csdn.net/hanjing_1995/article/details/51539532在之前的博客中&#xff0c;我也探索過strlen,sizeof區別&#xff0c;詳情可見博客http://10740184.blog.51cto.com/10730184/1705820。關于strlen,sizeof均可求字符串長度&#xff0c;這兩者是筆試面試常考的知識…

vim環境配置 +vimplus配置

vim配置 參考網站&#xff1a;傳送門 這個網站詳細說明了vim配置的命令&#xff0c;我挑選了我想要用的部分&#xff0c;自己配置了一下。 配置vim的文件有兩個&#xff0c;一個是/etc/vim/vimrc 這個是系統配置文件&#xff0c;修改這個文件將會修改所有用戶的vim環境&…

劍指offer面試題:替換空格

https://blog.csdn.net/yanxiaolx/article/details/52235212題目&#xff1a;請實現一個函數&#xff0c;把字符串中的每個空格替換成“%20”。例如輸入“We are happy.”&#xff0c;則輸出“We%20are%20happy.”。解析&#xff1a;時間復雜度為O(n)的解法。完整代碼及測試用例…

數據庫原理及應用【一】引言

什么是數據庫&#xff1a;一個大規模的集成的數據集合 作用&#xff1a;描述現實世界的實體(entities)以及實體之間的關系 管理數據庫的系統軟件&#xff1a;DBMS 文件是一個平滑的字符流&#xff0c;無法完成信息的檢索和管理 數據&#xff08;data&#xff09;:用來描述現…

Linux命令【三】gcc編譯+靜態庫+動態庫+makefile+gdb調試

用C編譯器編譯源文件&#xff1a;gcc 源文件 -o 可執行文件名 詳細步驟&#xff1a; gcc -E a.c -o a.i預處理器將頭文件展開&#xff0c;宏替換&#xff0c;去掉注釋gcc -S a.i -o a.s編譯器將C文件變成匯編文件gcc -c a.s -o a.o匯編器將會變文件變成二進制文件gcc a.o -o a…

用c++模擬實現一個學生成績管理系統

https://blog.csdn.net/yanxiaolx/article/details/53393437題目&#xff1a;用c模擬實現一個學生成績的信息管理系統&#xff0c;要求能添加、刪除、修改、查看和保存學生的信息等功能 源代碼如下:[cpp] view plaincopy#define _CRT_SECURE_NO_WARNINGS #include<iostr…