結果
| 問題 |
No.8011 品物の並び替え (Extra)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-07-31 08:53:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,311 bytes |
| コンパイル時間 | 2,561 ms |
| コンパイル使用メモリ | 205,952 KB |
| 最終ジャッジ日時 | 2025-01-12 08:35:05 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 3 WA * 6 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:37:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
37 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:40:33: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
40 | 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(){
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;
rep(_,100000){
shuffle(p.begin(),p.end(),rng);
int tmp=calc_score(p);
if(tmp>ans){
ans=tmp;
p_ans=p;
}
}
while(1){
bool updated=false;
rep(i,n) for(int j=i+1;j<n;j++) {
swap(p_ans[i],p_ans[j]);
int tmp=calc_score(p);
if(tmp>ans){
ans=tmp;
p_ans=p;
updated=true;
}
else{
swap(p_ans[i],p_ans[j]);
}
}
if(!updated) break;
}
printf("%d\n",ans);
rep(i,n) printf("%d%c",p_ans[i],i<n-1?' ':'\n');
return 0;
}