結果

問題 No.807 umg tours
ユーザー petite_progpetite_prog
提出日時 2020-04-14 02:50:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 320 ms / 4,000 ms
コード長 3,354 bytes
コンパイル時間 1,738 ms
コンパイル使用メモリ 193,136 KB
実行使用メモリ 35,236 KB
最終ジャッジ日時 2023-08-15 12:45:16
合計ジャッジ時間 6,467 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 134 ms
18,552 KB
testcase_12 AC 156 ms
21,248 KB
testcase_13 AC 212 ms
24,920 KB
testcase_14 AC 83 ms
13,596 KB
testcase_15 AC 57 ms
11,492 KB
testcase_16 AC 221 ms
25,816 KB
testcase_17 AC 312 ms
34,636 KB
testcase_18 AC 316 ms
34,456 KB
testcase_19 AC 320 ms
31,184 KB
testcase_20 AC 179 ms
22,620 KB
testcase_21 AC 207 ms
23,360 KB
testcase_22 AC 66 ms
12,016 KB
testcase_23 AC 43 ms
10,044 KB
testcase_24 AC 115 ms
32,084 KB
testcase_25 AC 318 ms
35,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
   ∫ ∫ ∫
   ノヽ
  (_  )
 (_    )
(______ )
 ヽ(´・ω・)ノ 
   |  /
   UU
*/
#pragma region macro
#include <bits/stdc++.h>
typedef long long int64;
using namespace std;
using P = pair<int64, int64>;
typedef vector<int> vi;
const int MOD = (int)1e9 + 7;
const int64 INF = 1LL << 62;
const int inf = 1<<30;
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; }
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = s; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end() //コンテナじゃないと使えない!!
#define debug(x) cerr << #x << ": " << x << "\n";
#define mp make_pair
#define bn '\n'
template <typename T>
ostream& operator<<(ostream& os, const vector<T> &V){
    int N = V.size();
    REP(i,N){
        os << V[i];
        if (i!=N-1) os << " ";
    }
    os << "\n";
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, pair<T,S> const&P){
    os << "(";
    os << P.first;
    os << " , ";
    os << P.second;
    os << ")";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> &S){
    auto it=S.begin();
    while(it!=S.end()){
        os << *it;
        os << " ";
        it++;
    }
    os << "\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T> &q){
    for(auto it=q.begin();it<q.end();it++){
        os<<*it;
        os<<" ";
    }
    os<<endl;
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, map<T,S> const&M){
    for(auto e:M){
        os<<e;
    }
    os<<endl;
    return os;
}
vector<pair<int,int>> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)};
#pragma endregion
//fixed<<setprecision(10)<<ans<<endl;

vector<vector<int64>> dijkstra(vector<vector<pair<int64,int64>>> const& edge, int64 start){
    int64 N = edge.size();
    vector<vector<int64>> dist(N,vector<int64>(2,INF));
    priority_queue<tuple<int64,int64,int64>,vector<tuple<int64,int64,int64>>, greater<tuple<int64,int64,int64>> > q; //距離、頂点、特権使用回数
    q.push(make_tuple(0, start, 0));
    vector<vector<bool>> visited(N,vector<bool>(2,false) );

    int64 from,d,skip_cnt;
    int64 to,cost;
    while(!q.empty()){
        tie(d,from,skip_cnt) = q.top(); q.pop();
        if (visited[from][skip_cnt]) continue;
        visited[from][skip_cnt] = true;
        dist[from][skip_cnt] = d;
        for(auto e:edge[from]){
            tie(to,cost) = e;
            if (visited[to][skip_cnt]) continue;
            if(chmin(dist[to][skip_cnt], int64(d+cost)))
                q.push(make_tuple(d+cost, to, skip_cnt));
            if(skip_cnt==0 and chmin(dist[to][skip_cnt+1], d))
                q.push(make_tuple(d, to, skip_cnt+1));
        }
    }

    return dist;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N,M;
    cin >> N >> M;

    vector<vector<pair<int64,int64>>> edge(N);
    int64 a,b,c;
    REP(i,M){
        cin >> a >> b >> c;
        edge[--a].emplace_back(--b,c);
        edge[b].emplace_back(a,c);
    }

    auto dist = dijkstra(edge,0);
    dist[0][1] = 0;
    REP(i,N){
        cout << dist[i][0] + dist[i][1] << bn;
    }
}
0