結果
問題 | No.2565 はじめてのおつかい |
ユーザー |
|
提出日時 | 2023-12-02 15:18:36 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 169 ms / 2,000 ms |
コード長 | 1,197 bytes |
コンパイル時間 | 2,369 ms |
コンパイル使用メモリ | 207,924 KB |
最終ジャッジ日時 | 2025-02-18 04:25:48 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 50 |
ソースコード
#include <bits/stdc++.h>using namespace std;int main(){ios_base::sync_with_stdio(false);cin.tie(nullptr);int N,M; cin >> N >> M;vector<vector<pair<int,int>>> Graph(4*N);for(int i=0; i<M; i++){int u,v; cin >> u >> v;u--; v--;for(int k=0; k<4; k++){Graph.at(u).push_back({v,1});u += N, v += N;}}Graph.at(N-2).push_back({N-2+N,0});Graph.at(N-2+2*N).push_back({N-2+3*N,0});Graph.at(N-1).push_back({N-1+2*N,0});Graph.at(N-1+N).push_back({N-1+3*N,0});vector<bool> already(4*N);vector<long long> dist(4*N,2e18);priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> Q;Q.push({0,0}); dist.at(0) = 0;while(Q.size()){auto[pdist,pos] = Q.top(); Q.pop();if(already.at(pos)) continue;already.at(pos) = true;for(auto [to,cost] : Graph.at(pos)){if(dist.at(to) > pdist+cost){dist.at(to) = pdist+cost;Q.push({dist.at(to),to});}}}if(dist.at(3*N) == 2e18) dist.at(3*N) = -1;cout << dist.at(3*N) << endl;}