이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 3e3 + 1;
const int inf = 0x3f3f3f3f;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int n, m, k;
int s, t;
int d[maxN * maxN];
struct TEdge{
int u, v, c, w;
TEdge(){}
TEdge(int _u, int _v, int _c, int _w){
u = _u;
v = _v;
c = _c;
w = _w;
}
};
vector <int> adj[maxN * maxN];
vector <TEdge> S;
int a[maxN][maxN];
bool inqueue[maxN * maxN];
int trace[maxN * maxN];
inline int conv(int i, int j){
return (i - 1) * m + j;
}
inline void add(int u, int v, int w){
adj[u].pb(S.size());
S.pb({u, v, 1, w});
adj[v].pb(S.size());
S.pb({v, u, 0, -w});
}
void SPFA(){
fill(d, d + n * m + 2, inf);
d[0] = 0;
queue <int> Q;
Q.push(0);
while (!Q.empty()){
int u = Q.front();
inqueue[u] = 0;
Q.pop();
for (int i: adj[u]){
int c = S[i].c, v = S[i].v, w = S[i].w;
if (c && d[v] > d[u] + w){
d[v] = d[u] + w;
trace[v] = i;
if (!inqueue[v]){
inqueue[v] = 1;
Q.push(v);
}
}
}
}
}
void Init(){
cin >> n >> k;
m = n;
s = 0, t = n * m + 1;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j) cin >> a[i][j];
}
ll res = 0;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
res += a[i][j];
if ((i + j) % 2){
add(s, conv(i, j), 0);
for (int k = 0; k < 4; ++k){
int x = i + dx[k];
int y = j + dy[k];
if (x < 1 || x > n || y < 1 || y > m) continue;
add(conv(i, j), conv(x, y), -a[i][j] - a[x][y]);
}
}
else add(conv(i, j), t, 0);
}
}
while (k--){
SPFA();
int v = t;
res += d[v];
while (v){
int i = trace[v];
S[i].c ^= 1;
S[i ^ 1].c ^= 1;
v = S[i].u;
}
}
cout << res;
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
컴파일 시 표준 에러 (stderr) 메시지
domino.cpp: In function 'int main()':
domino.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
domino.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |