AT_abc210_d [ABC210D] National Railway Solution

AT_abc210_d [ABC210D] National Railway Solution

Luogu Portal

AT Portal

Topic to the effect

gives you an n × mn\times mn×For a location of m size, you need to choose two locations to build a station, and build a track between them, asking for the minimum cost.

train of thought

According to the meaning of the question, in fact, the cost of establishing orbits is equivalent to their Manhattan distance × c \times c× c , so if we have built a station, it means that I can walk to the next location and build another station, so we can first record the minimum cost of the first half. We can usedp [ i ] [ j ] dp[i][j]d p ​​[ i ] [ j ] to indicate that a subway station has been built and has reached( i , j ) (i,j)(i,j ) , in order to consider comprehensively, the directions we traverse are actually the northeast and southeast directions. So we have to run twice. For the southeast direction, the state transition equation is easy to know:dp [ i ] [ j ] = { aij , dp [ i − 1 ] [ j ] + c , dp [ i ] [ j − 1 ] + c } dp[i][j] = \{a_{ij},dp[i-1][j]+c,dp[i][j-1]+c\}dp[i][j]={ aij,dp[i1][j]+c,dp[i][j1]+c } Then we deal withdp dpAfter the dp array, there is another point is how to determine where the last subway station is best, which is obvious, we directly traversedp dpThe d p array can then enumerate the subway stations we have built.

the code

#include <bits/stdc++.h>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
    
    
	inline int read() {
    
    
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
    
    
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
    
    
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
const ll INF = 0x3f3f3f3f3f3f3f;
int n, m, c;
ll a[1005][1005], dp[1005][1005], ans[1005][1005];
int main() {
    
    
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
    n = read(), m = read(), c = read();
    for(int i = 1; i <= n; i ++) {
    
    
        for(int j = 1; j <= m; j ++) {
    
    
            scanf("%lld", &a[i][j]);
        }
    }
    fill(dp[0], dp[0] + 1005 * 1005, INF);
    fill(ans[0], ans[0] + 1005 * 1005, INF);
    for(int i = 1; i <= n; i ++) {
    
    
        for(int j = 1; j <= m; j ++) {
    
    
            dp[i][j] = min(a[i][j], min(dp[i - 1][j], dp[i][j - 1]) + c);
        }
    }
    ll minn = INF;
    for(int i = 1; i <= n; i ++) {
    
    
        for(int j = 1; j <= m; j ++) {
    
    
            ans[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + c + a[i][j];
            minn = min(ans[i][j], minn);
        }
    }
    for(int i = n; i >= 1; i --) {
    
    
        for(int j = 1; j <= m; j ++) {
    
    
            dp[i][j] = min(a[i][j], min(dp[i + 1][j], dp[i][j - 1]) + c);
        }
    }
    for(int i = 1; i <= n; i ++) {
    
    
        for(int j = 1; j <= m; j ++) {
    
    
            ans[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + c + a[i][j];
            minn = min(ans[i][j], minn);
        }
    }
    printf("%lld\n", minn);
	return 0;
}

Guess you like

Origin blog.csdn.net/ZH_qaq/article/details/130458137