結果

問題 No.1283 Extra Fee
ユーザー furafura
提出日時 2020-11-06 22:04:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 1,417 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 215,528 KB
実行使用メモリ 36,304 KB
最終ジャッジ日時 2024-04-27 23:16:55
合計ジャッジ時間 5,865 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 9 ms
6,940 KB
testcase_12 AC 10 ms
6,940 KB
testcase_13 AC 8 ms
6,940 KB
testcase_14 AC 31 ms
8,960 KB
testcase_15 AC 49 ms
12,160 KB
testcase_16 AC 10 ms
6,940 KB
testcase_17 AC 178 ms
33,436 KB
testcase_18 AC 183 ms
32,328 KB
testcase_19 AC 198 ms
33,656 KB
testcase_20 AC 191 ms
31,836 KB
testcase_21 AC 187 ms
32,324 KB
testcase_22 AC 167 ms
29,164 KB
testcase_23 AC 190 ms
34,676 KB
testcase_24 AC 200 ms
34,572 KB
testcase_25 AC 206 ms
34,660 KB
testcase_26 AC 211 ms
34,604 KB
testcase_27 AC 208 ms
34,608 KB
testcase_28 AC 206 ms
34,696 KB
testcase_29 AC 187 ms
36,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

const int dx[]={0,-1,0,1},dy[]={1,0,-1,0};

template<class T> struct edge{
	int to;
	T wt;
	edge(int to,const T& wt):to(to),wt(wt){}
};
template<class T> using weighted_graph=vector<vector<edge<T>>>;

template<class T>
void add_directed_edge(weighted_graph<T>& G,int u,int v,const T& wt){
	G[u].emplace_back(v,wt);
}

template<class T>
vector<T> Dijkstra(const weighted_graph<T>& G,int s){
	constexpr T INF=numeric_limits<T>::max();
	int n=G.size();
	vector<T> d(n,INF); d[s]=0;
	priority_queue<pair<T,int>> Q; Q.emplace(0,s);
	while(!Q.empty()){
		T d0=-Q.top().first;
		int u=Q.top().second; Q.pop();
		if(d0>d[u]) continue;
		for(const auto& e:G[u]){
			int v=e.to;
			if(d[v]>d[u]+e.wt){
				d[v]=d[u]+e.wt;
				Q.emplace(-d[v],v);
			}
		}
	}
	return d;
}

const long long INF=1LL<<61;

int main(){
	int n,m; scanf("%d%d",&n,&m);
	vector cost(n,vector(n,0LL));
	rep(i,m){
		int x,y;
		lint c; scanf("%d%d%lld",&x,&y,&c); x--; y--;
		cost[x][y]=c;
	}

	weighted_graph<lint> G(n*n);
	rep(i,n) rep(j,n) rep(k,4) {
		int x=i+dx[k],y=j+dy[k];
		if(0<=x && x<n && 0<=y && y<n){
			add_directed_edge(G,i*n+j,x*n+y,cost[x][y]+1);
		}
	}

	auto d1=Dijkstra(G,0);
	auto d2=Dijkstra(G,n*n-1);

	lint ans=INF;
	rep(i,n) rep(j,n) ans=min(ans,d1[i*n+j]+d2[i*n+j]-2*cost[i][j]);
	printf("%lld\n",ans);

	return 0;
}
0