Codeforces Round #108 (Div. 2)--State compression Dp+spfa+dfs--garden

Source: Internet
Author: User

Vasya have a very beautiful country garden The can is represented as an n x m rectangular field divi Ded to n· M Squares. One beautiful day Vasya remembered so he needs to pave roads between K Important squares that contain build Ings. To pave a road, he can cover some squares of his garden with concrete.

For each garden square we know number aiJ that represents the number of Flowe Rs. grow in the square with coordinates (i, J). When a square was covered with concrete, all flowers that grow in the square die.

Vasya wants to cover some squares with concrete so, the following conditions were fulfilled:

    • All k important squares should necessarily is covered with concrete
    • From each important square there should is a-a-to-all other important square. The should go is paved with concrete-covered squares Considering that neighboring squares is squares that has a comm On side
    • The total number of dead plants should is minimum

As Vasya have a rather large, he asks you to help him.

Input

The first input line contains three integersN,mandk(1≤N,m≤100,N·m≤200,1≤kmin(N·m, 7))-the Garden ' s sizes and the number of the important squares. Each of the next n lines contains m numbers aiJ (1≤ a iJ ≤1000)-the numbers of flowers in the squares. Next k lines contain coordinates of important squares written as "x y" (WI Thout quotes) (1≤ xn, 1≤ ym). The numbers written on one line is separated by spaces. It is guaranteed this all K Important squares has different coordinates.

Output

In the first line print, the single integer-the minimum number of plants, the die during the road construction. Then print n lines each containing m characters-the Garden ' s plan. In this plan use character "X" (uppercase Latin letter X) to represent a concrete-covered square and use character "." (dot) for a square that isn ' t covered with concrete. If There is multiple solutions, print any of them.

Sample Test (s) input
3 3 2
1 2 3
1 2 3
1 2 3
1 2
3 3
Output
9
. X.
. X.
. Xx
Input
4 5 4
1 4 5) 1 2
2 2 2) 2 7
2 4 1) 4 5
3 2 1) 7 1
1 1
1 5
4 1
4 4
Output
 
X. XX
XXXX.
x.x.
x.xx.
Original blog: http://blog.csdn.net/gzh1992n/article/details/9119543
I don't know if the minimum spanning tree can get out
/* Test instructions: The K point is connected to the minimum weight, and the output diagram order Dp[i][j] is currently the first I point, has been connected to the J state required to connect the minimum number of connected weights so there are two dynamic transfer Equation 1: According to the edge dp[i][j] = min (Dp[k][j] + maze[k][i]) Same state, connection point transfer 2: Based on status dp[i][j] = Min (dp[i][j1] + dp[i][j2]-maze[i/m][i%m]) j1,j2 subset of J DFS Find path */#include <iostream># Include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue > #include <cmath> #include <string> #include <map>using namespace std;const int inf = 1 << 29;co NST int MAX = + 10;int dp[max][1<<7], Pre[max][1<<7];int N, M, K, nn, mm;int hash1[max];int Maz[max][max];     Char g[max][max];bool visit[max][1<<7];int dx[] = {0, 0,-1, 1};int dy[] = {-1, 1, 0, 0};struct Node {int u, st;    Node (int _u, int _st) {u = _u, st = _st;//for type conversion}};queue<node> que;bool check (int x, int y) {    if (x >= 0 && x < n && y >= 0 && y < m) return true; return false;} Update (U, Hash[u], maz[a][b],-1); void update (int u, intSt, int w, int fa) {if (Dp[u][st] > W) {dp[u][st] = W;            PRE[U][ST] = fa;//labeled point if (!visit[u][st]) {Que.push (Node (U, st));        Visit[u][st] = true;    }}} void Dfs (int u, int st) {int x = u/m, y = u% m;    G[x][y] = ' x ';        if (pre[u][st] = =-1) return;//stop else {int v = pre[u][st]/, STT = pre[u][st]% 1000 If the point to mark is reached;        DFS (v, STT);        if (stt-st) Dfs (v, ST-STT);//split Current state}}void solve () {//SPFA while (!que.empty ()) {Node now = Que.front ();        Que.pop ();        int u = now.u, x = now.u/m, y = now.u% m, st = Now.st;            VISIT[U][ST] = false;//splits the state for (int i = 0; i < 4; i++) {int xx = x + dx[i], yy = y + dy[i];            if (!check (xx, yy)) continue;            int v = xx * m + yy;        Update (V, St, dp[u][st] + maz[xx][yy], U * + st);//point dynamic equation} int t = mm-1-st; for (int i = t; i; i = (i-1) & T) {update (U, I|    St, Dp[u][i] + dp[u][st]-maz[x][y], U * + st);//The dynamic equation of the state, because it is the same point to subtract a heavy} int ans = inf, u;            for (int i = 0; i < nn; i++) {if (ans > dp[i][mm-1]) {ans = dp[i][mm-1];        U = i;    }}//find the minimum Value DFS (U, mm-1);//dfs find path cout << ans << endl;        for (int i = 0, i < n; i++) {for (int j = 0; J < m; j + +) cout << g[i][j];    cout << Endl;        }}int Main () {while (CIN >> n >> m >> k) {while (!que.empty ()) Que.pop ();                for (int i = 0, i < n; i++) {for (int j = 0; J < m; J + +) {cin >> maz[i][j]; G[I][J] = '. ';        /init diagram}} NN = n * m, mm = 1 << k;//the graph is memset (hash1, 0, sizeof (HASH1));        Memset (visit, false, sizeof (visit));        for (int i = 0; i < nn; i++) for (int j = 0; J < mm; J + +) Dp[i][j] = inf;//The number of the total points, the number of points that need to be connected For(int i = 0, a, b; i < K; i++)            {cin >> a >> b;            a--, b--;            int u = A * m + b;        Hash1[u] = 1 << i;//record status update (U, Hash1[u], maz[a][b],-1);    } solve (); } return 0;}

  

Codeforces Round #108 (Div. 2)--State compression Dp+spfa+dfs--garden

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.