結果

問題 No.160 最短経路のうち辞書順最小
ユーザー hogeover30hogeover30
提出日時 2015-03-26 05:19:26
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,128 bytes
コンパイル時間 439 ms
コンパイル使用メモリ 64,352 KB
最終ジャッジ日時 2024-04-27 02:07:07
合計ジャッジ時間 893 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function ‘std::string dijkstra(int, int)’:
main.cpp:21:9: error: ‘tie’ was not declared in this scope
   21 |         tie(c, v)=q.top(); q.pop();
      |         ^~~
main.cpp:4:1: note: ‘std::tie’ is defined in header ‘<tuple>’; did you forget to ‘#include <tuple>’?
    3 | #include <queue>
  +++ |+#include <tuple>
    4 | #include <string>

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;

vector<int> G[210];
int cost[210][210];
int n;
const int inf=1e9;

string dijkstra(int s, int g)
{
    vector<int> d(n, inf);
    vector<unsigned int> p(n, -1u);
    priority_queue<pair<int, int>> q;
    d[g]=0;
    q.emplace(0, g);
    while (q.size()) {
        int c, v;
        tie(c, v)=q.top(); q.pop();
        if (d[v]<-c) continue;
        for(int w: G[v]) {
            if (d[w]>d[v]+cost[v][w] or d[w]==d[v]+cost[v][w] and p[w]>v) {
                d[w]=d[v]+cost[v][w];
                q.emplace(-d[w], w);
                p[w]=v;
            }
        }
    }

    string res;
    while (s!=g) {
        res+=to_string(s)+' ';
        s=p[s];
    }
    res+=to_string(g);
    return res;
}

int main()
{
    int m, s, g;
    while (cin>>n>>m>>s>>g) {
        for(int i=0;i<n;++i) G[i].clear();
        for(int i=0;i<m;++i) {
            int a, b, c; cin>>a>>b>>c;
            G[a].push_back(b);
            G[b].push_back(a);
            cost[a][b]=cost[b][a]=c;
        }
        cout<<dijkstra(s, g)<<endl;
    }
}
0