結果

問題 No.3011 品物の並び替え (Extra)
ユーザー ttkkggwwttkkggww
提出日時 2022-09-20 05:12:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,920 ms / 5,000 ms
コード長 1,407 bytes
コンパイル時間 5,563 ms
コンパイル使用メモリ 264,200 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 19:23:05
合計ジャッジ時間 55,482 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,902 ms
4,376 KB
testcase_01 AC 4,901 ms
4,376 KB
testcase_02 AC 4,908 ms
4,380 KB
testcase_03 AC 4,909 ms
4,380 KB
testcase_04 AC 4,907 ms
4,376 KB
testcase_05 AC 4,903 ms
4,376 KB
testcase_06 AC 4,903 ms
4,380 KB
testcase_07 AC 4,902 ms
4,376 KB
testcase_08 AC 4,920 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
int n,m;
int score[50][50];

chrono::system_clock::time_point start,end;

int calc_score(const vector<int> &v){
	int res = 0;
	for(int i = 0;i<n;i++){
		for(int j = i+1;j<n;j++){
			res += score[v[i]][v[j]];
		}
	}
	return res;
}

void solve(){
	int ansscore = 0;
	vector<int> ans(n);
	vector<int> v;
	for(int i=0;i<n;i++){
		v.push_back(i);
	}
	random_device seedgen;
	mt19937 engine(seedgen());
	//for(int i = 0;i<10;i++){
	while(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now()-start).count()<4900){
		shuffle(v.begin(),v.end(),engine);
		int score = calc_score(v);
		bool is = true;
		while(is){
			is = false;
			int up = 0;
			int l,r;
			for(int i = 0;i<n;i++){
				for(int j = i+1;j<n;j++){
					auto nv = v;
					swap(nv[i],nv[j]);
					int cur = calc_score(nv);
					if(up<cur-score){
						up = cur-score;
						l = i,r = j;
						is = true;
					}
				}
			}
			if(is){
				score += up;
				swap(v[l],v[r]);
			}
		}
		if(ansscore<score){
			ansscore = score;
			ans = v;
		}
	}
	cout<<ansscore<<'\n';
	for(auto &i:ans)cout<<i<<' ';cout<<'\n';
}

signed main(){
	start = chrono::system_clock::now();
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for(int i =0;i<m;i++){
		int a,b,c;
		cin >> a >> b >> c;
		score[a][b] = c;
	}
	solve();
}
0