結果

問題 No.1615 Double Down
ユーザー iaNTUiaNTU
提出日時 2021-06-22 07:55:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,396 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 201,104 KB
実行使用メモリ 103,792 KB
最終ジャッジ日時 2023-09-24 14:47:48
合計ジャッジ時間 27,960 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 TLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;

using coord = ll; // type of data

const int MAXN = 5010;
const coord INF = 0; //for maximum set INF to 0, and negate costs

int N, matchl[MAXN], matchr[MAXN];
coord cst[MAXN][MAXN];

void add(int x, int y, coord c){ cst[x][y] = c; }

coord hungarian(){
	int mat = 0, dad[MAXN], vis[MAXN];
	coord ds[MAXN], y[MAXN], z[MAXN];

	for (int i = 0; i < N; i++)
		y[i] = *min_element(cst[i], cst[i] + N);
	
	for (int j = 0; j < N; j++){
		z[j] = cst[0][j] - y[0];
		for (int i = 1; i < N; i++)
			z[j] = min(z[j], cst[i][j] - y[i]);
	}
	
	memset(matchl, -1, sizeof matchl);
	memset(matchr, -1, sizeof matchr);

	for (int i = 0; i < N; i++) // speedup
		for (int j = 0; j < N; j++)
			if (matchr[j] == -1 && !(cst[i][j] - y[i] - z[j])) {
				matchl[i] = j;
				matchr[j] = i;
				mat++;
				break;
			}

	while (mat < N){
	    int s = 0, j = 0, i;
	    while (matchl[s] != -1)
	    	s++;

	    memset(dad, -1, sizeof dad);
	    memset(vis, 0, sizeof vis);

	    for (int k = 0; k < N; k++)
	    	ds[k] = cst[s][k] - y[s] - z[k];
	    
	    while (true){
	        j = -1;

	        for (int k = 0; k < N; k++)
	        	if (!vis[k] && (j == -1 || ds[k] < ds[j]))
	        		j = k;
	        
	        vis[j] = 1; 
	        i = matchr[j];
	        if (i == -1) 
	        	break;

	        for (int k = 0; k < N; k++)
		        if (!vis[k]){
		            auto new_ds = ds[j] + cst[i][k] - y[i] - z[k];
		            if(ds[k] > new_ds){
		            	ds[k] = new_ds;
		            	dad[k] = j;
		            }
		        }
	    }

	    for (int k = 0; k < N; k++)
		    if (k != j && vis[k]){
		    	auto w = ds[k] - ds[j];
		    	z[k] += w;
		    	y[matchr[k]] -= w;
		    }
	  
	    y[s] += ds[j];
	  
	    while (dad[j] >= 0){
	    	int d = dad[j]; 
	    	matchr[j] = matchr[d]; 
	    	matchl[matchr[j]] = j; 
	    	j = d;
	    }

	    matchr[j] = s; 
	    matchl[s] = j;
	    mat++;
	}
	
	coord value = 0;
	for (int i = 0; i < N; i++)
		value += cst[i][matchl[i]];
	return value;
}

constexpr int kM = int(1E5 + 10);
int x[kM], y[kM], z[kM];

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, m, k, l; cin >> n >> m >> k >> l;
	for (int i = 1; i <= l; i++) cin >> x[i] >> y[i] >> z[i];

	N = max(n, m);
	for (int i = 1; i <= l; i++) add(x[i] - 1, y[i] - 1, -(1LL << z[i]));

	cout << -hungarian() << endl;
}
0