This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int dx[4] = { 0, -1, 0, 1 };
const int dy[4] = { -1, 0, 1, 0 };
int m, n; // pocet riadkov, pocet stlpcov
vector<vector<int> > w;
vector<vector<bool> > ce, vis;
bool valid(int x, int y)
{
return x >= 0 && y >= 0 && x < m && y < n;
}
void dfs(int x, int y, int& c, int& f, vector<int> &v)
{
if (ce[x][y]) c++;
else f++, v.push_back(w[x][y]);
vis[x][y] = true;
for (int i = 0; i < 4; i++)
{
int xi = x + dx[i], yi = y + dy[i];
if (valid(xi, yi) && !vis[xi][yi] && (ce[x][y] || ce[xi][yi]))
dfs(xi, yi, c, f, v);
}
}
void no()
{
cout << "No\n"; exit(0);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> m >> n;
w.assign(m, vector<int>(n, 0)), ce.assign(m, vector<bool>(n, false)), vis.assign(m, vector<bool>(n, false));
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) cin >> w[i][j];
int k;
cin >> k;
int ans = 0;
for (int i = 0, r, c; i < k; i++) cin >> r >> c, ce[r][c] = true, ans += w[r][c];
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (ce[i][j] && !vis[i][j])
{
int c = 0, f = 0; vector<int> v;
dfs(i, j, c, f, v);
if (c * 3 > f) no();
swap(v.back(), *min_element(v.begin(), v.end()));
for (int i = 0; i < c * 3; i++) ans += v[i];
}
cout << ans << "\n";
return 0;
}
# | 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... |