結果
| 問題 |
No.1324 Approximate the Matrix
|
| コンテスト | |
| ユーザー |
theory_and_me
|
| 提出日時 | 2021-07-16 01:40:45 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 167 ms / 2,000 ms |
| コード長 | 9,521 bytes |
| コンパイル時間 | 3,901 ms |
| コンパイル使用メモリ | 218,556 KB |
| 最終ジャッジ日時 | 2025-01-23 01:12:25 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};
const ll INF = (1ll<<62);
// const ld EPS = 1e-10;
// const ld PI = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;
#include <atcoder/maxflow>
using namespace atcoder;
template < class Flow, class Cost > struct cs_graph{
struct edge{
int from, to;
Flow cap, flow;
Cost cost, cost_m;
};
struct residual_edge{
int to, rev, id;
bool is_rev;
Flow cap;
Cost cost;
};
int n;
vector<edge> E;
vector<Cost> dual;
vector<Flow> b;
cs_graph() {}
cs_graph(int n) : n(n){
dual.resize(n, 0);
b.resize(n, 0);
}
int add_edge(int from, int to, Flow cap, Flow flow, Cost cost){
int m = int(E.size());
E.push_back(edge{from, to, cap, flow, cost, -1});
return m;
}
void set_b(const vector<Flow> &b_input){
b = b_input;
return;
}
Cost calc_objective(){
Cost obj = 0;
for(const auto &e: E){
obj += e.cost * e.flow;
}
return obj;
}
void print_E(){
for(const auto &e: E){
cerr << "from: " << e.from << ", to: " << e.to << ", cap: " << e.cap << ", flow: " << e.flow << ", cost: " << e.cost << ", cost_m: " << e.cost_m << "\n" ;
}
return;
}
bool check_feasibility(){
mf_graph<Flow> mf_G(n+2);
int s = n;
int t = s + 1;
Flow plus_flow_sum = 0, minus_flow_sum = 0;
for(int i=0;i<n;i++){
if(b[i] > 0){
plus_flow_sum += b[i];
mf_G.add_edge(s, i, b[i]);
}
if(b[i] < 0){
minus_flow_sum += (-b[i]);
mf_G.add_edge(i, t, -b[i]);
}
}
assert(plus_flow_sum == minus_flow_sum);
for(const auto &e: E){
mf_G.add_edge(e.from, e.to, e.cap);
}
Flow mf = mf_G.flow(s, t);
return (mf == plus_flow_sum);
}
Cost cost_scaling(){
Cost cost_max = 0;
for(const auto &e: E){
if(e.cost > cost_max) cost_max = e.cost;
}
Cost scaling_factor = 2;
Cost init_eps = 1;
while(true){
init_eps *= scaling_factor;
if(init_eps >= n * cost_max) break;
}
for(auto &e: E){
e.cost_m = e.cost * init_eps;
}
Cost eps = init_eps;
while(true){
// improve approximation(eps, x, pi)
vector<vector<residual_edge>> g(n);
// for debug
// auto print_residual_graph = [&](){
// for(int i=0;i<n;i++){
// for(int j=0;j<(int)g[i].size();j++){
// const auto &e = g[i][j];
// Cost reduced_cost = e.cost - dual[i] + dual[e.to];
// if(e.cap > 0) cerr << "from: " << i << ", to: " << e.to << ", cap: " << e.cap << ", r_cost: " << reduced_cost << "\n";
// }
// }
// };
// add edges to the residual graph
for(int i=0;i<(int)E.size();i++){
auto &e = E[i];
Cost reduced_cost = e.cost_m - dual[e.from] + dual[e.to];
if(reduced_cost > 0){
e.flow = 0;
g[e.from].push_back(residual_edge{e.to, (int)g[e.to].size(), i, false, e.cap, e.cost_m});
g[e.to].push_back(residual_edge{e.from, (int)g[e.from].size() - 1, i, true, 0, -e.cost_m});
}else{
e.flow = e.cap;
g[e.from].push_back(residual_edge{e.to, (int)g[e.to].size(), i, false, 0, e.cost_m});
g[e.to].push_back(residual_edge{e.from, (int)g[e.from].size() - 1, i, true, e.cap, -e.cost_m});
}
}
// calculate node balance
vector<Flow> balance = b;
for(const auto &e: E){
balance[e.from] -= e.flow;
balance[e.to] += e.flow;
}
// define active node queue
queue<int> active;
for(int i=0;i<n;i++){
if(balance[i] > 0){
active.push(i);
}
}
// print_residual_graph();
vector<int> current_arc(n, 0);
vector<bool> is_positive_cap(n, false);
while(!active.empty()){
int v = active.front();
while(true){
if(current_arc[v] == (int)g[v].size()){
if(!is_positive_cap[v]){
return -1;
}
dual[v] += eps / scaling_factor;
current_arc[v] = 0;
is_positive_cap[v] = false;
}else{
auto &e = g[v][current_arc[v]];
Cost reduced_cost = e.cost - dual[v] + dual[e.to];
if(e.cap > 0){
is_positive_cap[v] = true;
}
if(e.cap > 0 and reduced_cost < 0){
Flow push_flow = min(balance[v], e.cap);
Flow pre_balance = balance[e.to];
// augment
e.cap -= push_flow;
g[e.to][e.rev].cap += push_flow;
balance[v] -= push_flow;
balance[e.to] += push_flow;
// change flow of E
if(!e.is_rev){
E[e.id].flow += push_flow;
}else{
E[e.id].flow -= push_flow;
}
// add node e.to to active
if(pre_balance <= 0 and balance[e.to] > 0){
active.push(e.to);
}
if(balance[v] == 0){
active.pop();
break;
}
}
current_arc[v]++;
}
}
}
// print_E();
// dump(balance)
// dump(dual)
// print_residual_graph();
if(eps == 2) break;
eps /= scaling_factor;
}
return calc_objective();
}
};
int main(){
int N, K;
cin >> N >> K;
vector<int> A(N), B(N);
for(int i=0;i<N;i++) cin >> A[i];
for(int i=0;i<N;i++) cin >> B[i];
vector<vector<int>> P(N, vector<int>(N, 0));
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cin >> P[i][j];
}
}
cs_graph<int, ll> G(2*N+2);
int s = 2*N, t = s+1;
for(int i=0;i<N;i++){
G.add_edge(s, i, A[i], 0, 0);
}
ll S = 0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
S += P[i][j] * P[i][j];
for(int k=0;k<A[i];k++){
G.add_edge(i, N+j, 1, 0, 2*k+1-2*P[i][j]);
}
}
}
for(int i=0;i<N;i++){
G.add_edge(N+i, t, B[i], 0, 0);
}
vector<int> b(2*N+2, 0);
b[s] = K;
b[t] = -K;
G.set_b(b);
cout << G.cost_scaling() + S << endl;
return 0;
}
theory_and_me