結果
| 問題 |
No.1283 Extra Fee
|
| コンテスト | |
| ユーザー |
沙耶花
|
| 提出日時 | 2020-11-06 21:53:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 414 ms / 2,000 ms |
| コード長 | 1,329 bytes |
| コンパイル時間 | 2,411 ms |
| コンパイル使用メモリ | 211,448 KB |
| 最終ジャッジ日時 | 2025-01-15 20:35:23 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 10000000000000000
int main(){
int N,M;
cin>>N>>M;
vector<vector<long long>> cost(N,vector<long long>(N,0));
rep(i,M){
long long h,w,c;
cin>>h>>w>>c;
cost[h-1][w-1] = c;
}
vector dis(N,vector(N,vector<long long>(2,Inf)));
dis[0][0][0] = 0;
priority_queue<pair<long long,vector<long long>>,vector<pair<long long,vector<long long>>>,greater<pair<long long,vector<long long>>>> Q;
Q.emplace(0,vector<long long>{0,0,0});
vector<int> dx = {0,1,0,-1},dy = {1,0,-1,0};
while(Q.size()>0){
long long D = Q.top().first;
int y = Q.top().second[0];
int x = Q.top().second[1];
int used = Q.top().second[2];
Q.pop();
if(dis[y][x][used]!=D)continue;
rep(i,4){
int yy = y+dy[i],xx = x+dx[i];
if(yy<0||yy>=N||xx<0||xx>=N)continue;
long long c = 1;
if(used==0){
if(dis[yy][xx][1]>dis[y][x][0]+c){
dis[yy][xx][1] = dis[y][x][0]+c;
Q.emplace(dis[yy][xx][1],vector<long long>{yy,xx,1});
}
}
c += cost[yy][xx];
if(dis[yy][xx][used]>dis[y][x][used]+c){
dis[yy][xx][used] = dis[y][x][used] + c;
Q.emplace(dis[yy][xx][used],vector<long long>{yy,xx,used});
}
}
}
cout<<dis.back().back().back()<<endl;
return 0;
}
沙耶花