結果

問題 No.3334 I hate Image Convolution
コンテスト
ユーザー shinchan
提出日時 2025-11-07 22:56:15
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 194 ms / 3,000 ms
コード長 3,789 bytes
コンパイル時間 2,399 ms
コンパイル使用メモリ 215,932 KB
実行使用メモリ 49,628 KB
最終ジャッジ日時 2025-11-07 22:56:35
合計ジャッジ時間 19,674 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;



#define all(v) (v).begin(),(v).end()
#define pb emplace_back
#define rep(i, n) for(int i=0;i<(n);i++)
#define foa(e, v) for(auto& e : v)
#define dout(a) cout<<fixed<<setprecision(10)<<a<<'\n';
#define Cout(a) cout<<a<<'\n';

using ll = long long;
using ld = long double;
using Int = __int128;
template <class T> using pqr = priority_queue<T, vector<T>, greater<T>>;

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
    bool compare = a < b;
    if(compare) a = b;
    return compare;
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
    bool compare = a > b;
    if(compare) a = b;
    return compare;
}
template <typename T> inline T back(std::set<T> &s) {
    return *s.rbegin();
}
template <typename T> inline T back(std::multiset<T> &s) {
    return *s.rbegin();
}
template <typename T> inline T pop_back(std::set<T> &s) {
    auto it = prev(s.end());
    T val = *it;
    s.erase(it); 
    return val;
}
template <typename T> inline T pop_back(std::multiset<T> &s) {
    auto it = prev(s.end());
    T val = *it;
    s.erase(it); 
    return val;
}

const int dy[8] = {-1, 0, 0, 1, 1, -1, 1, -1};
const int dx[8] = {0, -1, 1, 0, -1, -1, 1, 1};

const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (3LL << 59);
const int inf = 1 << 30;
const char br = '\n';



// depends on dijkstra
vector<ll> USHI(ll n, vector<tuple<ll, ll, ll>> edges, ll s) {
    // v : x_i - x_j <= M
    vector<vector<pair<ll, ll>>> G(n);
    for(auto [i, j, M] : edges) {
        G[j].push_back({M, i});
    }
    vector<ll> dist(n, INF);
    dist[s] = 0;

    bool update = 1;
    bool ng = 0;
    rep(_, n) {
        update = 0;
        rep(i, n) {
            for(auto [d, node]: G[i]) {
                if(dist[i] + d < dist[node]) {
                    dist[node] = dist[i] + d;
                    update = 1;
                }
            }
        }
        if(!update) break;
        else if(_ == n - 1) ng = 1;
    }
    if(ng) {
        dist[0] = -1;
    }
    return dist;
}

vector<vector<ll>> calc(vector<vector<ll>> &s) {
    int n = s.size() + 1;
    int m = n;
    vector ans(n, vector(m, 0LL));
    for(int i = 1; i < n; i ++) {
        for(int j = 1; j < m; j ++) {
            ans[i][j] = s[i - 1][j - 1] - ans[i - 1][j] - ans[i][j - 1] - ans[i - 1][j - 1];
        }
    }
    vector<tuple<ll, ll, ll>> edges;
    rep(i, n) {
        rep(j, m) {
            bool flag = (i + j) & 1;
            if(!flag) {
                edges.pb(i, j + n, ans[i][j]);
            } else {
                edges.pb(j + n, i, ans[i][j]);
            }
        }
    }
    auto dis = USHI(n + m, edges, 0);
    if(dis[0] == -1) {
        ans[0][0] = -1;
        return ans;
    } else {
        rep(i, n) rep(j, m) {
            bool flag = (i + j) & 1;
            if(!flag) {
                ans[i][j] += dis[j + n] - dis[i];
            } else {
                ans[i][j] += dis[i] - dis[j + n];
            }
        }
    }
    return ans;
}

void solve() {
    ll n; cin >> n;
    vector<ll> a((n - 1) * (n - 1));
    for(ll &e : a) cin >> e;
    sort(all(a));
    {
        vector s(n - 1, vector(n - 1, 0LL));
        rep(i, n - 1) rep(j, n - 1) s[i][j] = a[i * (n - 1) + j];
        auto ret = calc(s);
        if(ret[0][0] != -1) {
            rep(i, n) {
                rep(j, n) {
                    cout << ret[i][j] << " ";
                }
                cout << endl;
            }
            return;
        }
    }
    cout << -1 << endl;
    return;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int testcase = 1; 
    cin >> testcase;
    while(testcase --) solve();

    return 0;
}

// https://contest.ucup.ac/contest/2135/problem/12159 部分問題がUCup既出
0