結果

問題 No.1283 Extra Fee
ユーザー akun0716akun0716
提出日時 2020-11-08 10:47:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 3,246 bytes
コンパイル時間 1,189 ms
コンパイル使用メモリ 98,036 KB
実行使用メモリ 79,616 KB
最終ジャッジ日時 2024-04-27 23:38:28
合計ジャッジ時間 5,905 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 14 ms
8,192 KB
testcase_12 AC 17 ms
8,960 KB
testcase_13 AC 11 ms
6,912 KB
testcase_14 AC 43 ms
18,048 KB
testcase_15 AC 68 ms
26,240 KB
testcase_16 AC 14 ms
8,832 KB
testcase_17 AC 219 ms
69,616 KB
testcase_18 AC 240 ms
74,112 KB
testcase_19 AC 252 ms
77,184 KB
testcase_20 AC 233 ms
72,576 KB
testcase_21 AC 237 ms
73,728 KB
testcase_22 AC 218 ms
66,688 KB
testcase_23 AC 227 ms
79,616 KB
testcase_24 AC 243 ms
79,568 KB
testcase_25 AC 261 ms
79,616 KB
testcase_26 AC 265 ms
79,616 KB
testcase_27 AC 256 ms
79,488 KB
testcase_28 AC 261 ms
79,616 KB
testcase_29 AC 234 ms
74,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
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; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;

struct edge { int to, cost; };
#define LLINF 922337203685477580

int N, M;
vector<vector<edge> > G;
VI d;
int t[510][510][2];
int co[510][510];

void dikstra(int s) {
	priority_queue<pii, vector<pii>, greater<pii> >que;
	REP(i, N)d[i] = LLINF;
	d[s] = 0;
	que.push(pii(0, s));
	while (!que.empty()) {
		pii p = que.top();
		que.pop();
		int v = p.second;
		if (d[v] < p.first)continue;
		REP(i, G[v].size()) {
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost) {
				d[e.to] = d[v] + e.cost;
				que.push(pii(d[e.to], e.to));
			}
		}
	}
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int pt = 0;
	cin >> N >> M;
	int n = N;
	REP(i, N)REP(j, N) {
		REP(k, 2)t[i][j][k] = pt++;
		co[i][j] = 1;
	}
	N = N * N * 2;
	G.resize(N);
	d.resize(N);
	REP(i, M) {
		int x, y, c; cin >> x >> y >> c;
		x--; y--;
		co[x][y] += c;
	}

	REP(i, n)REP(j, n) {
		int now = t[i][j][0];
		int to;
		if (i + 1 < n) {
			to = t[i + 1][j][0];
			G[now].push_back((edge) { to, co[i + 1][j] });
			if (co[i + 1][j] > 1) {
				to = t[i + 1][j][1];
				G[now].push_back((edge) { to, 1 });
			}
		}
		if (i - 1 >= 0) {
			to = t[i - 1][j][0];
			G[now].push_back((edge) { to, co[i - 1][j] });
			if (co[i - 1][j] > 1) {
				to = t[i - 1][j][1];
				G[now].push_back((edge) { to, 1 });
			}
		}
		if (j + 1 < n) {
			to = t[i][j + 1][0];
			G[now].push_back((edge) { to, co[i][j + 1] });
			if (co[i][j + 1] > 1) {
				to = t[i][j + 1][1];
				G[now].push_back((edge) { to, 1 });
			}
		}
		if (j - 1 >= 0) {
			to = t[i][j - 1][0];
			G[now].push_back((edge) { to, co[i][j - 1] });
			if (co[i][j - 1] > 1) {
				to = t[i][j - 1][1];
				G[now].push_back((edge) { to, 1 });
			}
		}
		now = t[i][j][1];
		if (i + 1 < n) {
			to = t[i + 1][j][1];
			G[now].push_back((edge) { to, co[i + 1][j] });
		}
		if (i - 1 >= 0) {
			to = t[i - 1][j][1];
			G[now].push_back((edge) { to, co[i - 1][j] });
		}
		if (j + 1 < n) {
			to = t[i][j + 1][1];
			G[now].push_back((edge) { to, co[i][j + 1] });
		}
		if (j - 1 >= 0) {
			to = t[i][j - 1][1];
			G[now].push_back((edge) { to, co[i][j - 1] });
		}
	}
	dikstra(t[0][0][0]);
	cout << d[t[n - 1][n - 1][1]] << endl;
	return 0;
}

0