結果
問題 | No.8011 品物の並び替え (Extra) |
ユーザー |
|
提出日時 | 2020-07-31 08:58:20 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4,996 ms / 5,000 ms |
コード長 | 1,430 bytes |
コンパイル時間 | 2,355 ms |
コンパイル使用メモリ | 206,668 KB |
最終ジャッジ日時 | 2025-01-12 08:37:08 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 9 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:39:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 39 | scanf("%d%d",&n,&m); | ~~~~~^~~~~~~~~~~~~~ main.cpp:42:33: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 42 | int u,v,c; scanf("%d%d%d",&u,&v,&c); | ~~~~~^~~~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; template<class T> struct edge{ int to; T wt; edge(int to,const T& wt):to(to),wt(wt){} }; template<class T> using weighted_graph=vector<vector<edge<T>>>; template<class T> void add_directed_edge(weighted_graph<T>& G,int u,int v,const T& wt){ G[u].emplace_back(v,wt); } random_device seed_gen; mt19937 rng(seed_gen()); int n,m; weighted_graph<int> G; int calc_score(const vector<int>& p){ int res=0; vector<bool> vis(n); rep(i,n){ int u=p[n-1-i]; vis[u]=true; for(auto e:G[u]) if(vis[e.to]) res+=e.wt; } return res; } int main(){ auto start=std::chrono::system_clock::now(); scanf("%d%d",&n,&m); G.resize(n); rep(i,m){ int u,v,c; scanf("%d%d%d",&u,&v,&c); add_directed_edge(G,u,v,c); } vector<int> p(n); iota(p.begin(),p.end(),0); int ans=calc_score(p); auto p_ans=p; for(int t=0;;t++){ auto now=std::chrono::system_clock::now(); if(std::chrono::duration_cast<std::chrono::milliseconds>(now-start).count()>4990) break; shuffle(p.begin(),p.end(),rng); while(1){ bool updated=false; rep(i,n) for(int j=i+1;j<n;j++) { swap(p[i],p[j]); int tmp=calc_score(p); if(tmp>ans){ ans=tmp; p_ans=p; updated=true; } else{ swap(p[i],p[j]); } } if(!updated) break; } } printf("%d\n",ans); rep(i,n) printf("%d%c",p_ans[i],i<n-1?' ':'\n'); return 0; }