結果

問題 No.1283 Extra Fee
ユーザー onsen_manjuuu
提出日時 2020-11-06 22:09:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 469 ms / 2,000 ms
コード長 1,466 bytes
コンパイル時間 2,132 ms
コンパイル使用メモリ 185,536 KB
実行使用メモリ 76,768 KB
最終ジャッジ日時 2024-11-16 06:41:47
合計ジャッジ時間 8,927 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:47:34: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   47 |                         for(auto [ny, nx] : dir4(i, j)) {
      |                                  ^

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using ll = long long;


//経路復元なしDijkstra法
template<class T> 
vector<T> dijkstra( const vector<vector<pair<int,T>>>& Hen, int start = 0){
	T INF = numeric_limits<T>::max();
	int N = Hen.size();
	vector<T> dist(N, INF);
	dist[start] = 0;
	priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> que;
	que.push({0, start});
	while(que.size()) {
		int ver = que.top().second;
		T cost = que.top().first; que.pop();
		if(cost != dist[ver])continue;
		for(auto i : Hen[ver]) {
			if(dist[i.first] > cost + i.second) {
				dist[i.first] = cost + i.second;
				que.push({dist[i.first], i.first});
			}
		}
	}
	return dist;
}

vector<pair<int,int>> dir4(int x, int y) {return {{x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}};};

int main()
{
	int N, M; cin >> N >> M;
	vector<vector<int>> grid(N, vector<int>(N, 1));
	for(int i = 0; i < M; i++) {
		int h, w, c; cin >> h >> w >> c; grid[h - 1][w - 1] += c;
	}
	int K = N * N;



	vector<vector<pair<int,ll>>> hen(2 * N * N);
	for(int i = 0; i < N; i++) {
		for(int j = 0; j < N; j++) {
			int cur = i * N + j;
			for(auto [ny, nx] : dir4(i, j)) {
				if(ny < 0 || nx < 0 || ny >= N || nx >= N)continue;
				int nxt = ny * N + nx;
				hen[cur].push_back({nxt, grid[ny][nx]});
				hen[cur + K].push_back({nxt + K, grid[ny][nx]});
				hen[cur].push_back({nxt + K, 1});
			}
		}
	}


	vector<ll> dist = dijkstra(hen);
	cout << dist[2 * K - 1] << endl;

}
0