結果

問題 No.258 回転寿司(2)
ユーザー albicilla
提出日時 2017-09-23 11:12:43
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
RE  
実行時間 -
コード長 1,749 bytes
コンパイル時間 1,628 ms
コンパイル使用メモリ 167,516 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-14 03:59:15
合計ジャッジ時間 9,692 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 RE * 1
other AC * 15 WA * 24 RE * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) FOR(i,0,b)
#define INF mugen
#define dump(x) cerr<<#x<<"="<<x<<endl
#define all(a) (a).begin(),(a).end()
#define int long long
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
template <class T> void chmin(T & a, T const & b) { if (b < a) a = b; }

using ll = long long;
const ll mod = LLONG_MAX;

int prevv[1010];
struct Edge{
  int to;int cost;
  Edge(){};
  Edge(int to,int cost){
    this->to=to;
    this->cost=cost;
  }
};

//経路復元(ダイクストラ) prevv配列 prevv[to]=fromを更新式に追加しておく アリ本p99
vector<int> get_path(int t){
  vector<int> path;
  for(;t!=0;t=prevv[t]){
    path.push_back(t);
  }
  //このままだとt->sなので逆順にする
  reverse(path.begin(),path.end());
  return path;
}

vector<Edge> edv[1010];

int d[1010];
signed main(){
  int n;
  cin>>n;
  memset(d,0x3f,sizeof(d));
  memset(prevv,-1,sizeof(prevv));
  V vec(n);
  for(int i=1;i<=n;i++){
    cin>>vec[i];
  }

  edv[0].push_back(Edge(1,-vec[1]));
  edv[0].push_back(Edge(2,-vec[2]));
  for(int i=3;i<=n;i++){
    edv[i-2].push_back(Edge(i,-vec[i]));
  }
  for(int i=4;i<=n;i++){
    edv[i-3].push_back(Edge(i,-vec[i]));
  }

  priority_queue<int> pq;
  pq.push(0);
  d[0]=0;
  while(!pq.empty()){
    int now=pq.top();pq.pop();
    rep(i,edv[now].size()){
      int next=edv[now][i].to;
      int path=edv[now][i].cost;
      if(d[now]+path<d[next]){
        d[next]=d[now]+path;
        prevv[next]=now;
        pq.push(next);
      }
    }
  }

  cout<<-d[n]<<endl;

  V pathvec= get_path(n);
  rep(i,pathvec.size()-1){
    cout<<pathvec[i]<<" ";
  }
  cout<<pathvec[pathvec.size()-1]<<endl;

}
0