ABC012D バスと割けられない運命

D: バスと避けられない運命 - AtCoder Beginner Contest 012 | AtCoder

(null)

バス停の数と小さいので,愚直にそこを始点とした最短経路を求め,最大値の最小値を取った.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1LL<<60
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

struct edge {
  int from,to;
  ll cost;

  edge(int t, ll c) : to(t),cost(c) {}
  edge(int f, int t, ll c) : from(f),to(t),cost(c) {}

  bool operator<(const edge &e) const {
      return cost < e.cost;
  }
};

vector<edge> G[305];
ll d[305];

void dijkstra(int s, int n) {
  priority_queue<P, vector<P>, greater<P> > que;
  fill(d, d+n, INF);

  d[s] = 0;
  que.push(P(0,s));

  while(que.size()) {
      P p = que.top();
      que.pop();

      int v = p.second;
      if(d[v] < p.first) continue;

      rep(i, G[v].size()) {
          edge e = G[v][i];
          if(d[e.to] > d[v] + e.cost) {
              d[e.to] = d[v] + e.cost;
              que.push(P(d[e.to],e.to));
          }
      }
  }
}


int main() {
  int n, m;
  cin >> n >> m;

  rep(i, m) {
      ll s, t, c;
      cin >> s >> t >> c;
      s--; t--;

      G[s].push_back(edge(t, c));
      G[t].push_back(edge(s, c));
  }

  ll ans = INF;
  rep(i, n) {
      dijkstra(i, n);
      ll res = 0;
      rep(j, n) {
          res = max(res, d[j]);
      }

      ans = min(ans, res);
  }

  cout << ans << endl;

  return 0;
}
Apr 3rd, 2016