結果

問題 No.357 品物の並び替え (Middle)
ユーザー koyumeishikoyumeishi
提出日時 2016-04-03 23:55:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,908 bytes
コンパイル時間 996 ms
コンパイル使用メモリ 108,796 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-01 07:40:20
合計ジャッジ時間 1,835 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
#include <chrono>
using namespace std;

namespace my_io{
	template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
	template<class T> istream& operator ,  (istream& is, T& val){ return is >> val;}
	template<class T> ostream& operator << (ostream& os, const vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"":" "); return os;}
	template<class T> ostream& operator ,  (ostream& os, const T& val){ return os << " " << val;}
	template<class T> ostream& operator >> (ostream& os, const T& val){ return os << " " << val;}

	template<class H> void println(const H& head){ cout << head << endl; }
	template<class H, class ... T> void println(const H& head, const T& ... tail){ cout << head << " "; println(tail...); }
}
using namespace my_io;

int bit_count(long long x){
	long long ret = 0;
	while(x){
		ret++;
		x -= x&-x;
	}
	return ret;
}

int main(){
	int n,m;
	scanf("%d%d", &n, &m);
	vector<int> u(m),v(m),s(m);
	for(int i=0; i<m; i++){
		scanf("%d%d%d", &u[i], &v[i], &s[i]);
	}
	vector<vector<int>> mat(n, vector<int>(n,0));
	for(int i=0; i<m; i++){
		mat[u[i]][v[i]] = s[i];
	}

	vector<int> dp(1<<n, 0);

	vector<vector<int>> ord(n+1);
	for(int i=0; i<1<<n; i++){
		ord[bit_count(i)].push_back(i);
	}
	
	vector<int> logk(1<<n, 0);
	for(int k=0; k<n; k++){
		logk[1<<k] = k;
	}

	for(int i=0; i<n; i++){
		for(int k=0; k<n; k++){
			for(int s: ord[i]){
				if((s>>k)&1) continue;
				int tmp = 0;
				for(int ss=s; ss; ss -= ss&-ss){
					tmp += mat[logk[ss&-ss]][k];
				}
				int& next = dp[s|(1<<k)];
				next = max(next, dp[s]+tmp);
			}
		}
	}
	println(dp[(1<<n)-1]);
	return 0;
}
0