結果
| 問題 | No.357 品物の並び替え (Middle) | 
| コンテスト | |
| ユーザー |  koyumeishi | 
| 提出日時 | 2016-04-03 23:55:42 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 1,908 bytes | 
| コンパイル時間 | 1,246 ms | 
| コンパイル使用メモリ | 110,000 KB | 
| 実行使用メモリ | 6,820 KB | 
| 最終ジャッジ日時 | 2024-10-10 23:53:45 | 
| 合計ジャッジ時間 | 1,800 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 18 | 
ソースコード
#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;
}
            
            
            
        