AOJ0596 Taxis

Taxis | Aizu Online Judge

Introduction to Programming Introduction to Algorithms and Data Structures Library of Data Structures Library of Graph Algorithms Library of Computational Geometry Library of Dynamic Programming Library of Number Theory

タクシーの移動しか使わないのでタクシーの移動の場合のグラフを作り直す.まず連結している町の辺のコストをにする.
頂点からdijkstraして以下の場合に,にコストの辺を張る.
新しいグラフを構築したらそのグラフでdijkstra.

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#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 1<<30
#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;
  int cost;

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

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

vector<edge> G[5005];
vector<int> g[5005];
int C[5005], R[5005], d[5005];

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;

  memset(C, 0, sizeof(C));
  memset(R, 0, sizeof(R));
  rep(i, n) cin >> C[i] >> R[i];
  rep(i, m) {
      int s, t;
      cin >> s >> t;

      s--;
      t--;

      g[s].push_back(t);
      g[t].push_back(s);
  }

  rep(i, n) {
      fill(d, d + n, INF);

      priority_queue<P, vector<P>, greater<P> > que;
      que.push(mp(0, i));

      d[i] = 0;

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

          int v = p.second;

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

          rep(i, g[v].size()) {
              if(d[g[v][i]] > d[v] + 1) {
                  d[g[v][i]] = d[v] + 1;
                  que.push(mp(d[v] + 1, g[v][i]));
              }
          }
      }

      rep(j, n) {
          if(i == j) continue;
          if(d[j] <= R[i]) {
              G[i].push_back(edge(j, C[i]));
          }
      }
  }

  dijkstra(0, n);

  cout << d[n-1] << endl;

  return 0;
}
Mar 6th, 2016