2018 ACM/ICPC 南京 I题 Magic Potion

题解:最大流板题;增加两个源点,一个汇点。第一个源点到第二个源点连边,权为K,然后第一个源点再连其他点(英雄点)边权各为1,然后英雄和怪物之间按照所给连边(边权为1)。

每个怪物连终点,边权为1;

参考代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define INF 0x3f3f3f3f
 4 const int maxn = 2100;
 5 int n,m,k,s,t,u,v,w,num,num1;
 6 struct Edge {
 7     int from, to, cap, flow;
 8 };
 9 vector<Edge> edges;
10 vector<int> G[maxn];
11 bool vis[maxn];
12 int d[maxn], cur[maxn];
13 void Init()
14 {
15     memset(d,0,sizeof d);
16     for(int i=0;i<=n+m+4;i++) G[i].clear();
17 }
18 void addedge(int from, int to, int cap)
19 {
20     edges.push_back((Edge){from, to, cap, 0});
21     edges.push_back((Edge){to, from, 0, 0});
22     int m = edges.size();
23     G[from].push_back(m-2); G[to].push_back(m-1);
24 }
25 bool bfs()
26 {
27     memset(vis,0,sizeof vis);
28     queue<int> q;
29     q.push(s);
30     d[s] = 0; vis[s] = 1;
31     while (!q.empty())
32     {
33         int x = q.front(); q.pop();
34         for(int i = 0; i < G[x].size(); ++i)
35         {
36             Edge &e = edges[G[x][i]];
37             if (!vis[e.to] && e.cap > e.flow)
38             {
39                 vis[e.to] = 1;
40                 d[e.to] = d[x] + 1;
41                 q.push(e.to);
42             }
43         }
44     }
45     return vis[t];
46 }
47
48 int dfs(int x,int a)
49 {
50     if(x == t || a == 0) return a;
51     int flow = 0, f;
52     for(int &i = cur[x]; i < G[x].size(); ++i)
53     {
54         Edge &e = edges[G[x][i]];
55         if (d[e.to] == d[x] + 1 && (f=dfs(e.to, min(a, e.cap-e.flow))) > 0)
56         {
57             e.flow += f;
58             edges[G[x][i]^1].flow -= f;
59             flow += f; a -= f;
60             if (a == 0) break;
61         }
62     }
63     return flow;
64 }
65
66 int Maxflow(int s, int t)
67 {
68     int flow = 0;
69     while (bfs())
70     {
71         memset(cur,0,sizeof cur);
72         flow += dfs(s, INF);
73     }
74     return flow;
75 }
76 int main()
77 {
78     scanf("%d%d%d",&n,&m,&k);
79     Init();s=1;t=n+m+3;addedge(s,2,k);
80     for(int i=1;i<=n;++i) addedge(s,i+2,1),addedge(2,i+2,1);
81     for(int i=1;i<=n;++i)
82     {
83         scanf("%d",&num);
84         while(num--)
85         {
86             scanf("%d",&num1);
87             addedge(i+2,num1+n+2,1);
88         }
89     }
90     for(int i=1;i<=m;++i) addedge(i+n+2,t,1);
91     printf("%d\n",Maxflow(s,t));
92     return 0;
93  } 

原文地址:https://www.cnblogs.com/songorz/p/10331465.html

时间: 2024-08-30 11:28:34

2018 ACM/ICPC 南京 I题 Magic Potion的相关文章

2018ACM-ICPC亚洲区域赛南京站I题Magic Potion(网络流)

http://codeforces.com/gym/101981/attachments 题意:有n个英雄,m个敌人,k瓶药剂,给出每个英雄可以消灭的敌人的编号.每个英雄只能消灭一个敌人,但每个英雄只能消灭一个敌人.现在有药剂,英雄喝了之后可以多消灭一个敌人,但每个英雄只能喝一瓶,问最多能消灭多少个敌人. 下午在实验室队内自己开训练,和JC大佬那队一起开的,当时JC大佬他们队开的J题,没有看I题,当我们队AC之后JC大佬才看了I题,听到他们说,这题就差直接把网络流三个字写在题目里了.确实非常明显

2017 ACM/ICPC 沈阳 K题 Rabbits

Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number line, each occupying a different integer. In a single move, one of the outer rabbits jumps into a space between any other two. At no point may two rabbits occupy the same p

2017 ACM/ICPC 沈阳 F题 Heron and his triangle

A triangle is a Heron's triangle if it satisfies that the side lengths of it are consecutive integers t−1, t, t+ 1 and thatits area is an integer. Now, for given n you need to find a Heron's triangle associated with the smallest t bigger than or equa

2017 ACM/ICPC 沈阳 L题 Tree

Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the concept of a tree here, please omit this problem

2019 ACM ICPC 南京站 H. Prince and Princess

题意 王子想要娶公主,但是需要完成一个挑战:在一些房间中找出公主在哪. 每个房间有一个人,他们彼此知道谁在哪个房间.可以问他们三种问题: 你是谁? 在某个房间是谁? 公主在哪个房间? 有三类人,一类一定说真话,一类一定说假话,一类可能说真话可能说假话. 王子知道这三类人的人数分别为 \(a\), \(b\), \(c\),求能否通过问一些问题找到公主在哪,如果能,输出最少需要的问题数. 思路 第三类人有可能说假话,因此最坏情况就是说假话,所以把他们视为第二类人. 首先问所有人第三个问题,那么最坏

【转】ACM/ICPC生涯总结暨退役宣言—alpc55

转自:http://hi.baidu.com/accplaystation/item/ca4c2ec565fa0b7fced4f811 ACM/ICPC生涯总结暨退役宣言—alpc55 前言 早就该写这篇文章了,但是也很不想去写.毕竟是为之奋斗了两年的目标,不是说舍得就舍得的.然而,自己毕竟是到了该退的时候了,与其扭扭捏捏,不如挥一挥衣袖,尚落得一份潇洒.回首这两年来,有很多是需要总结的.在这里不分巨细的记录下来,或许有点像流水账,但是更多的,是一份对过去的难忘. 童年 我的ACM/ICPC的生

ACM/ICPC算法训练 之 数学很重要-浅谈“排列计数” (DP题-POJ1037)

这一题是最近在看Coursera的<算法与设计>的公开课时看到的一道较难的DP例题,之所以写下来,一方面是因为DP的状态我想了很久才想明白,所以借此记录,另一方面是看到这一题有运用到 排列计数 的方法,虽然排列计数的思路简单,但却是算法中一个数学优化的点睛之笔. Poj1037  A decorative fence 题意:有K组数据(1~100),每组数据给出总木棒数N(1~20)和一个排列数C(64位整型范围内),N个木棒长度各异,按照以下条件排列,并将所有可能结果进行字典序排序 1.每一

2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money. For example, the price when

网络流板子/费用流板子 2018南京I题+2016青岛G题

2018南京I题: dinic,链式前向星,数组队列,当前弧优化,不memset全部数组,抛弃满流点,bfs只找一条增广路,每次多路增广 #include <bits/stdc++.h> #define ll long long #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define rep(ii,a,b) for(int ii=a;ii<=b;++ii) using namespace std; con