結果

問題 No.1283 Extra Fee
ユーザー leaf_1415leaf_1415
提出日時 2020-11-06 23:31:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 316 ms / 2,000 ms
コード長 1,946 bytes
コンパイル時間 652 ms
コンパイル使用メモリ 89,780 KB
実行使用メモリ 83,672 KB
最終ジャッジ日時 2024-04-27 23:26:33
合計ジャッジ時間 5,955 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
25,812 KB
testcase_01 AC 7 ms
25,628 KB
testcase_02 AC 7 ms
26,800 KB
testcase_03 AC 8 ms
25,504 KB
testcase_04 AC 7 ms
26,116 KB
testcase_05 AC 7 ms
25,460 KB
testcase_06 AC 7 ms
25,608 KB
testcase_07 AC 6 ms
26,336 KB
testcase_08 AC 6 ms
26,264 KB
testcase_09 AC 8 ms
25,476 KB
testcase_10 AC 8 ms
26,720 KB
testcase_11 AC 19 ms
28,728 KB
testcase_12 AC 21 ms
29,212 KB
testcase_13 AC 16 ms
28,248 KB
testcase_14 AC 53 ms
35,780 KB
testcase_15 AC 85 ms
41,708 KB
testcase_16 AC 24 ms
30,204 KB
testcase_17 AC 265 ms
78,624 KB
testcase_18 AC 277 ms
77,816 KB
testcase_19 AC 299 ms
80,200 KB
testcase_20 AC 276 ms
76,752 KB
testcase_21 AC 282 ms
77,728 KB
testcase_22 AC 261 ms
72,184 KB
testcase_23 AC 271 ms
81,968 KB
testcase_24 AC 277 ms
81,972 KB
testcase_25 AC 316 ms
81,968 KB
testcase_26 AC 309 ms
81,968 KB
testcase_27 AC 311 ms
81,968 KB
testcase_28 AC 305 ms
81,968 KB
testcase_29 AC 294 ms
83,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18

using namespace std;

typedef long long llint;
typedef pair<llint, llint> P;

struct edge{
	llint to, cost;
	edge(){}
	edge(llint a, llint b){
		to = a, cost = b;
	}
};

llint n, m;
llint a[505][505];
vector<edge> G[700005];
llint dist[700005];
llint dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

llint get(llint i, llint x, llint y)
{
	return i*n*n + y*n + x;
}

void dijkstra(vector<edge> G[], llint S, llint dist[])
{
	for(int i = 0; i <= 700000; i++) dist[i] = inf;
	dist[S] = 0;
	
	priority_queue< P, vector<P>, greater<P> > Q;
	Q.push( make_pair(0, S) );
	
	llint v, d;
	while(Q.size()){
		d = Q.top().first;
		v = Q.top().second;
		Q.pop();
		if(dist[v] < d) continue;
		for(int i = 0; i < G[v].size(); i++){
			if(dist[G[v][i].to] > d + G[v][i].cost){
				dist[G[v][i].to] = d + G[v][i].cost;
				Q.push( make_pair(dist[G[v][i].to], G[v][i].to) );
			}
		}
	}
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
	llint h, w, c;
	rep(i, 1, m){
		cin >> h >> w >> c;
		a[w][h] = c;
	}
	
	rep(x, 1, n){
		rep(y, 1, n){
			rep(i, 0, 1){
				int v = get(i, x, y);
				rep(d, 0, 3){
					int nx = x + dx[d], ny = y + dy[d];
					if(nx <= 0 || nx > n || ny <= 0 || ny > n) continue;
					G[v].push_back(edge(get(i, nx, ny), a[nx][ny]+1));
					if(i == 0) G[v].push_back(edge(get(1, nx, ny), 1));
				}
			}
		}
	}
	dijkstra(G, get(0, 1, 1), dist);
	cout << min(dist[get(0, n, n)], dist[get(1, n, n)]) << endl;
	
	return 0;
}
0