经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
洛谷P2045 方格取数加强版(费用流)
来源:cnblogs  作者:自为风月马前卒  时间:2019/1/30 9:42:23  对本文有异议

题意

题目链接

Sol

这题能想到费用流就不难做了

从S向(1, 1)连费用为0,流量为K的边

从(n, n)向T连费用为0,流量为K的边

对于每个点我们可以拆点限流,同时为了保证每个点只被经过一次,需要拆点。

对于拆出来的每个点,在其中连两条边,一条为费用为点权,流量为1,另一条费用为0,流量为INF

相邻两个点之间连费用为0,流量为INF的边。

跑最大费用最大流即可

  1. #include<bits/stdc++.h>
  2. #define Pair pair<int, int>
  3. #define MP(x, y) make_pair(x, y)
  4. #define fi first
  5. #define se second
  6. #define int long long
  7. #define LL long long
  8. #define Fin(x) {freopen(#x".in","r",stdin);}
  9. #define Fout(x) {freopen(#x".out","w",stdout);}
  10. //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
  11. //char buf[(1 << 22)], *p1 = buf, *p2 = buf;
  12. using namespace std;
  13. const int MAXN = 51, MAX = 1e5 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
  14. const double eps = 1e-9, PI = acos(-1);
  15. template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
  16. template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
  17. template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
  18. template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
  19. template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
  20. template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
  21. template <typename A> inline void debug(A a){cout << a << '\n';}
  22. template <typename A> inline LL sqr(A x){return 1ll * x * x;}
  23. inline int read() {
  24. char c = getchar(); int x = 0, f = 1;
  25. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  26. while(c >= '0' && c <= '9') x = (x * 10 + c - '0') % mod, c = getchar();
  27. return x * f;
  28. }
  29. int N, K, S = 0, T = 1e5 - 1, a[MAXN][MAXN], dis[MAX], vis[MAX], Pre[MAX], id[MAXN][MAXN][2], cnt, MaxCost;
  30. struct Edge {
  31. int u, v, w, f, nxt;
  32. }E[MAX];
  33. int head[MAX], num;
  34. inline void add_edge(int x, int y, int w, int f) {
  35. E[num] = (Edge) {x, y, w, f, head[x]};
  36. head[x] = num++;
  37. }
  38. inline void AE(int x, int y, int w, int f) {
  39. add_edge(x, y, w, f);
  40. add_edge(y, x, -w, 0);
  41. }
  42. bool SPFA() {
  43. queue<int> q; q.push(S);
  44. memset(dis, -0x3f, sizeof(dis));
  45. memset(vis, 0, sizeof(vis));
  46. dis[S] = 0;
  47. while(!q.empty()) {
  48. int p = q.front(); q.pop(); vis[p] = 0;
  49. for(int i = head[p]; ~i; i = E[i].nxt) {
  50. int to = E[i].v;
  51. if(E[i].f && dis[to] < dis[p] + E[i].w) {
  52. dis[to] = dis[p] + E[i].w; Pre[to] = i;
  53. if(!vis[to]) vis[to] = 1, q.push(to);
  54. }
  55. }
  56. }
  57. return dis[T] > -INF;
  58. }
  59. void F() {
  60. int canflow = INF;
  61. for(int i = T; i != S; i = E[Pre[i]].u) chmin(canflow, E[Pre[i]].f);
  62. for(int i = T; i != S; i = E[Pre[i]].u) E[Pre[i]].f -= canflow, E[Pre[i] ^ 1].f += canflow;
  63. MaxCost += canflow * dis[T];
  64. }
  65. void MCMF() {
  66. while(SPFA()) F();
  67. }
  68. signed main() {
  69. // freopen("a.in", "r", stdin);
  70. memset(head, -1, sizeof(head));
  71. N = read(); K = read();
  72. for(int i = 1; i <= N; i++)
  73. for(int j = 1; j <= N; j++)
  74. a[i][j] = read(), id[i][j][0] = ++cnt, id[i][j][1] = ++cnt;
  75. AE(S, id[1][1][0], 0, K);
  76. AE(id[N][N][1], T, 0, K);
  77. for(int i = 1; i <= N; i++) {
  78. for(int j = 1; j <= N; j++) {
  79. AE(id[i][j][0], id[i][j][1], a[i][j], 1);
  80. AE(id[i][j][0], id[i][j][1], 0, INF);
  81. if(i + 1 <= N) AE(id[i][j][1], id[i + 1][j][0], 0, INF);
  82. if(j + 1 <= N) AE(id[i][j][1], id[i][j + 1][0], 0, INF);
  83. }
  84. }
  85. MCMF();
  86. printf("%d", MaxCost);
  87. return 0;
  88. }
  89. /*
  90. 3 2
  91. 1 2 3
  92. 0 2 1
  93. 1 4 2
  94. */

原文链接:http://www.cnblogs.com/zwfymqz/p/10336008.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号