TopCoder SRM 558 Div 1 - Problem 1000 SurroundingGame

传送门:https://284914869.github.io/AEoj/558.html

题目简述 

一个人在一个n * m棋盘上玩游戏,想要占领一个格子有两个方法: 

  在这个格子放一个棋子。
  这个格子周围(四联通)的格子中**都有棋子**。

在(i, j)中放棋子需要花费cost[i][j],占领(i, j)能获得benefit[i][j]。求一种放置棋子的方法,使得总收益(收益 - 花费)最大。

2<=n,m<=20

分析

一眼看上去,

状压?

我是不是dp学傻了。。根本想不出

网络流?

嗯,此题是一道非常套路的网络流练习题。

如果想不到对棋盘进行黑白染色,就GG了。

所以套路之一,你要对棋盘进行黑白染色,黑点放一边,白点放一边。

感受一下,肯定是建个图,连一些收益边、花费边,跑一遍最大流来求最小割之类的。

套路之二,引用一段zzx的话:“

要验证你建出来的图是否正确也很容易,

只要看看当你选择保留某条收益边的时候,会要割掉哪些边。

假如需要割边的情况和题目要求一致,这个图就建对了。”

如何建边?

我来讲一下我的心路历程。。。(毕竟我是一个网络流萌新,这应该是我做过的第5道网络流题目)。

首先,

我们先把所有收益算上,不算任何花费。这显然是不合法的,

所以建的图要满足,当收益与花费不合法的情况下,还能继续找到从源点到汇点的一条流量>0的路径。

这时需要割去一些边(增加花费,或减少收益),使得收益与花费合法,且割去的边权值和最小(即总收益最大),这就是最小割。

对于花费边,

某一个格子,得到它的收益,一种情况是用该格子的花费,还有一种情况是用与它相邻的四个格子的花费。

由于已经进行黑白染色,该格子与相邻四个格子的颜色不同。

那么可以脑补出下图:

 

图中0是源点,6是汇点。

左图中,可以选择割0-1,即用1的花费,或选择割2-6,3-6,4-6,5-6,即用相邻四个格子的花费。

考虑这样还没完,因为你也可以选择放弃这个格子的收益。

同时我们又不能再加一条收益边与上图串联,

因为放弃这个格子的收益,代表着相邻的格子的收益必须要靠它自己的花费。所以简单的串联很显然是不对的。

所以我们考虑,割掉1的收益边之后,左图不存在流量>0的路,但对右图没有影响。

于是诞生了下图的连边方式:

简单地说,就是把所有的点都拆成两个点,两个点之间连收益边。

如图中一号收益边,割去之后,不存在经过 1‘ 到汇点的路,但依然存在经过1,9,8,7再经过2再到汇点的路。这样就合法了。

题外话:

一开始以为输入的字符,A-Z代表10~35,a-z代表36-61,写完代码之后发现一直wa,过了好久发现自己搞反了

之后又把数组开小了。。。。真难过

代码:

  1 #include <cstdio>
  2 #include <string>
  3 #include <vector>
  4 #include <cstring>
  5 #include <iostream>
  6 #include <algorithm>
  7 using namespace std;
  8 #define _CLASSNAME_ SurroundingGame
  9 #define _METHODNAME_ maxScore
 10 #define _RC_ int
 11 #define _METHODPARMS_ vector <string> cost, vector <string> benefit
 12 #define ref(i,x,y)for(int i=x;i<=y;++i)
 13 #define def(i,x,y)for(int i=x;i>=y;--i)
 14 const int inf = (int)1e9;
 15 int n, m, op, ed, w1[21][21], w2[21][21];
 16 int cnt, head[804], d[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
 17 struct edge {
 18     int to, next, s;
 19     edge() {}
 20     edge(int a, int b, int c) : to(a), next(b), s(c) {}
 21 }e[10000];
 22 int p(int x, int y, int s) {
 23     return s*n*m + (x - 1)*m + y + 1;
 24 }
 25 void Add(int x, int y, int s) {
 26     //if (x > ed || y > ed || x<1||y<1) { cout << "err" << endl; }
 27     //cout << x << " " << y << " " << s << endl;
 28     e[++cnt] = edge(y, head[x], s); head[x] = cnt;
 29     e[++cnt] = edge(x, head[y], 0); head[y] = cnt;
 30 }
 31 void build() {
 32     memset(head, 0, sizeof head);
 33     memset(e, 0, sizeof e);
 34     cnt = 1;
 35     op = p(1, 1, 0) - 1, ed = p(n, m, 1) + 1;
 36     ref(i, 1, n) ref(j, 1, m) {
 37         if ((i & 1) == (j & 1)) {
 38             Add(op, p(i, j, 0), w1[i][j]);
 39             Add(p(i, j, 0), p(i, j, 1), w2[i][j]);
 40             ref(k, 0, 3) {
 41                 int I = i + d[k][0], J = j + d[k][1];
 42                 if (I<1 || J<1 || I>n || J>m)continue;
 43                 Add(p(i, j, 1), p(I, J, 0), inf);
 44             }
 45         }
 46         else {
 47             Add(p(i, j, 0), ed, w1[i][j]);
 48             Add(p(i, j, 1), p(i, j, 0), w2[i][j]);
 49             ref(k, 0, 3) {
 50                 int I = i + d[k][0], J = j + d[k][1];
 51                 if (I<1 || J<1 || I>n || J>m)continue;
 52                 Add(p(I, J, 0), p(i, j, 1), inf);
 53             }
 54         }
 55     }
 56 }
 57 int ans, flow[803], pre[803]; bool vis[803];
 58 void work() {
 59     while (1) {
 60         //cout << ans << endl;
 61         memset(flow, 0, sizeof flow);
 62         memset(pre, 0, sizeof pre);
 63         memset(vis, 0, sizeof vis);
 64         flow[op] = inf;
 65         int o, f;
 66         while (1) {
 67             o = 0; f = 0;
 68             ref(i, op, ed)if (!vis[i] && flow[i] > f)
 69                 f = flow[i], o = i;
 70             if (o == 0 || o == ed)break;
 71             vis[o] = 1;
 72             for (int i = head[o]; i; i = e[i].next) {
 73                 int v = e[i].to; if (vis[v])continue;
 74                 int s = min(f, e[i].s); if (s <= flow[v])continue;
 75                 flow[v] = s; pre[v] = i;
 76                 //if (v > ed) { cout << "err" << endl; }
 77             }
 78         }
 79         if (o == 0)break;
 80         for (; o != op; o = e[pre[o] ^ 1].to) {
 81             e[pre[o]].s -= f; e[pre[o] ^ 1].s += f;
 82         }
 83         ans -= f;
 84     }
 85     //cout << ans << endl;
 86 }
 87 int chd(char c) {
 88     if (c >= '0'&&c <= '9')return c - '0';
 89     if (c >= 'a'&&c <= 'z')return c - 'a' + 10;
 90     if (c >= 'A'&&c <= 'Z')return c - 'A' + 36;
 91 }
 92 class _CLASSNAME_ {
 93 public:
 94     _RC_ _METHODNAME_(_METHODPARMS_) {
 95         memset(w1, 0, sizeof w1);
 96         memset(w2, 0, sizeof w2);
 97         ans = 0;
 98         n = cost.size();
 99         m = cost[0].size();
100         ref(i, 1, n)ref(j, 1, m) {
101             w1[i][j] = chd(cost[i - 1][j - 1]);
102             w2[i][j] = chd(benefit[i - 1][j - 1]);
103             ans += w2[i][j];
104         }
105         build();
106         work();
107         return _RC_(ans);
108     }
109 
110     // BEGIN CUT HERE
111 public:
112     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5();}
113 private:
114     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
115     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
116     void test_case_0() { string Arr0[] = { "21","12" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "21","12" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 4; verify_case(0, Arg2, maxScore(Arg0, Arg1)); }
117     void test_case_1() { string Arr0[] = { "ZZ","ZZ" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "11","11" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(1, Arg2, maxScore(Arg0, Arg1)); }
118     void test_case_2() { string Arr0[] = { "XXX","XXX","XXX" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "aaa","aZa","aaa" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(2, Arg2, maxScore(Arg0, Arg1)); }
119     void test_case_3() { string Arr0[] = { "asam","atik" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "123A","45BC" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 71; verify_case(3, Arg2, maxScore(Arg0, Arg1)); }
120     void test_case_4() {
121         string Arr0[] = { "IIIIIIII",
122             "IIWWWWII",
123             "IIWIIIII",
124             "IIWIIIII",
125             "IIWWWWII",
126             "IIIIIWII",
127             "IIIIIWII",
128             "IIWWWWII",
129             "IIIIIIII" }
130         ; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = { "IIIIIIII",
131             "II0000II",
132             "II0II0II",
133             "II0II0II",
134             "II0000II",
135             "II0II0II",
136             "II0II0II",
137             "II0000II",
138             "IIIIIIII" }
139         ; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 606; verify_case(4, Arg2, maxScore(Arg0, Arg1));
140     }
141     void test_case_5() {
142         string Arr0[] = { "ej8i1lj87i54767cbkla",
143             "7m4l31ehe4c337k9lee6",
144             "4h24Z91ab8a649dacmc4",
145             "0f0631g78g8461jk8i4c",
146             "baf21g6g5a6c75Z8a3ke",
147             "7f957i0ib888ed1k06ga",
148             "j2cb87d949ghkifg3m1i",
149             "fi53Zee4ij8ded796l6b",
150             "k015e95m415gbgkml29m",
151             "aidlk9kmj2lkdbZ4digm",
152             "he67f0clim758be5a69a",
153             "03i54c218elje2kbl5im",
154             "djjjZb36l124012c7fdb",
155             "jhe38eh2i1fi3l061hk6",
156             "216i59211a67g3Z0934c",
157             "db5kbmjm1e3be4l8bml0",
158             "h22ee7gm20j82mmg6eh0",
159             "89aiY4c7a1cg41d1ahck",
160             "lcma6i2171kjk7l9h65l",
161             "lmhif2m96098g6Z84ka3" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0])));
162         string Arr1[] = { "a97e5100a1b45d9ae663", "897696a9c2fc8fe803ea", "03te33c97c81af5b9a26", "3743f85dd2b87b1cd20f", "3dl3a00f706b4217f220", "5a71bdadpf4bc48db593", "02l52900cca45f348dac", "cc3dd16b08c837142ada", "A70e94239c87af935b50", "f0be492c0bda3b0b4062", "050838039967e420cc98", "eed15ec303858d472304", "090b0607f91515ef4537", "cae6ebb4096e73429695", "0cH554009df2a470cd15", "36c976d9096f5c4a4c40", "06690c3b3c585207c46d", "08779dd1a5131a60122e", "D7099feb596918e9324e", "ddd3785103238eac8ef8" };
163         vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0])));
164         int Arg2 = 1125; verify_case(5, Arg2, maxScore(Arg0, Arg1));
165     }
166 
167     // END CUT HERE
168 };
169 // BEGIN CUT HERE
170 int main() {
171     cout << "This is SurroundingGame" << endl;
172     SurroundingGame ___test;
173     ___test.run_test(-1);
174     getchar();
175     return 0;
176 }
177 // END CUT HERE

 

posted @ 2017-11-04 15:01  I_m_Eden  阅读(1063)  评论(0编辑  收藏  举报