結果
| 問題 |
No.1995 CHIKA Road
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-09-03 14:41:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,042 ms / 2,000 ms |
| コード長 | 1,761 bytes |
| コンパイル時間 | 2,038 ms |
| コンパイル使用メモリ | 154,952 KB |
| 最終ジャッジ日時 | 2025-02-07 02:19:28 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#include <sstream>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
int main(){
int N,M; cin >> N >> M;
set<int> st;
st.insert(1);
st.insert(N);
map<int,vector<int>> G;
map<int,vector<ll>> CO;
rep(i,M){
int a,b; cin >> a >> b;
G[a].pb(b);
CO[a].pb(2*b-2*a-1);
st.insert(a);
st.insert(b);
}
vector<int> vst;
for(auto s:st){
vst.pb(s);
}
rep(i,sz(vst)-1){
int a = vst[i];
int b = vst[i+1];
G[a].pb(b);
CO[a].pb(2*(b-a));
}
map<int,ll> D;
set<int> used;
priority_queue<pair<ll,int>,
vector<pair<ll,int>>,
greater<pair<ll,int>>> q;
q.push({(ll)0,1});
while(!q.empty()){
ll d = q.top().first; int to = q.top().second; q.pop();
if(used.count(to)) continue;
used.insert(to); D[to] = d;
rep(i,sz(G[to])){
int nxt=G[to][i];
if(D.count(nxt) && D[nxt] <= D[to] + CO[to][i]) continue;
q.push({D[to] + CO[to][i], nxt});
}
}
cout << D[N] << endl;
return 0;
}