結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ei1333333ei1333333
提出日時 2016-07-21 16:10:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,162 bytes
コンパイル時間 2,149 ms
コンパイル使用メモリ 169,556 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 20:21:12
合計ジャッジ時間 2,202 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 5 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 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 14 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:49:8: warning: use of an operand of type ‘bool’ in ‘operator++’ is deprecated [-Wdeprecated]
   49 |     if(first++) putchar(' ');
      |        ^~~~~
main.cpp:15:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 |   scanf("%d %d %d %d", &N, &M, &S, &G);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:18:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   18 |     scanf("%d %d %d", &a, &b, &c);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
typedef pair< int, int > Pi;
struct edge
{
  int to, cost;
};
 
vector< edge > graph[10000];
int N, M, S, G;
 
int main()
{  
  scanf("%d %d %d %d", &N, &M, &S, &G);
  while(M--) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    graph[a].push_back((edge){b, c});
    graph[b].push_back((edge){a, c});
  }
 
  priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  que.emplace(0, G);
  vector< Pi > v(N, Pi(INF, INF));
  v[G] = {0, -1};
  while(!que.empty()) {
    auto p = que.top(); que.pop();
    for(auto& e : graph[p.second]) {
      if(Pi(p.first + e.cost, p.second) >= v[e.to]) continue;
      v[e.to] = Pi(p.first + e.cost, p.second);
      que.emplace(v[e.to].first, e.to);
    }
  }
 
  vector< int > dd;
  int now = S;
  dd.push_back(S);
  while(now != G) {
    int net  = 1 << 30;
    for(auto& k : graph[now]) {
      if(v[k.to].first + k.cost == v[now].first) net = min(net, k.to);
    }
    now = net;
    dd.push_back(now);
  }
  bool first = false;
  for(int i = 0; i < dd.size(); i++) {
    if(first++) putchar(' ');
    printf("%d", dd[i]);
  }
  putchar('\n');
}
0