QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#339959#7737. Extending DistanceSlongodWA 0ms9760kbC++145.4kb2024-02-28 09:52:002024-02-28 09:52:00

Judging History

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

  • [2024-02-28 09:52:00]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:9760kb
  • [2024-02-28 09:52:00]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
namespace Slongod{
const int N = 1005 , M = 2e4+7 , inf = 0x3f3f3f3f;
int n , m , econt , s , t , S , T , k;
int head[N] , hu[N] , r[N][N] , d[N][N] , redgeid[N][N][2] , dedgeid[N][N][2];
struct EDGE{int to , nxt , w , c;}edge[M];
inline void _add(int x , int y , int w , int c){edge[++econt].to = y; edge[econt].nxt = head[x]; head[x] = econt; edge[econt].w = w; edge[econt].c = c;}
inline void add(int x , int y , int w , int c){_add(x , y , w , c); _add(y , x , 0 , -c);}
inline int id(int x , int y){return (x - 1) * (m + 1) + y;}
void init()
{
    econt = 1; S = id(n - 1 , m + 1) + 1; T = S + 1; t = T + 1; s = 0;
    for (int i = s; i <= t; i++){head[i] = 0;}
}

namespace Dinic
{
    int s , t , dep[N];
    bool bfs()
    {
        for (int i = 0; i <= t; i++){dep[i] = -1; hu[i] = head[i];}
        queue <int> q; q.push(s); dep[s] = 0;
        while(!q.empty()) {
            int u = q.front(); q.pop();
            for (int i = head[u]; i; i = edge[i].nxt) {
                int v = edge[i].to , w = edge[i].w;
                if (w and dep[v] == -1) {
                    dep[v] = dep[u] + 1;
                    q.push(v);
                }
            }
        } return dep[t] != -1;
    }
    int dfs(int u , int f)
    {
        if (u == t){return f;} int used = 0;
        for (int &i = hu[u]; i; i = edge[i].nxt) {
            int v = edge[i].to , w = edge[i].w;
            if (dep[v] == dep[u] + 1 and w) {
                int tmp = dfs(v , min(f-used , w));
                edge[i].w -= tmp; edge[i^1].w += tmp; used += tmp;
                if (f == used){break;}
            }
        } if (!used){dep[u] = -1;} return used;
    }
    int dinic(int s , int t){Dinic::s = s; Dinic::t = t; int re = 0; while(bfs()){re += dfs(s , inf);} return re;}
}
namespace CDinic
{
    int s , t , dis[N]; bool vis[N];
    bool spfa()
    {
        for (int i = 0; i <= t; i++){dis[i] = inf; hu[i] = head[i];}
        queue <int> q; q.push(s); dis[s] = 0;
        while(!q.empty()) {
            int u = q.front(); q.pop(); vis[u] = 0;
            for (int i = head[u]; i; i = edge[i].nxt) {
                int v = edge[i].to , w = edge[i].w , c = edge[i].c;
                if (w and dis[v] > dis[u] + c) {
                    dis[v] = dis[u] + c;
                    if (!vis[v]) {
                        q.push(v); vis[v] = 1;
                    }
                }
            }
        } return dis[t] != inf;
    }
    int dfs(int u , int f)
    {
        if (u == t){return f;} int used = 0; vis[u] = 1;
        for (int &i = hu[u]; i; i = edge[i].nxt) {
            int v = edge[i].to , w = edge[i].w , c = edge[i].c;
            if (dis[v] == dis[u] + c and w and !vis[v]) {
                int tmp = dfs(v , min(f-used , w));
                edge[i].w -= tmp; edge[i^1].w += tmp; used += tmp;
                if (f == used){break;}
            }
        } if (!used){dis[u] = inf;} vis[u] = 0; return used;
    }
    int dinic(int s , int t){CDinic::s = s; CDinic::t = t; int re = 0; while(spfa()){re += dfs(s , inf) * dis[t];} return re;}
}

void main()
{
    cin >> n >> m >> k; init();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < m; j++) {
            cin >> r[i][j];
        }
    }
    for (int i = 1; i < n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> d[i][j];
        }
    }
    for (int i = 2; i <= m; i++) {
        add(S , id(n - 1 , i) , r[n][i-1] , 0);
        add(id(1 , i) , T , r[1][i-1] , 0);
    }
    for (int i = 1; i < n; i++) {
        for (int j = 2; j < m; j++) {
            add(id(i , j) , id(i , j + 1) , d[i][j] , 0);
            add(id(i , j + 1) , id(i , j) , d[i][j] , 0);
        }
    }
    for (int i = 1; i < n - 1; i++) {
        for (int j = 2; j <= m; j++) {
            add(id(i + 1 , j) , id(i , j) , r[i+1][j-1] , 0);
            add(id(i , j) , id(i + 1 , j) , r[i+1][j-1] , 0);
        }
    }
    Dinic :: dinic(S , T);
    for (int i = 2; i <= m; i++) {
        add(S , id(n - 1 , i) , inf , 1);
        redgeid[n][i-1][0] = econt;
        redgeid[n][i-1][1] = 0;
        add(id(1 , i) , T , inf , 1);
        redgeid[1][i-1][0] = econt;
        redgeid[1][i-1][0] = 0;
    }
    for (int i = 1; i < n; i++) {
        for (int j = 1; j <= m; j++) {
            add(id(i , j) , id(i , j + 1) , inf , 1);
            dedgeid[i][j][0] = econt;
            add(id(i , j + 1) , id(i , j) , inf , 1);
            dedgeid[i][j][1] = econt;
        }
    }
    for (int i = 1; i < n - 1; i++) {
        for (int j = 2; j <= m; j++) {
            add(id(i + 1 , j) , id(i , j) , inf , 1);
            redgeid[i+1][j-1][0] = econt;
            add(id(i , j) , id(i + 1 , j) , inf , 1);
            redgeid[i+1][j-1][1] = econt;
        }
    }
    add(s , S , k , 0); add(T , t , k , 0);
    cout << CDinic::dinic(s , t) << '\n';
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < m; j++) {
            cout << r[i][j] + edge[redgeid[i][j][0]].w + edge[redgeid[i][j][1]].w << ' ';
        } cout << '\n';
    }
    for (int i = 1; i < n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << d[i][j] + edge[dedgeid[i][j][0]].w + edge[dedgeid[i][j][1]].w << ' ';
        } cout << '\n';
    }
}
}int main()
{
    ios :: sync_with_stdio(0);
    cin.tie(0) , cout.tie(0);
    int T; cin >> T;
    while(T--){Slongod :: main();}
    return 0;
}

詳細信息

Test #1:

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

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
2 1 15 
7 1 12 
13 3 6 
3 6 1 2 
5 2 15 3 
4
1 1 
3 2 
3 3 
1 1 1 
2 2 2 

result:

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