博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深海机器人问题
阅读量:7030 次
发布时间:2019-06-28

本文共 2405 字,大约阅读时间需要 8 分钟。

费用流,两个点间连费用为负价值容量为1的边,再连费用为零容量为INF的边,建立S,T,分别向起点终点连边,跑最小费用流,对答案取反即可

# include 
# define RG register# define IL inline# define Fill(a, b) memset(a, b, sizeof(a))using namespace std;typedef long long ll;const int _(1010), __(1e6 + 10), INF(2e9);IL ll Read(){ char c = '%'; ll x = 0, z = 1; for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1; for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0'; return x * z;}int a, b, p, q, id[20][20];int cnt, fst[_], w[__], to[__], nxt[__], dis[_], vis[_], S, T, cost[__], pe[_], pv[_], max_flow, max_cost;queue
Q;IL void Add(RG int u, RG int v, RG int f, RG int co){ cost[cnt] = co; w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++; cost[cnt] = -co; w[cnt] = 0; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;}IL bool Bfs(){ Q.push(S); Fill(dis, 127); dis[S] = 0; vis[S] = 1; while(!Q.empty()){ RG int u = Q.front(); Q.pop(); for(RG int e = fst[u]; e != -1; e = nxt[e]){ if(!w[e] || dis[to[e]] <= dis[u] + cost[e]) continue; dis[to[e]] = dis[u] + cost[e]; pe[to[e]] = e; pv[to[e]] = u; if(!vis[to[e]]) vis[to[e]] = 1, Q.push(to[e]); } vis[u] = 0; } if(dis[T] >= dis[T + 1]) return 0; RG int ret = INF; for(RG int u = T; u != S; u = pv[u]) ret = min(ret, w[pe[u]]); for(RG int u = T; u != S; u = pv[u]) w[pe[u]] -= ret, w[pe[u] ^ 1] += ret; max_cost -= ret * dis[T]; max_flow += ret; return 1;}int main(RG int argc, RG char *argv[]){ Fill(fst, -1); a = Read(); b = Read(); p = Read(); q = Read(); for(RG int i = 0; i <= p; ++i) for(RG int j = 0; j <= q; ++j) id[i][j] = ++cnt; T = cnt + 1; cnt = 0; for(RG int i = 0; i <= p; ++i) for(RG int j = 1, v; j <= q; ++j) v = Read(), Add(id[i][j - 1], id[i][j], 1, -v), Add(id[i][j - 1], id[i][j], INF, 0); for(RG int j = 0; j <= q; ++j) for(RG int i = 1, v; i <= p; ++i) v = Read(), Add(id[i - 1][j], id[i][j], 1, -v), Add(id[i - 1][j], id[i][j], INF, 0); for(RG int i = 1, k, x, y; i <= a; ++i) k = Read(), x = Read(), y = Read(), Add(S, id[x][y], k, 0); for(RG int i = 1, k, x, y; i <= b; ++i) k = Read(), x = Read(), y = Read(), Add(id[x][y], T, k, 0); while(Bfs()); printf("%d\n", max_cost); return 0;}

转载于:https://www.cnblogs.com/cjoieryl/p/8206323.html

你可能感兴趣的文章
POJ NOI MATH-7653 地球人口承载力估计
查看>>
iOS UI高级之网络编程(HTTP协议)
查看>>
使用cocoaPods import导入时没有提示的解决办法
查看>>
iOS数据持久化存储之归档NSKeyedArchiver
查看>>
JavaScript面向对象
查看>>
Intellij修改模板代码
查看>>
2.页面布局示例笔记
查看>>
一些老游戏CPU 100%占用的解决方法
查看>>
f5基本介绍
查看>>
博前语
查看>>
Java SE核心之一:常用类,包装类,其他基本数据类型包装类。
查看>>
郑捷《机器学习算法原理与编程实践》学习笔记(第二章 中文文本分类(一))...
查看>>
python (ploit)
查看>>
Android 用achartengine 画折线图怎么显示不正确
查看>>
程序简单的测试与升级(暨实践第一次作业)
查看>>
信号处理
查看>>
【100题】第五十九题 用C++编写不能被继承的类
查看>>
轻描淡写
查看>>
mysql基本操作
查看>>
39.CSS3弹性伸缩布局【下】
查看>>