atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
思路:
首先考虑暴力的做法,枚举两个点计算距离后取最小值,复杂度\(O(n^{4})\)
考虑怎么优化。
首先可以确定的是,一定要枚举一个点,那么可以通过控制坐标的大小关系将绝对值去掉。
假设对于点\((i,j)\),只考虑点\((x,y)\)满足\(i<=x<=n,j<=y<=m\)
那么代价就变成了$$a[i][j]+a[x][y]+c(x-i)+c(y-j)$$
拆开得到$$a[i][j]-ci-cj+a[x][j]+cx+cy$$
对于点\((i,j)\)的贡献\(a[i][j]-c*i-c*j\),可以通过枚举计算;
对于点\((x,y)\)的贡献\(a[x][j]+c*x+c*y\),由于题目要求代价最小,所以应当维护最小值。
也就是说\(dp[i][j]\)表示的是对于满足\(i<=x<=n,j<=y<=m\)的所有的点\((x,y)\)的贡献\(a[x][j]+c*x+c*y\)的最小值。
所以每次枚举到\((i,j)\)时,只需要假定这个点必选,再加上\(dp[i][j]\)表示上一个点的贡献,所有的答案取最小值即可,注意更新完答案后应当更新\(dp\)数组。
这样会产生的一个问题就是,只是考虑了两个点的位置大致是呈左上-右下的趋势的情况,如果一个点在右上,另一个点在左下,这种情况没有被枚举到。
所以还应该再跑一遍,基本流程同上文类似。
代码:
又臭又长的垃圾代码:(下一个是比较简洁的代码!)
ll n,m,c,a[1100][1100],dp[1100][1100],dp1[1100][1100];
int main(){
n=read,m=read,c=read;
rep(i,1,n) rep(j,1,m) a[i][j]=read;
rep(i,0,n+1) rep(j,0,m+1) dp[i][j]=1e18,dp1[i][j]=1e18;
ll res=1e18;
for(int i=n;i;i--)
for(int j=m;j;j--){
ll now=a[i][j]-c*i-c*j;
ll las=1e18;
if(i+1<=n&&j+1<=m) las=min(las,dp[i+1][j+1]);
if(i+1<=n) las=min(las,dp[i+1][j]);
if(j+1<=m) las=min(las,dp[i][j+1]);
res=min(res,now+las);
dp[i][j]=min(dp[i][j],a[i][j]+c*i+c*j);
if(i+1<=n&&j+1<=m) dp[i][j]=min(dp[i][j],dp[i+1][j+1]);
if(i+1<=n) dp[i][j]=min(dp[i][j],dp[i+1][j]);
if(j+1<=m) dp[i][j]=min(dp[i][j],dp[i][j+1]);
}
for(int i=1;i<=n;i++){
for(int j=m;j;j--){
ll now=a[i][j]+c*i-c*j;
ll las=1e18;
if(i-1>=1&&j+1<=m) las=min(las,dp1[i-1][j+1]);
if(i-1>=1) las=min(las,dp1[i-1][j]);
if(j+1<=m) las=min(las,dp1[i][j+1]);
res=min(res,now+las);
dp1[i][j]=min(dp1[i][j],a[i][j]-c*i+c*j);
if(i-1>=1&&j+1<=m) dp1[i][j]=min(dp1[i][j],dp1[i-1][j+1]);
if(i-1>=1) dp1[i][j]=min(dp1[i][j],dp1[i-1][j]);
if(j+1<=m) dp1[i][j]=min(dp1[i][j],dp1[i][j+1]);
}
}
printf("%lld\n",res);
return 0;
}
跟我的思路类似但是代码超级简洁的巨巨代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#define endl '\n'
#define int long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
int n,m,C;
int a[1010][1010];
int dp[1010][1010];
int ans=0x3f3f3f3f3f3f3f3f;
void DP(){
memset(dp,0x3f,sizeof dp);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=min(dp[i][j-1],dp[i-1][j]);
ans=min(ans,dp[i][j]+a[i][j]+C*(i+j));
dp[i][j]=min(dp[i][j],a[i][j]-C*(i+j));
}
}
}
signed main(){
cin>>n>>m>>C;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
DP();
for(int i=1;i<=n;i++){
int l=1,r=m;
while(l<r){
swap(a[i][l],a[i][r]);
l++,r--;
}
}
DP();
cout<<ans<<endl;
return 0;
}
赛时写假了边界,宇巨一波奇技淫巧强行AC的代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void out(ll x){
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
inline void write(ll x){
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
puts("");
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
ll res = 1;
while(b)
{
if(b & 1)res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
const int inf = 0x3f3f3f3f;
#define PI acos(-1)
const int maxn=4e5+100;
ll n,m,c,a[1100][1100],dp[1100][1100],dp1[1100][1100];
struct Node{
ll x,y,val;
}t[1000009];
bool cmp(Node x,Node y)
{
return x.val<y.val;
}
int main(){
n=read,m=read,c=read;
ll cnt=0;
rep(i,1,n) rep(j,1,m) {
a[i][j]=read,dp[i][j]=1e18,dp1[i][j]=1e18;
t[++cnt] = {i,j,a[i][j]};
}
if(n*m<=30000)
{
ll anss = 1e18;
sort(t+1,t+1+cnt,cmp);
rep(i,1,min(n*m,30000ll)) rep(j,1,min(n*m,30000ll))
{
if(i==j) continue;
ll x1,y1,x2,y2,val1,val2;
x1 = t[i].x,y1 = t[i].y,val1 = t[i].val;
x2 = t[j].x,y2 = t[j].y,val2 = t[j].val;
ll temp = c*(abs(x1-x2)+abs(y1-y2)) +val1+val2;
anss = min(anss,temp);
}
printf("%lld",anss);
return 0;
}
ll res=1e18;
for(int i=n;i;i--)
for(int j=m;j;j--){
if(i==n&&j==m){
dp[i][j]=a[i][j]+c*i+c*j;
continue;
}
ll now=a[i][j]-c*i-c*j;
ll las=1e18;
if(i+1<=n&&j+1<=m) las=min(las,dp[i+1][j+1]);
if(i+1<=n) las=min(las,dp[i+1][j]);
if(j+1<=m) las=min(las,dp[i][j+1]);
res=min(res,now+las);
dp[i][j]=min(dp[i][j],a[i][j]+c*i+c*j);
if(i+1<=n&&j+1<=m) dp[i][j]=min(dp[i][j],dp[i+1][j+1]);
if(i+1<=n) dp[i][j]=min(dp[i][j],dp[i+1][j]);
if(j+1<=m) dp[i][j]=min(dp[i][j],dp[i][j+1]);
}
for(int i=1;i<=n;i++){
for(int j=m;j;j--){
if(i==1&&j==m){
dp1[i][j]=a[i][j]+c*i+c*j;
continue;
}
ll now=a[i][j]+c*i-c*j;
ll las=1e18;
if(i-1>=1&&j+1<=m) las=min(las,dp1[i-1][j+1]);
if(i-1>=1) las=min(las,dp1[i-1][j]);
if(j+1<=m) las=min(las,dp1[i][j+1]);
res=min(res,now+las);
dp1[i][j]=min(dp1[i][j],a[i][j]-c*i+c*j);
if(i-1>=1&&j+1<=m) dp1[i][j]=min(dp1[i][j],dp1[i-1][j+1]);
if(i-1>=1) dp1[i][j]=min(dp1[i][j],dp1[i-1][j]);
if(j+1<=m) dp1[i][j]=min(dp1[i][j],dp1[i][j+1]);
}
}
printf("%lld\n",res);
return 0;
}
/*
-4999000000****1061109567****3****2
-4999000000****1061109567****2****3
-3937890433
2001000000 3001000000 4000000001
3001000000 4001000000 5001000000
4000000001 5001000000 6001000000
*/