結果

問題 No.1283 Extra Fee
ユーザー fura
提出日時 2020-11-06 22:04:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 1,417 bytes
コンパイル時間 2,377 ms
コンパイル使用メモリ 207,008 KB
最終ジャッジ日時 2025-01-15 20:44:29
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:46:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   46 |         int n,m; scanf("%d%d",&n,&m);
      |                  ~~~~~^~~~~~~~~~~~~~
main.cpp:50:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   50 |                 lint c; scanf("%d%d%lld",&x,&y,&c); x--; y--;
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~

ソースコード

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