QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#427421#7737. Extending DistancezlxFTHWA 0ms13964kbC++144.9kb2024-06-01 13:16:322024-06-01 13:16:33

Judging History

你现在查看的是最新测评结果

  • [2024-06-01 13:16:33]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:13964kb
  • [2024-06-01 13:16:32]
  • 提交

answer

#include <algorithm>
#include <cassert>
#include <cstdio>

#define debug(...) fprintf(stderr, __VA_ARGS__), \
    fflush(stderr)

using LL = long long;
const int N = 505 + 5;
const int NN = 505 * 505 + 5;
const LL InfLL = 1e18;

int tot = 1, head[NN], cur[NN], dep[NN], pre[NN];
LL dis[NN], flow[NN];
struct Edge {
  int nxt, v;
  LL w, c;
} e[NN * 20];
int s, s1, t;
inline void addEdge(int u, int v, LL w, LL c) {
  e[++tot] = {head[u], v, w, c};
  head[u] = tot; 
}
inline void add(int u, int v, LL w, LL c) {
  if (v == s || u == t) return;
  // debug("%d %d %lld %lld\n", u, v, w, c);
  addEdge(u, v, w, c);
  addEdge(v, u, 0, -c);
}
inline bool bfs() {
  for (int i = 1; i <= t; ++i) dep[i] = 0;
  static int q[NN];
  int qh = 1, qt = 0;
  q[++qt] = s;
  dep[s] = 1;
  while (qh <= qt) {
    int u = q[qh++];
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].v;
      LL w = e[i].w;
      if (w && !dep[v]) {
        dep[v] = dep[u] + 1;
        q[++qt] = v; 
      }
    }
  }
  return dep[t];
}
LL dfs(int u, LL in) {
  if (u == t) return in;
  LL out = 0;
  for (int &i = cur[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    LL w = e[i].w;
    if (dep[v] == dep[u] + 1 && w) {
      LL res = dfs(v, std::min(in - out, w));
      e[i].w -= res;
      e[i ^ 1].w += res;
      out += res;
      if (in == out) return in;
    }
  }
  if (!out) dep[u] = 0;
  return out;
}
inline bool spfa() {
  static int vis[NN];
  for (int i = 1; i <= t; ++i) {
    dis[i] = InfLL;
    vis[i] = 0;
    flow[i] = 0;
  }
  static int q[NN];
  int qh = 1, qt = 0;
  q[++qt] = s, vis[s] = 1, dis[s] = 0, flow[s] = InfLL;
  while (qh <= qt) {
    int u = q[qh++];
    vis[u] = 0;
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].v;
      LL w = e[i].w, c = e[i].c;
      if (dis[v] > dis[u] + c && w) {
        dis[v] = dis[u] + c;
        pre[v] = i;
        flow[v] = std::min(flow[u], w);
        if (!vis[v]) {
          vis[v] = 1;
          q[++qt] = v;
        }
      }
    }
  }
  return flow[t];
}
inline LL ek() {
  LL res = 0, mf = flow[t];
  // debug("%lld\n", mf);
  for (int u = t; u != s;) {
    int i = pre[u];
    res += mf * e[i].c;
    e[i].w -= mf;
    e[i ^ 1].w += mf;
    u = e[i ^ 1].v;
  }
  return res;
}

int n, m, K;
LL a[N][N], b[N][N];

inline int id(int i, int j) {
  assert(i <= n - 1 && j <= m - 1 && i > 0 && j > 0);
  return (i - 1) * (m - 1) + j;
}

void solve() {
  scanf("%d%d%d", &n, &m, &K);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < m; ++j)
      scanf("%lld", &a[i][j + 1]);
  for (int i = 1; i < n; ++i)
    for (int j = 1; j <= m; ++j)
      scanf("%lld", &b[i][j + 1]);
  m += 2;
  s1 = (n - 1) * (m - 1) + 1, s = s1 + 1, t = s + 1;
  add(s, s1, InfLL, 0);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < m; ++j) {
      int u = (i == 1 ? s1 : id(i - 1, j));
      int v = (i == n ? t : id(i, j));
      add(u, v, a[i][j], 0);
      add(v, u, a[i][j], 0);
    }
  }
  for (int i = 1; i < n; ++i) {
    for (int j = 1; j <= m; ++j) {
      int u = (j == m ? s1 : id(i, j));
      int v = (j == 1 ? t : id(i, j - 1));
      add(u, v, b[i][j], 0);
      add(v, u, b[i][j], 0);
    }
  }
  LL mf = 0;
  while (bfs()) {
    for (int u = 1; u <= t; ++u) cur[u] = head[u];
    mf += dfs(s, InfLL);
  }
  // debug("shortest path: %lld\n", mf);
  e[2].w = 0;
  add(s, s1, K, 0);
  // e[2].w = K, e[3].w = 0;
  int bck = tot;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < m; ++j) {
      int u = (i == 1 ? s1 : id(i - 1, j));
      int v = (i == n ? t : id(i, j));
      if (j == 1 || j == m - 1) continue;
      add(u, v, K, 1);
      add(v, u, K, 1);
    }
  }
  for (int i = 1; i < n; ++i) {
    for (int j = 1; j <= m; ++j) {
      int u = (j == m ? s1 : id(i, j));
      int v = (j == 1 ? t : id(i, j - 1));
      if (j == 1 || j == m) continue;
      add(u, v, K, 1);
      add(v, u, K, 1);
    }
  }
  LL ans = 0;
  while (spfa()) ans += ek();
  printf("%lld\n", ans);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < m; ++j) {
      int u = (i == 1 ? s1 : id(i - 1, j));
      int v = (i == n ? t : id(i, j));
      if (j == 1 || j == m - 1) continue;
      printf("%lld%c", a[i][j] + e[bck + 2].w + e[bck + 4].w, " \n"[j == m - 2]);
      bck += 4;
    }
  }
  for (int i = 1; i < n; ++i) {
    for (int j = 1; j <= m; ++j) {
      int u = (j == m ? s1 : id(i, j));
      int v = (j == 1 ? t : id(i, j - 1));
      if (j == 1 || j == m) continue;
      printf("%lld%c", b[i][j] + e[bck + 2].w + e[bck + 4].w, " \n"[j == m - 1]);
      bck += 4;
    }
  }
  for (int i = 1; i <= t; ++i) head[i] = 0;
  tot = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) a[i][j] = b[i][j] = 0;
}

int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 13964kb

input:

2
3 4 6
2 1 15
7 1 9
13 3 2
3 6 1 2
5 2 15 3
3 3 3
1 1
2 2
3 3
1 1 1
2 2 2

output:

9
4 1 15
7 1 12
13 7 2
3 6 1 2
5 2 15 3
4
2 3
2 3
3 3
1 1 1
2 2 6

result:

wrong answer The output T doesn't match number of operations. (test case 2)