AcWing 1137. 选择最佳线路
分析
建立虚拟原点, 做一遍最短路算法
注意
多组数据读入, st
数组不用初始化, 因为spfa 最后所有点都会出队列, 都会变成false
循环队列 tt
最好设置成下一个位置, hh = 0, tt = 1
然后将第一个元素方到hh = 0
,
方便后后面循环队列
hh == N, hh = 0; tt == N; tt = 0
重置为0的操作
code
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1010, M = 21010, INF = 0x3f3f3f3f;
int h[N], e[M], w[M], ne[M], idx;
int n, m, T;
int q[N];
int dist[N];
bool st[N];
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void spfa(){
memset(dist, 0x3f, sizeof dist);
int hh = 0, tt = 1;
int scnt;
scanf("%d", &scnt);
while (scnt -- ){
int u;
scanf("%d", &u);
q[tt ++ ] = u;
dist[u] = 0;
st[u] = true;
}
while (hh != tt){
int t = q[hh ++ ];
if (hh == N) hh = 0;
st[t] = false;
for (int i = h[t]; ~i; i = ne[i]){
int j = e[i];
if (dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i];
if (!st[j]){
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
}
int main(){
while (scanf("%d%d%d", &n, &m, &T) != -1){
memset(h, -1, sizeof h);
idx = 0;
while (m -- ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
spfa();
if (dist[T] == INF) dist[T] = -1;
printf("%d\n", dist[T]);
}
return 0;
}
spfa,bfs, dijkstra, A*的判重数组含义的区别
spfa中的st数组: 当前元素是否在队列中
bfs中的st数组:当前元素是否被遍历过
dijkstra中的st数组:当前这个元素有没有出队
A*:当前点出队的时候也不能判重,只有终点第一次出队才是最小值, 第k次出队,第k小。
AcWing 1131. 拯救大兵瑞恩
分析
code
#include <iostream>
#include <cstring>
#include <deque>
#include <set>
using namespace std;
#define x first
#define y second
typedef pair<int, int> PII;
const int N = 11, M = 360, P = 1 << 10;
int n, m, k, p;
int h[N * N], e[M], w[M], ne[M], idx;
int g[N][N], key[N * N];// key数组表示 当前位置存储的二进制钥匙信息
// 注意key 存储的位置已经是一维的了
int dist[N * N][P]; // dist存的也是一维
bool st[N * N][P]; // st存的也是一维
set<PII> edges;
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void build(){
int dx[] = {
-1, 0, 1, 0}, dy[] = {
0, 1, 0, -1};
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
for (int u = 0; u < 4; u ++ ){
int x = i + dx[u], y = j + dy[u];
if (!x || x > n || !y || y > m) continue;
int a = g[i][j], b = g[x][y];
if (!edges.count({
a, b})) add(a, b, 0); // 建可以通的路
}
}
int bfs(){
memset(dist, 0x3f, sizeof dist);
dist[1][0] = 0;
deque<PII> q;
q.push_back({
1, 0});
while (q.size()){
PII t = q.front(); q.pop_front();
if (st[t.x][t.y]) continue;
st[t.x][t.y] = true;
if (t.x == n * m) return dist[t.x][t.y];
if (key[t.x]){
int state = t.y | key[t.x];
if (dist[t.x][state] > dist[t.x][t.y]){
// dist[t.x][t.y] -> dist[t.x][state]
// 如果后者能被前者更新, 就更新
dist[t.x][state] = dist[t.x][t.y];
q.push_front({
t.x, state});
}
}
for (int i = h[t.x]; ~i; i = ne[i]){
int j = e[i];
if (w[i] && !(t.y >> w[i] - 1 & 1)) continue; // 有门并且没有钥匙
if (dist[j][t.y] > dist[t.x][t.y] + 1){
dist[j][t.y] = dist[t.x][t.y] + 1;
q.push_back({
j, t.y});
}
}
}
return -1;
}
int main(){
cin >> n >> m >> p >> k;
for (int i = 1, t = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
g[i][j] = t ++; // 二维墙, 转化为点图, 方便计算距离
memset(h, -1, sizeof h);
while (k -- ){
// 注意是k --
int x1, y1, x2, y2, c;
cin >> x1 >> y1 >> x2 >> y2 >> c;
int a = g[x1][y1], b = g[x2][y2];
edges.insert({
a, b}), edges.insert({
b, a}); // 插入黑色墙
if (c) add(a, b, c), add(b, a, c); // 有颜色的们
}
build(); // 建可以通过的双向门
int s;
cin >> s;
while (s -- ){
int x, y, c;
cin >> x >> y >> c;
key[g[x][y]] |= 1 << c - 1; // 所有钥匙往左偏移1位
}
cout << bfs() << endl;
return 0;
}
图论最短路求方案数分析
AcWing 1134. 最短路计数
分析
此题只有0, 1权重的边, 因此bfs, 然后判断,
如果dist[j] > dist[t] + 1
, 那么说明dist[j]
可以被前驱更新, cnt[j] = cnt[t]
如果(else if), dist[j] == dist[t] + 1
, 说明有多个前驱, cnt[j] = (cnt[t] + cnt[j]) % mod
code
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 10, M = 4e5 + 10, mod = 100003;
int h[N], e[M], ne[M], idx;
int n, m;
int q[N];
int dist[N], cnt[N];
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void bfs(){
memset(dist, 0x3f, sizeof dist);
dist[1] = 0, cnt[1] = 1;
int hh = 0, tt = 0;
q[0] = 1;
while (hh <= tt){
int t = q[hh ++];
for (int i = h[t]; ~i; i = ne[i]){
int j = e[i];
if (dist[j] > dist[t] + 1){
dist[j] = dist[t] + 1;
cnt[j] = cnt[t];
q[ ++ tt] = j;
}else if (dist[j] == dist[t] + 1){
cnt[j] = (cnt[j] + cnt[t]) % mod;
}
}
}
}
int main(){
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
for (int i = 0; i < m; i ++ ){
int a, b;
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
bfs();
for (int i = 1; i <= n; i ++ )
printf("%d\n", cnt[i]);
return 0;
}
AcWing 383. 观光
分析
图片来自PeterBishop0题解
code
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1010, M = 100010;
struct Ver{
int id, type, dist;
bool operator > (const Ver& W) const{
return dist > W.dist;
}
};
int n, m, S, T;
int h[N], e[M], ne[M], w[M], idx;
int dist[N][2], cnt[N][2];
bool st[N][2];
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
int dijkstra(){
memset(st, 0, sizeof st);
memset(dist, 0x3f, sizeof dist);
memset(cnt, 0, sizeof cnt);
dist[S][0] = 0, cnt[S][0] = 1;
priority_queue<Ver, vector<Ver>, greater<Ver>> heap;
heap.push({
S, 0, 0});
while (heap.size()){
Ver t = heap.top();
heap.pop();
int ver = t.id, type = t.type, distance = t.dist, count = cnt[ver][type];
if (st[ver][type]) continue;
st[ver][type] = true;
for (int i = h[ver]; ~i; i = ne[i]){
int j = e[i];
if (dist[j][0] > distance + w[i]){
// 如果当前j点的最短边可以被更新
// 以前的最短边变成当前的次短边
dist[j][1] = dist[j][0], cnt[j][1] = cnt[j][0];
heap.push({
j, 1, dist[j][1]});
// 更新当前j点最短边
dist[j][0] = distance + w[i], cnt[j][0] = count;
heap.push({
j, 0, dist[j][0]});
}else if (dist[j][0] == distance + w[i]) cnt[j][0] += count; // 有多个最短边前驱
else if (dist[j][1] > distance + w[i]){
// 次短边可以被更新
dist[j][1] = distance + w[i], cnt[j][1] = count;
heap.push({
j, 1, dist[j][1]});
}else if (dist[j][1] == distance + w[i]) cnt[j][1] += count; // 次短边有多个前驱
}
}
int res = cnt[T][0];
if (dist[T][0] + 1 == dist[T][1]) res += cnt[T][1];
return res;
}
int main(){
int cases;
cin >> cases;
while (cases --){
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
idx = 0;
while (m -- ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
scanf("%d%d", &S, &T);
printf("%d\n", dijkstra());
}
return 0;
}