Day19
今天开始进入搜索,速度提上来!
求所有0到最近的1,反过来想就是用1去更新到0的每一个距离,因为有多个1,所以就是多源BFS。
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 1010;
char f[N][N];
int g[N][N];
bool st[N][N];
int n, m;
queue<pair<int, int>> q;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
void bfs()
{
while (q.size()) {
auto b = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = b.x + dx[i];
int ny = b.y + dy[i];
if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) continue;
if (!st[nx][ny]) {
st[nx][ny] = true;
g[nx][ny] = g[b.x][b.y] + 1;
q.push({nx, ny});
}
}
}
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> f[i][j];
if (f[i][j] == '1') {
st[i][j] = true;
q.push({i, j});
}
}
}
bfs();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << g[i][j] << " ";
}
cout << endl ;
}
return 0;
}
类似于用BFS求最短路,如果不需要转动就可以走,那么边权就是0,如果需要转动,那就是1,我们把边权为0的放到双端队列头部,把边权为1的放到双端队列尾部,每次都是从头弹出,这样就相当于每次都是找最优的路径像Dijkstra一样,最后得出最短路径即可。
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 510;
int n, m;
char g[N][N];
int dist[N][N];
bool st[N][N];
int bfs()
{
deque<PII> q;
memset(st, 0, sizeof st);
memset(dist, 0x3f, sizeof dist);
char cs[5] = "\\/\\/";
int dx[] = {-1, -1, 1, 1}, dy[] = {-1, 1, 1, -1};
int ix[] = {-1, -1, 0, 0}, iy[] = {-1, 0, 0, -1};
dist[0][0] = 0;
q.push_back({0, 0});
while (q.size())
{
auto t = q.front();
q.pop_front();
int x = t.x, y = t.y;
if (x == n && y == m) return dist[x][y];
if (st[x][y]) continue;
st[x][y] = true;
for (int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a > n || b < 0 || b > m) continue;
int ga = x + ix[i], gb = y + iy[i];
int w = (g[ga][gb] != cs[i]);
int d = dist[x][y] + w;
if (d <= dist[a][b])
{
dist[a][b] = d;
if (!w) q.push_front({a, b});
else q.push_back({a, b});
}
}
}
return -1;
}
int main(void)
{
int t;
cin >> t;
while (t--) {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> g[i];
if (n + m & 1) puts("NO SOLUTION");
else cout << bfs() << endl ;
}
return 0;
}
Astar算法
在最短路问题中,如果所有边权都是非负的,那么就可以用启发函数优化BFS的过程。Dijkstra算法是找到从某个点到其他所有点的最短距离,但如果只需要找从某个点到另一个点的最短距离,就可以用Astar算法,这个算法支持的点数更多。
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
const int N = 1010, M = 200010;
int n, m, S, T, K;
int h[N], rh[N], e[M], w[M], ne[M], idx;
int dist[N], cnt[N];
bool st[N];
void add(int h[], int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void dijkstra()
{
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, T});
memset(dist, 0x3f, sizeof dist);
dist[T] = 0;
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.y;
if (st[ver]) continue;
st[ver] = true;
for (int i = rh[ver]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[ver] + w[i])
{
dist[j] = dist[ver] + w[i];
heap.push({dist[j], j});
}
}
}
}
int A_star()
{
priority_queue<PIII, vector<PIII>, greater<PIII>> heap;
heap.push({dist[S], {0, S}});
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.y.y, distance = t.y.x;
cnt[ver] ++ ;
if (cnt[T] == K) return distance;
for (int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if (cnt[j] < K)heap.push({distance + w[i] + dist[j], {distance + w[i], j}});
}
}
return -1;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
memset(rh, -1, sizeof rh);
for (int i = 0; i < m; i ++ )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(h, a, b, c), add(rh, b, a, c);
}
scanf("%d%d%d", &S, &T, &K);
if (S == T) K ++ ;
dijkstra();
cout << A_star() << endl;
return 0;
}