这章主要讲解单源最短路和其他算法结合
和dfs, 二分, dp, 拓扑排序的结合
AcWing 1135. 新年好
分析
最短路和dfs的结合
code
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 500010, M = 200010, INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
int h[N], e[M], w[M], ne[M], idx;
int source[6], dist[6][N];
int n, m;
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 dijkstra(int s, int dist[]){
memset(st, 0, sizeof st);
memset(dist, 0x3f, N * 4);
priority_queue<PII, vector<PII>, greater<PII>> heap;
dist[s] = 0;
heap.push({
0, s});
while (heap.size()){
auto t = heap.top(); heap.pop();
int ver = t.second, distance = t.first;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[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 dfs(int u, int start, int distance){
if (u > 5) return distance;
int res = INF;
for (int i = 1; i <= 5; i ++ )
if (!st[i]){
// 如果没有遍历过
int next = source[i];
st[i] = true;
res = min(res, dfs(u + 1, i, distance + dist[start][next]));
st[i] = false; // 恢复现场
}
return res;
}
int main(){
cin >> n >> m;
source[0] = 1; // 起点1 一定要初始化
for (int i = 1; i <= 5; i ++ ) {
int x;
cin >> x;
source[i] = x;
}
memset(h, -1, sizeof h);
for (int i = 1; i <= m; i ++ ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
for (int i = 0; i < 6; i ++ ) dijkstra(source[i], dist[i]);
memset(st, 0, sizeof st);
printf("%d\n", dfs(1, 0, 0)); // 1.已经遍历过几个点, 从start 开始
return 0;
}
AcWing 340. 通信线路
分析
根据题意, 可以发现我们在图中找第k大的边, 不过下面的分析思路有点太神奇了
二分求出: 从1走到N, 最少经过长度大于x的边的数量是否<= k,
可以发现如果x是答案, 那么经过x右边长度的边的数量一定满足,
经过x左边数量的边的数量, 一定不满足,
因为如果存在x1 < x且 经过大于x1的边的数量也<= k, 那么与x是答案,矛盾.
因此可以用二分
有了二分以后, 可以将所有边分类, 如果边的长度大于x, 则权值为1, 否则边权为0.
权值为0, 1的最短路问题可以用双端队列bfs求解
注意: 二分边界可以取到0, 砍掉所有边, 取到1e6 + 1, 可以用来区分图是否连通, 因为题目最大值x = 1e6, 如果最后二分返回1e6 + 1, 说明无解.
灰之魔女题解
code
#include <iostream>
#include <cstring>
#include <deque>
using namespace std;
const int N = 1010, M = 10010 * 2;
int n, m, k;
int h[N], e[M], w[M], ne[M], idx;
int st[N];
int dist[N];
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
bool check(int bound){
memset(st, 0, sizeof st);
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
deque<int> q;
q.push_back(1);
while (q.size()){
int t = q.front(); q.pop_front();
if (st[t]) continue;
st[t] = true;
for (int i = h[t]; ~i; i = ne[i]){
int j = e[i], v = w[i] > bound;
if (dist[j] > dist[t] + v){
dist[j] = dist[t] + v;
if (!v) q.push_front(j);
else q.push_back(j);
}
}
}
return dist[n] <= k;
}
int main(){
memset(h, -1, sizeof h);
cin >> n >> m >> k;
for (int i = 1; i <= m; i ++ ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
int l = 0, r = 1e6 + 1;
while (l < r){
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
if (r == 1e6 + 1) r = -1;
cout << r << endl;
return 0;
}
AcWing 342. 道路与航线
分析
code
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N = 25010, M = 50010 * 3, INF = 0x3f3f3f3f; // 道路双向 * 2 + 航线, 因此是3倍
#define x first
#define y second
typedef pair<int, int> PII;
int h[N], e[M], w[M], ne[M], idx;
int n, mr, mp, S;
int dist[N], din[N];
int id[N], bcnt; // id[i] 返回i点所在的连通块id
vector<int> block[N];
bool st[N];
queue<int> q;
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void dfs(int u, int bid){
id[u] = bid, block[bid].push_back(u);
for (int i = h[u]; ~i; i = ne[i]){
int j = e[i];
if (!id[j])
dfs(j, bid);
}
}
void dijkstra(int bid){
priority_queue<PII, vector<PII>, greater<PII>> heap;
for (auto u : block[bid])
heap.push({
dist[u], u});
while (heap.size()){
auto t = heap.top(); heap.pop();
int ver = t.y, distance = t.x;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; ~i; i = ne[i]){
int j = e[i];
if (id[j] != id[ver] && -- din[id[j]] == 0) q.push(id[j]); // q是全局变量, 表示团子队列
if (dist[j] > dist[ver] + w[i]){
dist[j] = dist[ver] + w[i];
if (id[j] == id[ver]) heap.push({
dist[j], j});
}
}
}
}
void topsort(){
memset(dist, 0x3f, sizeof dist);
dist[S] = 0;
for (int i = 1; i <= bcnt; i ++ )
if (!din[i])
q.push(i);
while (q.size()){
int t = q.front();
q.pop();
dijkstra(t);
}
}
int main(){
cin >> n >> mr >> mp >> S;
memset(h, -1, sizeof h);
while (mr -- ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
// flood-fill 找连通块
for (int i = 1; i <= n; i ++ )
if (!id[i]){
bcnt ++;
dfs(i, bcnt);
}
while (mp --){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
din[id[b]] ++;
add(a, b, c);
}
topsort();
for (int i = 1; i <= n; i ++ )
if (dist[i] > INF / 2) puts("NO PATH");
else cout << dist[i] << endl;
return 0;
}
AcWing 341. 最优贸易
分析
code
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 10, M = 500010 * 2;
int hs[N], ht[N], e[M], w[M], ne[M], idx;
int n, m;
bool st[N];
int dmin[N], dmax[N];
int q[N];
void add(int h[], int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void spfa(int h[], int dist[], int type){
int hh = 0, tt = 1;
if (type == 0){
memset(dist, 0x3f, sizeof dmin);
dist[1] = w[1];
q[0] = 1;
}else {
memset(dist, -0x3f, sizeof dmax);
dist[n] = w[n];
q[0] = n;
}
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 (type == 0 && dist[j] > min(dist[t], w[j]) || type == 1 && dist[j] < max(dist[t], w[j])){
if (type == 0) dist[j] = min(dist[t], w[j]);
else dist[j] = max(dist[t], w[j]);
if (!st[j]){
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
}
int main(){
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
memset(hs, -1, sizeof hs), memset(ht, -1, sizeof ht);
while (m -- ){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
// 只能写成 add ... ; if (c == 2) 的形式
// 如果写成 if (c == 1) ... ; else 会出错, 因此 c == 2是双向边, 上面这种写法, 少建了正向边
add(hs, a, b), add(ht, b, a);
if (c == 2) add(hs, b, a), add(ht, a, b);
}
spfa(hs, dmin, 0);
spfa(ht, dmax, 1);
int res = 0;
for (int i = 1; i <= n; i ++ ) res = max(res, dmax[i] - dmin[i]);
printf("%d\n", res);
return 0;
}