結果

問題 No.807 umg tours
ユーザー peroonperoon
提出日時 2019-04-10 22:11:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 592 ms / 4,000 ms
コード長 5,412 bytes
コンパイル時間 1,454 ms
コンパイル使用メモリ 117,228 KB
実行使用メモリ 41,976 KB
最終ジャッジ日時 2024-05-02 23:46:56
合計ジャッジ時間 8,615 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 324 ms
34,500 KB
testcase_12 AC 304 ms
24,544 KB
testcase_13 AC 448 ms
33,628 KB
testcase_14 AC 158 ms
16,932 KB
testcase_15 AC 131 ms
14,004 KB
testcase_16 AC 471 ms
38,120 KB
testcase_17 AC 592 ms
41,288 KB
testcase_18 AC 583 ms
41,344 KB
testcase_19 AC 591 ms
41,716 KB
testcase_20 AC 284 ms
19,880 KB
testcase_21 AC 299 ms
20,624 KB
testcase_22 AC 122 ms
10,752 KB
testcase_23 AC 94 ms
9,216 KB
testcase_24 AC 273 ms
31,992 KB
testcase_25 AC 585 ms
41,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>
#include<complex>
#include<ctype.h>
#include<iomanip>
#include<iostream>
#include<fstream>
#include<map>
#include<math.h>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<stdio.h>
#include<string>
#include<string>
#include<vector>

using namespace std;
typedef long long ll;

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

const int N_MAX = 200010;

struct Edge{
    ll to = 0;
    ll cost = 0;
    Edge(ll to, ll cost): to(to), cost(cost) {}
    Edge(){}
};

struct Node{
    ll distance;
    ll index;
    bool ticket_used = false;
    Node(ll dist, ll ind){
        distance = dist;
        index = ind;
    }
    Node(){}

    bool operator<(const Node &another) const
    {
        return distance < another.distance;
    }
    bool operator>(const Node &another) const
    {
        return distance > another.distance;
    }
};

struct DijkstraBase{
    vector<ll> d; // 距離
    vector<vector<Edge> > graph;
    vector<bool> done;

    // ノード数を入れる
    void initialize(ll size){
        d.resize(size);
        done.resize(size);
        graph.resize(size);

        FOR(i, 0, size){
            d[i] = inf;
            done[i] = false;
        }
    }
    
    ll distance(int i){
        if(d.size()<=i) return -1;
        return d[i];
    }

    void print_graph(){
        FOR(i, 0, graph.size()){
            cout << i << " -> ";

            for(auto edge : graph[i]){
                cout << edge.to << " ";
            }
            cout << endl;
        }
    }

    void register_edge(ll a, ll b, ll cost){
        auto edge = Edge(b, cost);
        graph[a].push_back(edge);
    }

    void calc_shortest_path(){
        priority_queue<Node, vector<Node>, greater<Node> > que;
        auto node = Node(0, 0);
        que.push(node);

        while(!que.empty()){
            // 1番distanceが小さいノードを取り出す
            Node n = que.top();
            que.pop();

            if(done[n.index]) continue;
            
            done[n.index] = true;
            d[n.index] = n.distance;

            auto edge_list = graph[n.index];
            for(auto edge : edge_list){
                // 短くなるノードがあれば
                if(!done[edge.to] && n.distance + edge.cost < d[edge.to]){
                    ll shorter_distance = n.distance + edge.cost;
                    auto node = Node(shorter_distance, edge.to);
                    que.push(node);
                }
            }
        }
    }
};

struct Dijkstra : public DijkstraBase{
};

Dijkstra dij;

struct DijkstraTicket : public DijkstraBase{
    void calc_shortest_path(){
        ll N = graph.size();
        FOR(i, 0, N){
            d[i] = dij.distance(i);
        }

        priority_queue<Node, vector<Node>, greater<Node> > que;
        auto node = Node(0, 0);
        que.push(node);

        while(!que.empty()){
            Node n = que.top();
            que.pop();

            if(done[n.index]) continue;
            
            done[n.index] = true;
            d[n.index] = n.distance;

            auto edge_list = graph[n.index];
            for(auto edge : edge_list){
                ll dist0 = dij.distance(n.index) + 0; // 最後にチケットを使った
                ll dist1 = d[n.index] + edge.cost; // チケットを使ってiまで来て、最後の一歩は普通に

                if(!done[edge.to]){
                    auto node0 = Node(dist0, edge.to);
                    auto node1 = Node(dist1, edge.to);
                    que.push(node0);
                    que.push(node1);
                }


                // 短くなるノードがあれば
                // if(!done[edge.to] && n.distance + edge.cost < d[edge.to]){
                //     ll shorter_distance = n.distance + edge.cost;
                //     auto node = Node(shorter_distance, edge.to);
                //     que.push(node);
                // }
            }    
        }    

        // FOR(i, 0, N){
        //     // iまで普通に行って、そこでチケットを使う場合
        //     ll dist = dij.distance(i) + 0;
        //     auto edge_list = graph[i];
        //     for(auto edge : edge_list){
        //         d[edge.to] = min(d[edge.to], dist);
        //     }
        // }
    }
};

template < typename T >
void vprint(T &V){
	for(auto v : V){
    	cout << v << " ";
	}
	cout << endl;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N, M;
    cin >> N >> M;

    dij.initialize(N);

    DijkstraTicket dijT;
    dijT.initialize(N);

    FOR(i, 0, M){
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        dij.register_edge(a, b, c);
        dij.register_edge(b, a, c);

        dijT.register_edge(a, b, c);
        dijT.register_edge(b, a, c);
    }

    dij.calc_shortest_path();
    // dij.print_graph();
    // p(dij.distance(4));

    dijT.calc_shortest_path();
    // p(dijT.distance(4));

    // br();
    // p("answer");
    FOR(i, 0, N){
        ll ans = dij.distance(i) + dijT.distance(i);
        p(ans);
    }

    return 0;
}
0