mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-23 20:48:48 +00:00
P4667 [BalticOI 2011 Day1]Switch the Lamp On
https://www.luogu.com.cn/record/93519277
This commit is contained in:
parent
de099e673b
commit
e9724ddf1e
76
Luogu/P4667/P4667.cpp
Normal file
76
Luogu/P4667/P4667.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 505;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
|
||||
int n, m, dist[N * N];
|
||||
std::vector<std::pair<int, int>> g[N * N];
|
||||
bool vis[N * N];
|
||||
|
||||
int id(int x, int y) {
|
||||
return x * (m + 1) + y;
|
||||
}
|
||||
|
||||
void dijkstra() {
|
||||
std::fill_n(dist, N * N, INF);
|
||||
|
||||
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> q;
|
||||
|
||||
q.emplace(0, id(0, 0));
|
||||
dist[id(0, 0)] = 0;
|
||||
|
||||
while (!q.empty()) {
|
||||
int u = q.top().second;
|
||||
q.pop();
|
||||
|
||||
if (vis[u]) continue;
|
||||
vis[u] = true;
|
||||
|
||||
for (auto e : g[u]) {
|
||||
int v = e.first,
|
||||
w = e.second;
|
||||
|
||||
if (dist[v] > dist[u] + w) {
|
||||
dist[v] = dist[u] + w;
|
||||
q.emplace(dist[v], v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n >> m;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
char c;
|
||||
|
||||
cin >> c;
|
||||
|
||||
g[id(i, j)].emplace_back(id(i - 1, j - 1), c != '\\');
|
||||
g[id(i - 1, j - 1)].emplace_back(id(i, j), c != '\\');
|
||||
g[id(i, j - 1)].emplace_back(id(i - 1, j), c != '/');
|
||||
g[id(i - 1, j)].emplace_back(id(i, j - 1), c != '/');
|
||||
}
|
||||
}
|
||||
|
||||
dijkstra();
|
||||
|
||||
if (dist[id(n, m)] == INF) {
|
||||
cout << "NO SOLUTION" << endl;
|
||||
} else {
|
||||
cout << dist[id(n, m)] << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user