結果

問題 No.1678 Coin Trade (Multiple)
ユーザー chocoruskchocorusk
提出日時 2021-08-01 21:20:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,780 ms / 5,000 ms
コード長 2,654 bytes
コンパイル時間 3,588 ms
コンパイル使用メモリ 194,352 KB
実行使用メモリ 22,732 KB
最終ジャッジ日時 2023-09-02 15:01:12
合計ジャッジ時間 30,763 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,104 KB
testcase_01 AC 3 ms
6,276 KB
testcase_02 AC 2 ms
6,152 KB
testcase_03 AC 134 ms
14,240 KB
testcase_04 AC 796 ms
19,284 KB
testcase_05 AC 332 ms
21,836 KB
testcase_06 AC 243 ms
20,632 KB
testcase_07 AC 725 ms
15,464 KB
testcase_08 AC 465 ms
16,524 KB
testcase_09 AC 359 ms
22,224 KB
testcase_10 AC 125 ms
9,920 KB
testcase_11 AC 582 ms
18,648 KB
testcase_12 AC 118 ms
9,712 KB
testcase_13 AC 1,337 ms
21,300 KB
testcase_14 AC 374 ms
14,036 KB
testcase_15 AC 425 ms
18,188 KB
testcase_16 AC 106 ms
17,928 KB
testcase_17 AC 367 ms
22,328 KB
testcase_18 AC 3 ms
6,156 KB
testcase_19 AC 3 ms
6,108 KB
testcase_20 AC 3 ms
6,188 KB
testcase_21 AC 3 ms
6,124 KB
testcase_22 AC 3 ms
6,188 KB
testcase_23 AC 3 ms
6,116 KB
testcase_24 AC 4 ms
6,188 KB
testcase_25 AC 3 ms
6,152 KB
testcase_26 AC 3 ms
6,140 KB
testcase_27 AC 3 ms
6,176 KB
testcase_28 AC 3 ms
6,180 KB
testcase_29 AC 3 ms
6,112 KB
testcase_30 AC 3 ms
6,128 KB
testcase_31 AC 3 ms
6,180 KB
testcase_32 AC 3 ms
6,132 KB
testcase_33 AC 3 ms
6,188 KB
testcase_34 AC 3 ms
6,136 KB
testcase_35 AC 3 ms
6,316 KB
testcase_36 AC 3 ms
6,188 KB
testcase_37 AC 3 ms
6,152 KB
testcase_38 AC 3 ms
6,296 KB
testcase_39 AC 3 ms
6,116 KB
testcase_40 AC 3 ms
6,124 KB
testcase_41 AC 3 ms
6,184 KB
testcase_42 AC 3 ms
6,236 KB
testcase_43 AC 3 ms
6,128 KB
testcase_44 AC 3 ms
6,228 KB
testcase_45 AC 3 ms
6,108 KB
testcase_46 AC 3 ms
6,188 KB
testcase_47 AC 3 ms
6,428 KB
testcase_48 AC 1,700 ms
22,540 KB
testcase_49 AC 1,718 ms
22,584 KB
testcase_50 AC 1,728 ms
22,628 KB
testcase_51 AC 1,754 ms
22,524 KB
testcase_52 AC 1,755 ms
22,652 KB
testcase_53 AC 1,766 ms
22,732 KB
testcase_54 AC 1,780 ms
22,536 KB
testcase_55 AC 1,767 ms
22,588 KB
testcase_56 AC 1,767 ms
22,592 KB
testcase_57 AC 1,755 ms
22,564 KB
testcase_58 AC 813 ms
19,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct edge{
    int to, cap, rev; ll cost;
    edge(int to, int cap, ll cost, int rev):to(to), cap(cap), cost(cost), rev(rev){}
};
const int MAXV=100010;
int V;
vector<edge> g[MAXV];
ll h[MAXV];
ll dist[MAXV];
int prevv[MAXV], preve[MAXV];

void add_edge(int from, int to, int cap, ll cost){
    edge e=edge(to, cap, cost, g[to].size());
    g[from].push_back(e);
    e=edge(from, 0, -cost, g[from].size()-1);
    g[to].push_back(e);
}

ll min_cost_flow(int s, int t, int f){
    using P=pair<ll, int>;
    const ll INF=1e18;
    ll res=0;
    fill(h, h+V, 0);
    while(f>0){
        priority_queue<P, vector<P>, greater<P>> que;
        fill(dist, dist+V, INF);
        dist[s]=0;
        que.push({0, s});
        while(!que.empty()){
            P p=que.top(); que.pop();
            int v=p.second;
            if(dist[v]<p.first) continue;
            for(int i=0; i<g[v].size(); i++){
                edge &e=g[v][i];
                if(e.cap>0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
                    dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
                    prevv[e.to]=v;
                    preve[e.to]=i;
                    que.push({dist[e.to], e.to});
                }
            }
        }
        for(int v=0; v<V; v++) h[v]+=dist[v];
        if(dist[t]==INF) return -1;
        int d=f;
        for(int v=t; v!=s; v=prevv[v]){
            d=min(d, g[prevv[v]][preve[v]].cap);
        }
        f-=d;
        res+=d*h[t];
        for(int v=t; v!=s; v=prevv[v]){
            edge &e=g[prevv[v]][preve[v]];
            e.cap-=d;
            g[v][e.rev].cap+=d;
        }
    }
    return res;
}
int main()
{
	int n, k;
	cin>>n>>k;
	V=2*n+1;
	const ll geta=1e9;
	ll a[100010];
	for(int i=0; i<n; i++){
		cin>>a[i];
		int m; cin>>m;
		for(int j=0; j<m; j++){
			int b; cin>>b; b--;
			add_edge(2*b+1, 2*i+1, 1, geta*(i-b));
		}
	}
	for(int i=0; i<n; i++){
		add_edge(2*i, 2*i+1, k, a[i]);
		add_edge(2*i+1, 2*i+2, k, geta-a[i]);
		add_edge(2*i, 2*i+2, k, geta);
	}
	cout<<geta*k*n-min_cost_flow(0, 2*n, k)<<endl;
    return 0;
}
0