結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
umezo
|
| 提出日時 | 2020-11-07 07:19:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 624 ms / 2,000 ms |
| コード長 | 2,002 bytes |
| コンパイル時間 | 3,291 ms |
| コンパイル使用メモリ | 206,344 KB |
| 最終ジャッジ日時 | 2025-01-15 21:26:17 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
const ll INF=1LL<<60;
struct Edge{
int to;
ll w;
Edge(int to,ll w) : to(to),w(w) {}
};
using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;
ll A[510][510];
template<class T> bool chmin(T& a,T b){
if(a>b){
a=b;
return true;
}
return false;
}
int main(){
int n,m;
cin>>n>>m;
Graph G(n*n);
rep(i,m){
int h,w;
ll c;
cin>>h>>w>>c;
h--,w--;
A[h][w]=c;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i>0){
G[(i-1)*n+j].push_back(Edge(i*n+j,A[i][j]+1));
G[i*n+j].push_back(Edge((i-1)*n+j,A[i-1][j]+1));
}
if(i<n-1){
G[(i+1)*n+j].push_back(Edge(i*n+j,A[i][j]+1));
G[i*n+j].push_back(Edge((i+1)*n+j,A[i+1][j]+1));
}
if(j>0){
G[i*n+j-1].push_back(Edge(i*n+j,A[i][j]+1));
G[i*n+j].push_back(Edge(i*n+j-1,A[i][j-1]+1));
}
if(j<n-1){
G[i*n+j+1].push_back(Edge(i*n+j,A[i][j]+1));
G[i*n+j].push_back(Edge(i*n+j+1,A[i][j+1]+1));
}
}
}
vector<ll> dist(n*n,INF),dist1(n*n,INF);
dist[0]=0,dist1[n*n-1]=0;
priority_queue<pli,vector<pli>,greater<pli>> que,que1;
que.push({dist[0],0});
que1.push({dist1[n*n-1],n*n-1});
while(!que.empty()){
int v=que.top().second;
ll d=que.top().first;
que.pop();
if(d>dist[v]) continue;
for(auto e:G[v]){
if(chmin(dist[e.to],dist[v]+e.w)){
que.push({dist[e.to],e.to});
}
}
}
while(!que1.empty()){
int v=que1.top().second;
ll d=que1.top().first;
que1.pop();
if(d>dist1[v]) continue;
for(auto e:G[v]){
if(chmin(dist1[e.to],dist1[v]+e.w)){
que1.push({dist1[e.to],e.to});
}
}
}
ll mi=INF;
rep(i,n){
rep(j,n){
mi=min(mi,dist[i*n+j]+dist1[i*n+j]-2*A[i][j]);
}
}
cout<<mi<<endl;
return 0;
}
umezo