Submission #34029832
Source Code Expand
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
typedef pair<ll,ll> P;
#define SORT(a) sort((a).begin(),(a).end())
#define REV(a) reverse((a).begin(),(a).end())
#define For(i, a, b) for(ll i = (a) ; i < (b) ; ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
void coY() {cout <<"Yes"<<endl;}
void coN(){cout <<"No"<<endl;}
void coT() {cout <<"Takahashi"<<endl;}
void coA(){cout <<"Aoki"<<endl;}
void mswap(ll &a, ll &b){ if(a >= b) swap(a,b); }
void rswap(ll &a, ll &b){ if(a <= b) swap(a,b); }
const ll dy[] = {0,0,1,-1};
const ll dx[] = {1,-1,0,0};
const ll mod = 1e9+7;
const ll MOD = 998244353;
using mint = modint998244353;
const double PI=3.14159265358979323846;
const ll inf = 1001001001;
const ll INF = 1'000'000'000'000'000'000;
vector<ll> to[200'005];
//Write From this Line
ll dp[2010][2010][2][2];
int main()
{
int h, w;
cin >> h>> w;
vector<ll> R(h);
rep(i,h) cin >> R[i];
vector<ll> C(w);
rep(i,w) cin >> C[i];
vector<vector<int>> S(h+5,vector<int>(w+5,0));
rep(i,h){
string s;
cin >> s;
rep(j,w){
if(s[j] == '1') S[i][j] = 1;
}
}
// dp[i][j]:= min_cost to (i,j)
rep(i,2010)rep(j,2010)rep(x,2)rep(y,w)dp[i][j][x][y] = INF;
//dp[i][j][x][y]:= (i,j)
dp[0][0][0][0] = 0;
dp[0][0][1][1] = R[0] + C[0];
int now = S[0][0];
rep(i,h){
rep(j,w){
bool same = (now == S[i][j+1]);
bool same2 = (now == S[i+1][j]);
rep(x,2){
rep(y,2){
// dp[i][j][x][y]からの繊維
if((same && x) || (!same && !x)){
chmin(dp[i][j+1][x][1], dp[i][j][x][y] + C[j+1]);
} else {
chmin(dp[i][j+1][x][0], dp[i][j][x][y]);
}
if((same2 && y) || (!same2 && !y)){
chmin(dp[i+1][j][1][y], dp[i][j][x][y] + R[i+1]);
}else {
chmin(dp[i+1][j][0][y], dp[i][j][x][y] );
}
}
}
}
}
ll ans = INF;
rep(x,2)rep(y,2){
chmin(ans,dp[h-1][w-1][x][y]);
}
rep(i,2010)rep(j,2010)rep(x,2)rep(y,w)dp[i][j][x][y] = INF;
//dp[i][j][x][y]:= (i,j)
dp[0][0][0][1] = C[0];
dp[0][0][1][0] = R[0];
now = 1-now;
rep(i,h){
rep(j,w){
bool same = (now == S[i][j+1]);
bool same2 = (now == S[i+1][j]);
rep(x,2){
rep(y,2){
// dp[i][j][x][y]からの繊維
if((same && x) || (!same && !x)){
chmin(dp[i][j+1][x][1], dp[i][j][x][y] + C[j+1]);
} else {
chmin(dp[i][j+1][x][0], dp[i][j][x][y]);
}
if((same2 && y) || (!same2 && !y)){
chmin(dp[i+1][j][1][y], dp[i][j][x][y] + R[i+1]);
}else {
chmin(dp[i+1][j][0][y], dp[i][j][x][y] );
}
}
}
}
}
rep(x,2)rep(y,2){
chmin(ans,dp[h-1][w-1][x][y]);
}
cout << ans << endl;
}
Submission Info
Submission Time |
|
Task |
F - Monochromatic Path |
User |
nibosea |
Language |
C++ (GCC 9.2.1) |
Score |
500 |
Code Size |
3002 Byte |
Status |
AC |
Exec Time |
355 ms |
Memory |
150096 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
500 / 500 |
Status |
|
|
Set Name |
Test Cases |
Sample |
example0.txt, example1.txt |
All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, example0.txt, example1.txt |
Case Name |
Status |
Exec Time |
Memory |
000.txt |
AC |
100 ms |
134536 KB |
001.txt |
AC |
96 ms |
134632 KB |
002.txt |
AC |
250 ms |
149916 KB |
003.txt |
AC |
252 ms |
150092 KB |
004.txt |
AC |
253 ms |
149936 KB |
005.txt |
AC |
250 ms |
150008 KB |
006.txt |
AC |
352 ms |
150004 KB |
007.txt |
AC |
353 ms |
149920 KB |
008.txt |
AC |
355 ms |
150004 KB |
009.txt |
AC |
352 ms |
150028 KB |
010.txt |
AC |
350 ms |
149920 KB |
011.txt |
AC |
353 ms |
150096 KB |
012.txt |
AC |
340 ms |
150008 KB |
013.txt |
AC |
125 ms |
136364 KB |
014.txt |
AC |
155 ms |
138232 KB |
015.txt |
AC |
170 ms |
138904 KB |
016.txt |
AC |
276 ms |
145708 KB |
017.txt |
AC |
107 ms |
135108 KB |
018.txt |
AC |
162 ms |
138776 KB |
019.txt |
AC |
222 ms |
141856 KB |
020.txt |
AC |
183 ms |
139476 KB |
021.txt |
AC |
140 ms |
136792 KB |
022.txt |
AC |
140 ms |
136772 KB |
023.txt |
AC |
353 ms |
149996 KB |
024.txt |
AC |
354 ms |
150008 KB |
025.txt |
AC |
355 ms |
149992 KB |
026.txt |
AC |
352 ms |
149924 KB |
027.txt |
AC |
352 ms |
149916 KB |
028.txt |
AC |
351 ms |
149976 KB |
029.txt |
AC |
354 ms |
149924 KB |
030.txt |
AC |
353 ms |
149992 KB |
031.txt |
AC |
352 ms |
149976 KB |
032.txt |
AC |
352 ms |
150008 KB |
033.txt |
AC |
353 ms |
149996 KB |
034.txt |
AC |
352 ms |
150096 KB |
035.txt |
AC |
351 ms |
150004 KB |
036.txt |
AC |
352 ms |
150032 KB |
037.txt |
AC |
352 ms |
150036 KB |
038.txt |
AC |
350 ms |
149928 KB |
039.txt |
AC |
353 ms |
149936 KB |
040.txt |
AC |
350 ms |
149992 KB |
041.txt |
AC |
349 ms |
150036 KB |
042.txt |
AC |
350 ms |
149976 KB |
example0.txt |
AC |
94 ms |
134612 KB |
example1.txt |
AC |
100 ms |
134536 KB |