結果

問題 No.8011 品物の並び替え (Extra)
ユーザー ttkkggww
提出日時 2022-09-20 05:12:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4,922 ms / 5,000 ms
コード長 1,407 bytes
コンパイル時間 4,398 ms
コンパイル使用メモリ 255,012 KB
最終ジャッジ日時 2025-02-07 12:49:15
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:53:46: warning: ‘r’ may be used uninitialized [-Wmaybe-uninitialized]
   53 |                                 swap(v[l],v[r]);
      |                                              ^
main.cpp:38:31: note: ‘r’ was declared here
   38 |                         int l,r;
      |                               ^
main.cpp:53:41: warning: ‘l’ may be used uninitialized [-Wmaybe-uninitialized]
   53 |                                 swap(v[l],v[r]);
      |                                         ^
main.cpp:38:29: note: ‘l’ was declared here
   38 |                         int l,r;
      |                             ^

ソースコード

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