結果

問題 No.1678 Coin Trade (Multiple)
ユーザー chocoruskchocorusk
提出日時 2021-08-01 21:17:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,610 bytes
コンパイル時間 4,059 ms
コンパイル使用メモリ 194,368 KB
実行使用メモリ 18,124 KB
最終ジャッジ日時 2023-09-02 14:59:54
合計ジャッジ時間 10,956 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,224 KB
testcase_01 AC 3 ms
6,116 KB
testcase_02 AC 3 ms
6,152 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
	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, 0);
		}
	}
	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, -a[i]);
		add_edge(2*i, 2*i+2, k, 0);
	}
	cout<<-min_cost_flow(0, 2*n, k)<<endl;
    return 0;
}
0