結果

問題 No.971 いたずらっ子
ユーザー zekezeke
提出日時 2020-02-03 18:57:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,488 ms / 2,000 ms
コード長 4,229 bytes
コンパイル時間 1,399 ms
コンパイル使用メモリ 109,272 KB
実行使用メモリ 284,672 KB
最終ジャッジ日時 2024-04-25 02:10:55
合計ジャッジ時間 14,012 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,455 ms
284,508 KB
testcase_01 AC 1,441 ms
284,544 KB
testcase_02 AC 1,031 ms
214,272 KB
testcase_03 AC 1,486 ms
284,652 KB
testcase_04 AC 1,308 ms
268,124 KB
testcase_05 AC 1,488 ms
284,672 KB
testcase_06 AC 1,058 ms
221,312 KB
testcase_07 AC 10 ms
5,632 KB
testcase_08 AC 312 ms
77,184 KB
testcase_09 AC 309 ms
73,600 KB
testcase_10 AC 9 ms
5,632 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
    Author:zeke
    
    pass System Test!
    GET AC!!
*/
#include <iostream>
#include <queue>
#include <vector>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <algorithm>
#include <functional>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <map>
#include <iomanip>
#include <utility>
#include <stack>
#include <bitset>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s) \
    ll(s);      \
    cin >> (s);
#define Scin(s) \
    ll(s);      \
    cin >> (s);
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
const int INF = 7 + (1e+9);
typedef int Weight;
struct Edge
{ //src:辺の始点,dst:辺の終点,weight:辺の重さ
    int src, dst;
    Weight weight;
    Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f)
{
    return e.weight != f.weight ? e.weight > f.weight : //辺は重さが重いものを"小さい"と定義する
               e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
//引数 g:隣接リスト,s:始点,dist:各頂点までの距離が入る,prev:最短路木の親頂点が入る
//戻値 なし
void shortestPath(const Graph &g, int s, vector<Weight> &dist, vector<int> &prev)
{
    int n = g.size();
    dist.assign(n, INF);
    dist[s] = 0;
    prev.assign(n, -1);
    priority_queue<Edge> Q;
    Q.push(Edge(-2, s, 0));
    while (!Q.empty())
    {
        Edge e = Q.top();
        Q.pop();
        if (prev[e.dst] != -1)
            continue;
        prev[e.dst] = e.src;
        for (auto f = g[e.dst].begin(); f != g[e.dst].end(); f++)
        {
            if (dist[f->dst] > e.weight + f->weight)
            {
                dist[f->dst] = e.weight + f->weight;
                Q.push(Edge(f->src, f->dst, e.weight + f->weight));
            }
        }
    }
}

//引数 prev:最短路木の親頂点集合,t:終点
//戻値 path:sからtへの最短経路
vector<int> buildPath(const vector<int> &prev, int t)
{
    vector<int> path;
    for (int u = t; u >= 0; u = prev[u])
        path.push_back(u);
    reverse(path.begin(), path.end());
    return path;
}
/*int main()
{
    // ...
    Graph g(v); //頂点数vの空隣接リストgを生成
    // ...
    g[s].push_back(Edge(s, t, w)); //隣接リストgにsからtに向かう重さwの辺を追加
    // ...
    vector<Weight> dist;
    vector<int> prev;
    shortestPath(g, 0, dist, prev);
    // ...
}*/
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll h, w;
    cin >> h >> w;
    Graph g(h * w + 10);
    VV temp(h, V(w));
    int dx[] = {-1, 0};
    int dy[] = {0, -1};
    rep(i, h)
    {
        string s;
        cin >> s;
        rep(j, w)
        {
            rep(k, 2)
            {
                if (i + dy[k] >= 0 && i + dy[k] < h && j + dx[k] >= 0 && j + dx[k] < w)
                {
                    ll temp1 = i * w + j;
                    ll temp2 = (i + dy[k]) * w + (j + dx[k]);
                    ll weight = i + j + 1;
                    if (s[j] == '.')
                    {
                        g[temp2].push_back(Edge(temp2, temp1, 1));
                    }
                    else
                    {
                        g[temp2].push_back(Edge(temp2, temp1, weight));
                    }
                }
            }
        }
    }
    vector<Weight> dist;
    vector<int> prev;
    shortestPath(g, 0, dist, prev);
    cout << dist[h * w - 1] << endl;
}
0