結果

問題 No.2309 [Cherry 5th Tune D] 夏の先取り
ユーザー hongrockhongrock
提出日時 2023-05-19 23:25:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 510 ms / 3,000 ms
コード長 2,875 bytes
コンパイル時間 2,441 ms
コンパイル使用メモリ 208,256 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-23 01:17:14
合計ジャッジ時間 15,384 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
4,384 KB
testcase_01 AC 242 ms
4,380 KB
testcase_02 AC 248 ms
4,380 KB
testcase_03 AC 260 ms
4,380 KB
testcase_04 AC 161 ms
4,380 KB
testcase_05 AC 205 ms
4,376 KB
testcase_06 AC 117 ms
4,384 KB
testcase_07 AC 212 ms
4,380 KB
testcase_08 AC 263 ms
4,380 KB
testcase_09 AC 227 ms
4,380 KB
testcase_10 AC 215 ms
4,380 KB
testcase_11 AC 239 ms
4,376 KB
testcase_12 AC 224 ms
4,376 KB
testcase_13 AC 251 ms
4,380 KB
testcase_14 AC 207 ms
4,376 KB
testcase_15 AC 304 ms
4,380 KB
testcase_16 AC 192 ms
4,380 KB
testcase_17 AC 253 ms
4,380 KB
testcase_18 AC 255 ms
4,384 KB
testcase_19 AC 225 ms
4,376 KB
testcase_20 AC 271 ms
4,376 KB
testcase_21 AC 150 ms
4,376 KB
testcase_22 AC 172 ms
4,380 KB
testcase_23 AC 216 ms
4,380 KB
testcase_24 AC 243 ms
4,376 KB
testcase_25 AC 204 ms
4,380 KB
testcase_26 AC 292 ms
4,380 KB
testcase_27 AC 192 ms
4,380 KB
testcase_28 AC 227 ms
4,380 KB
testcase_29 AC 253 ms
4,380 KB
testcase_30 AC 224 ms
4,376 KB
testcase_31 AC 495 ms
4,380 KB
testcase_32 AC 499 ms
4,380 KB
testcase_33 AC 510 ms
4,380 KB
testcase_34 AC 495 ms
4,380 KB
testcase_35 AC 469 ms
4,380 KB
testcase_36 AC 58 ms
4,380 KB
testcase_37 AC 15 ms
4,376 KB
testcase_38 AC 56 ms
4,376 KB
testcase_39 AC 33 ms
4,376 KB
testcase_40 AC 41 ms
4,376 KB
testcase_41 AC 431 ms
4,384 KB
testcase_42 AC 425 ms
4,380 KB
testcase_43 AC 236 ms
4,376 KB
testcase_44 AC 137 ms
4,380 KB
testcase_45 AC 263 ms
4,380 KB
testcase_46 AC 194 ms
4,380 KB
testcase_47 AC 47 ms
4,376 KB
testcase_48 AC 37 ms
4,380 KB
testcase_49 AC 164 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i, a, n) for(int i=(a); i<(n); ++i)
#define per(i, a, n) for(int i=(a); i>(n); --i)
#define pb emplace_back
#define mp make_pair
#define clr(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(),(x).end()
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define lson o<<1
#define rson o<<1|1
#define gmid l[o]+r[o]>>1
 
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
using ui = unsigned int;
 
constexpr int mod = 998244353;
constexpr int inf = 0x3f3f3f3f;
constexpr ll infll = (ll)1e18;
constexpr double EPS = 1e-8;
const double PI = acos(-1.0);

constexpr int N = 2e5 + 10;

int T, a, b, c;
ll x, y, z, w;
int idx[10][10];

struct Edge{
  int from, to, cap, flow;
  ll cost;
  Edge(){}
  Edge(int from, int to, int cap, ll cost):from(from),to(to),cap(cap),cost(cost){
    flow = 0;
  }
};
vector<Edge> G;
vector<int> V[10];
int A[10], P[10];
ll d[10];
bool inq[10];

void add_edge(int from, int to){
  G.pb(from, to, 0, 0);
  G.pb(to, from, 0, 0);
  int x = G.size();
  idx[from][to] = x - 2;
  V[from].pb(x - 2);
  V[to].pb(x - 1);
}

void upd(int id, int cap, ll cost){
  G[id].cap = cap;
  G[id].cost = cost;
  G[id^1].cost = -cost;
}

bool BellmanFord(int &flow, ll &cost){
  rep(i, 1, 6){
    d[i] = infll;
    inq[i] = 0;
  }
  d[0] = 0;
  A[0] = inf;
  P[0] = -1;
  queue<int> Q;
  inq[0] = 1;
  Q.push(0);
  while(!Q.empty()){
    int x = Q.front(); Q.pop();
    inq[x] = 0;
    for(int y : V[x]){
      Edge &e = G[y];
      if(e.cap > e.flow && d[e.to] > d[x] + e.cost){
        d[e.to] = d[x] + e.cost;
        A[e.to] = min(A[x], e.cap - e.flow);
        P[e.to] = y;
        if(!inq[e.to]){
          inq[e.to] = 1;
          Q.push(e.to);
        }
      }
    }
  }
  if(d[5] == infll) return 0;
  flow += A[5];
  cost += A[5] * d[5];

  int x = 5;
  while(x){
    G[P[x]].flow += A[5];
    G[P[x]^1].flow -= A[5];
    x = G[P[x]].from;
  }

  return 1;
}

ll solve(int t){
  for(auto &e : G)  e.flow = 0;
  upd(idx[0][1], a, 0);
  upd(idx[0][2], t, 0);
  upd(idx[1][3], b - t, -x);
  upd(idx[1][4], a, -z);
  upd(idx[2][4], t, -y);
  upd(idx[3][4], b - t, -w + x);
  upd(idx[1][5], a, 0);
  upd(idx[2][5], t, 0);
  upd(idx[3][5], b - t, 0);
  upd(idx[4][5], c, 0);

  int flow = 0;
  ll cost = 0;
  while(BellmanFord(flow, cost));
  return -cost;
}

void _main(){
  add_edge(0, 1);
  add_edge(0, 2);
  add_edge(1, 3);
  add_edge(1, 4);
  add_edge(2, 4);
  add_edge(3, 4);
  add_edge(1, 5);
  add_edge(2, 5);
  add_edge(3, 5);
  add_edge(4, 5);

  cin >> T;
  while(T--){
    cin >> a >> b >> c;
    cin >> x >> y >> z >> w;
    ll ans = 0;
    rep(i, 0, b + 1){
      ans = max(ans, solve(i));
    }
    cout << ans << '\n';
  }
}

int main(){
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  _main();
  return 0;
}
0