hdu - 4971 - A simple brute force problem.(最大权闭合图)
题意:n(n <= 20)个项目,m(m <= 50)个技术问题,做完一个项目能够有收益profit (<= 1000),做完一个项目必须解决对应的技术问题,解决一个技术问题须要付出cost ( <= 1000),技术问题之间有先后依赖关系,求最大收益。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4971
——>>项目必须解决相应的技术问题,技术问题之间也存在依赖,相应闭合图,最大收益相应最大权和。。于是,最大权闭合图,最小割,最大流上场。。
建图:
1)超级源S = n + m, 超级汇T = n + m + 1
2)对于每一个项目i:S -> i (profit[i])
3)对于每一个技术问题i:i + n -> T (cost[i])
4)对于项目 i 必须解决的技术问题 j:i -> j + n (INF)
5)对于技术问题 j 必须先解决的技术问题
i: i + n -> j + n (INF) (这里我认为应为:j + n -> i + n (INF),这样理解才对,但是对不上例子,提交也WA。。)
然后,Dinic上场。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using std::queue;
using std::min; const int MAXN = 20 + 50 + 10;
const int MAXM = 20 + 1000 + 2500 + 50 + 10;
const int INF = 0x3f3f3f3f; int n, m, sum;
int hed[MAXN];
int cur[MAXN], h[MAXN];
int ecnt;
int S, T; struct EDGE
{
int to;
int cap;
int flow;
int nxt;
} edges[MAXM << 1]; void Init()
{
ecnt = 0;
memset(hed, -1, sizeof(hed));
sum = 0;
} void AddEdge(int u, int v, int cap)
{
edges[ecnt].to = v;
edges[ecnt].cap = cap;
edges[ecnt].flow = 0;
edges[ecnt].nxt = hed[u];
hed[u] = ecnt++;
edges[ecnt].to = u;
edges[ecnt].cap = 0;
edges[ecnt].flow = 0;
edges[ecnt].nxt = hed[v];
hed[v] = ecnt++;
} void Read()
{
int profit, cost, pc, tp; scanf("%d%d", &n, &m);
S = n + m;
T = n + m + 3;
for (int i = 0; i < n; ++i)
{
scanf("%d", &profit);
AddEdge(S, i, profit);
sum += profit;
}
for (int i = 0; i < m; ++i)
{
scanf("%d", &cost);
AddEdge(i + n, T, cost);
}
for (int i = 0; i < n; ++i)
{
scanf("%d", &pc);
for (int j = 0; j < pc; ++j)
{
scanf("%d", &tp);
AddEdge(i, tp + n, INF);
}
}
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < m; ++j)
{
scanf("%d", &tp);
if (tp)
{
AddEdge(i + n, j + n, INF);
}
}
}
} bool Bfs()
{
memset(h, -1, sizeof(h));
queue<int> qu;
qu.push(S);
h[S] = 0;
while (!qu.empty())
{
int u = qu.front();
qu.pop();
for (int e = hed[u]; e != -1; e = edges[e].nxt)
{
int v = edges[e].to;
if (h[v] == -1 && edges[e].cap > edges[e].flow)
{
h[v] = h[u] + 1;
qu.push(v);
}
}
} return h[T] != -1;
} int Dfs(int u, int cap)
{
if (u == T || cap == 0) return cap; int flow = 0, subFlow;
for (int e = cur[u]; e != -1; e = edges[e].nxt)
{
cur[u] = e;
int v = edges[e].to;
if (h[v] == h[u] + 1 && (subFlow = Dfs(v, min(cap, edges[e].cap - edges[e].flow))) > 0)
{
flow += subFlow;
edges[e].flow += subFlow;
edges[e ^ 1].flow -= subFlow;
cap -= subFlow;
if (cap == 0) break;
}
} return flow;
} int Dinic()
{
int maxFlow = 0; while (Bfs())
{
memcpy(cur, hed, sizeof(hed));
maxFlow += Dfs(S, INF);
} return maxFlow;
} int main()
{
int t, kase = 0; scanf("%d", &t);
while (t--)
{
Init();
Read();
printf("Case #%d: %d\n", ++kase, sum - Dinic());
} return 0;
}
hdu - 4971 - A simple brute force problem.(最大权闭合图)的更多相关文章
- HDU 4971 A simple brute force problem.
A simple brute force problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged o ...
- HDU 4971 - A simple brute force problem【最大权闭合图】
有n(20)个工程,完成每个工程获得收益是p[i],m(50)个需要解决的难题,解决每个难题花费是c[i] 要完成第i个工程,需要先解决ki个问题,具体哪些问题,输入会给出 每个难题之间可能有依赖关系 ...
- 【最小割】HDU 4971 A simple brute force problem.
说是最大权闭合图.... 比赛时没敢写.... 题意 一共同拥有n个任务,m个技术 完毕一个任务可盈利一些钱,学习一个技术要花费钱 完毕某个任务前须要先学习某几个技术 可是可能在学习一个任务前须要学习 ...
- HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...
- A simple brute force problem.
hdu4971:http://acm.hdu.edu.cn/showproblem.php?pid=4971 题意:给你n个项目,每完成一个项目会有一定的收益,但是为了完成某个项目,要先学会一些技能, ...
- HDU5772 String problem 最大权闭合图+巧妙建图
题意:自己看吧(不是很好说) 分析: 网络流:最大权闭合子图. 思路如下: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得 ...
- hdu 4972 A simple dynamic programming problem(高效)
pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...
- HDU 3879 && BZOJ 1497:Base Station && 最大获利 (最大权闭合图)
http://acm.hdu.edu.cn/showproblem.php?pid=3879 http://www.lydsy.com/JudgeOnline/problem.php?id=1497 ...
- hdu 3061 Battle 最大权闭合图
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3061 由于小白同学近期习武十分刻苦,很快被晋升为天策军的统帅.而他上任的第一天,就面对了一场极其困难的 ...
随机推荐
- 将App程序发布到苹果App Store
发布iOS应用程序到App Store - 前期工作 要发布iOS应用程序到App Store首先需要一个iOS developer帐号,账号是收费的,$99美元/年.即便是免费应用也需要一个开发者账 ...
- php对xml进行简单的增删改查(CRUD)操作
假如有以下xml文件: <?xml version="1.0" encoding="UTF-8"? > <setting> &l ...
- DateTime与timeStamp的转换
DateTime转换为timeStamp: DateTime dt = DateTime.Now; DateTime startTime = TimeZone.CurrentTi ...
- linux(debian)系统django配远程连接sqlserver数据库
费了将近一天时间.最终解决,记下来留给须要的人 须要安装的: python-odbc : https://github.com/mkleehammer/pyodbc下载后install 安装pytho ...
- 闪回drop恢复表后sql运行计划异常
-----正常运行计划 set autotrace traceonly set linesize 1000 select /*+index(t idx_object_id)*/ * from t wh ...
- (十)RabbitMQ消息队列-高可用集群部署实战
原文:(十)RabbitMQ消息队列-高可用集群部署实战 前几章讲到RabbitMQ单主机模式的搭建和使用,我们在实际生产环境中出于对性能还有可用性的考虑会采用集群的模式来部署RabbitMQ. Ra ...
- 【44.64%】【codeforces 743C】Vladik and fractions
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- mycat schema.xml 配置文件详解
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> &l ...
- css3背景透明文字不透明
在 FF/Chrome 等较新的浏览器中可以使用css属性background-color的rgba轻松实现背景透明,而文字保持不透明.而IE6/7/8浏览器不支持rgba,只有使用IE的专属滤镜fi ...
- bash - move faster
http://teohm.com/blog/shortcuts-to-move-faster-in-bash-command-line/ Shortcuts to move faster in Bas ...