AOJ2620 Trodden Cable

Trodden Cable

Nathan O. Davis is running a company. His company owns a web service which has a lot of users. So his office is full of servers, routers and messy LAN cables. He is now very puzzling over the messy cables, because they are causing many kinds of problems. For example, staff working at the company often trip over a cable.

全く分からずに調べた.

http://acm-icpc.aitea.net/index.php?plugin=attach&refer=2013%2FPractice%2F%E5%A4%8F%E5%90%88%E5%AE%BF%2F%E8%AC%9B%E8%A9%95&openfile=trodden_cable.pdf:image=http://acm-icpc.aitea.net/index.php?plugin=attach&refer=2013%2FPractice%2F%E5%A4%8F%E5%90%88%E5%AE%BF%2F%E8%AC%9B%E8%A9%95&openfile=trodden_cable.pdf

この画像が非常に分かりやすい.移動する時にまたがる線のコストをして,移動が終わったグリッドグラフでdijkstra.

現在位置を(y, x)と置くと

  • の時,
  • の時,
  • の時,
  • の時,

した.

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#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;

int w, h, n;
int sx,sy,gx,gy;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

bool can(int y, int x) {
  if(0 <= y && y < h && 0 <= x && x < w) return true;
  return false;
}

struct edge {
  int from,to;
  ll 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[505*505 + 5];
ll d[505 * 505 + 5];

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() {
  cin >> w >> h >> n;

  cin >> sx >> sy >> gx >> gy;

  rep(i, h * w) {
      G[i].clear();
  }

  w++;
  h++;

  rep(i, h) {
      rep(j, w) {
          rep(k, 4) {
              int y = i + dy[k];
              int x = j + dx[k];

              if(can(y, x)) {
                  G[i*w + j].push_back(edge(y*w + x, 0));
              }
          }
      }
  }

  rep(i, n) {
      int y, x, t;
      cin >> x >> y >> t;

      string s;
      cin >> s;

      rep(k, t) {
          rep(j, s.size()) {
              int from = -1, to = -1;
              if(s[j] == 'R' && x + 1 < w - 1) {
                  from = y * w + (x + 1);
                  to = (y + 1) * w + (x + 1);
                  x++;
              } else if(s[j] == 'L' && 0 <= x - 1) {
                  from = y * w + x;
                  to = (y + 1) * w + x;
                  x--;
              } else if(s[j] == 'U' && 0 <= y - 1) {
                  from = y * w + x;
                  to = y * w + (x + 1);
                  y--;
              } else if(s[j] == 'D' && y + 1 < h - 1) {
                  from = (y + 1) * w + x;
                  to = (y + 1) * w + (x + 1);
                  y++;
              }

              if(from == -1 && to == -1) continue;
              rep(k, G[from].size()) {
                  if(G[from][k].to == to) {
                      G[from][k].cost++;
                  }
              }
              rep(k, G[to].size()) {
                  if(G[to][k].to == from) {
                      G[to][k].cost++;
                  }
              }
          }
      }
  }

  // rep(i, h) {
  //     rep(j, w) {
  //         cout << " --------- " << i << ", " << j << " :" << i * w + j << " |";
  //         rep(k, G[i*w + j].size()) {
  //             cout << "(" << G[i*w + j][k].to << ", " << G[i*w + j][k].cost << ") ";
  //         }
  //         cout << endl;
  //     }
  // }

  dijkstra(sy * w + sx, h * w);

  // rep(i, h) {
  //     rep(j, w) {
  //         cout << d[i*w + j] << " ";
  //     }
  //     cout << endl;
  // }

  cout << d[gy * w + gx] << endl;

  return 0;
}
Mar 22nd, 2016