結果
| 問題 |
No.971 いたずらっ子
|
| コンテスト | |
| ユーザー |
Dente
|
| 提出日時 | 2020-01-17 21:45:09 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,502 ms / 2,000 ms |
| コード長 | 1,573 bytes |
| コンパイル時間 | 2,181 ms |
| コンパイル使用メモリ | 177,496 KB |
| 実行使用メモリ | 245,248 KB |
| 最終ジャッジ日時 | 2024-11-07 11:27:12 |
| 合計ジャッジ時間 | 14,596 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 21 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a) for(int i = 0; i < (a); i++)
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;
template <typename T>
vector<T> Dijkstra(int s, vector<vector<pair<int, T>>> & G, /*vector<int> & prev,*/ const T INF = 1e9){
using P = pair<T, int>;
int V = G.size();
vector<T> dist(V, INF);
priority_queue<P, vector<P>, greater<P>> que;
dist[s] = 0;
que.emplace(0, s);
/*prev.assign(V, -1);*/
while(!que.empty()){
P p = que.top();
que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i = 0; i < G[v].size(); i++){
int to = G[v][i].first;
T cost = G[v][i].second;
if(dist[to] > dist[v] + cost){
dist[to] = dist[v] + cost;
/*prev[to] = v;*/
que.emplace(dist[to], to);
}
}
}
return dist;
}
signed main(){
int h,w;
cin >> h >> w;
vector<string> a(h);
REP(i,h){
cin >> a[i];
}
int V = h * w;
vector<vector<pair<int ,int>>> G(V);
REP(i,h){
REP(j,w){
int cost = 1;
if(a[i][j] == 'k') cost = 1 + i + j;
if(i > 0) G[(i - 1) * w + j].emplace_back(i * w + j, cost);
if(j > 0) G[i * w + j - 1].emplace_back(i * w + j, cost);
}
}
vector<int> dist = Dijkstra(0, G);
cout << dist[V - 1] << endl;
return 0;
}
Dente