跳到正文

个人记录

LZM_最短路

King_tissue约 17 分钟
原文

这是 2026年7月17日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年7月17日
更新
2026年7月20日
篇幅
约 17 分钟

版本

  1. v2当前
  2. v1

最短路摸底

我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼我是傻逼😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬🤬

🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧咕咕嘎嘎🐧

T1

操你妈我真的气笑了。

错因这题是有向边。

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, s, ju[100005];
struct node {
	int id, ju;
	bool operator < (const node A) const {
		return ju > A.ju;
	}
};
vector<node> g[100005];
priority_queue<node> pq;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0); 
	cin >> n >> m >> s;
	for (int i = 1; i <= m; i++) {
		int u, v, w; cin >> u >> v >> w;
		g[u].push_back({v, w});
//		g[v].push_back({u, w});
	}
	memset(ju, 0x7f, sizeof(ju));
	pq.push({s, 0});
	while(!pq.empty()) {
		node t = pq.top(); pq.pop();
		if (t.ju >= ju[t.id]) continue;
		ju[t.id] = t.ju;
		for (int i = 0; i < g[t.id].size(); i++) {
			pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju});
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << ju[i] << " ";
	}
	return 0;
}

T2

我不会写 Spfa 😭😱🥵
我不会写 Spfa 😭😱🥵
我不会写 Spfa 😭😱🥵
我不会写 Spfa 😭😱🥵
我不会写 Spfa 😭😱🥵
我不会写 Spfa 😭😱🥵
我不会写 Spfa 😭😱🥵

dijkstra:

  1. 贪心思想;
  2. 不能应用于负权图;
  3. 堆优化的时间复杂度 ( O(M cdot log N) );
  4. 算法原理:
    将 ( n ) 个点分成已经找到最短路的点(黑色),尚未找到最短路的点(白色);
    维护 ( dis[i] ) 表示起点到点 ( i ) 的最短路径;
    迭代 ( n ) 轮,每一轮在白点找 ( s ) 距离最近的点 ( x ),染成黑色,( vis[x] = true );
    以 ( x ) 为中转点,松弛 ( x ) 的所有邻接点 ( y );

spfa本质是bellman-ford算法的队列优化:

  1. 以边为研究对象的算法;
  2. 时间复杂度 ( O(m cdot n) );
  3. 算法原理:
    任何一条起点到点 ( i ) 的最短路径至多包含 ( n-1 ) 条边;
    迭代 ( n-1 ) 轮,每一轮枚举 ( m ) 条边,检查是否应该在最短路上,也即松弛操作;
    队列优化是:只有一条边 ( (x, y, w) ) 松弛成功,( y ) 才应该去松弛别人,( y ) 要进队;
  4. 维护的 ( vis[x] ) 表示 ( x ) 是否在队列中,出队取消标记,进队打标记;
  5. 使用场景:有负权的图,找负环;
code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, ju[100005];
int cnt[100005], dis[100005];
struct node {
	int id, ju;
};
vector<node> g[100005];
bool vis[100005];
queue<int> q;
bool spfa() {
	memset(dis, 0x7f, sizeof(ju));
    memset(cnt, 0, sizeof(cnt));
    memset(vis, 0, sizeof(vis));
    while(!q.empty()) q.pop();
    q.push(1);
    dis[1] = 0; vis[1] = 1, cnt[1] = 1;
    while(!q.empty()) {
    	int x = q.front(); q.pop();
    	vis[x] = 0;
    	for (int i = 0; i < g[x].size(); i++) {
    		int y = g[x][i].id, z = g[x][i].ju;
    		if (dis[x] + z < dis[y]) {
    			dis[y] = dis[x] + z;
    			if (cnt[y] >= n) return 1;
    			if (!vis[y]) {
    				vis[y] = 1;
    				cnt[y] = cnt[x] + 1;
    				q.push(y);
    			}
			}
		}
	}
	return 0;
}
void solve() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) g[i].clear();
	for (int i = 1; i <= m; i++) {
		int u, v, w; cin >> u >> v >> w;
		g[u].push_back({v, w});
		if (w >= 0) g[v].push_back({u, w});
	}
	cout << (spfa() ? "YESn" : "NOn");
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0); 
	int t; cin >> t; while (t--) solve();
	return 0;
}

T3

依旧单向边建双边气笑了。

floyd 传递闭包确定拥有道具后的新图。(2k2^k 的边等同于新图中 11 的边,具体见代码)

然后再跑一次 floyd。

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, s, dis[105][105];
bool g[105][105][105];
struct node {
	int id, ju;
	bool operator < (const node A) const {
		return ju > A.ju;
	}
};
priority_queue<node> pq;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0); 
	cin >> n >> m;
	memset(dis, 0x3f, sizeof(dis));
	for (int i = 1; i <= m; i++) {
		int u, v; cin >> u >> v;
		g[0][u][v] = 1;
		 g[0][v][u] = 1;
		dis[u][v] = 1;
	}
	for (int k = 1; k <= 64; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				for (int p = 1; p <= n; p++) {
					if (g[k - 1][i][p] && g[k - 1][p][j]) {
						g[k][i][j] = 1; dis[i][j] = 1;
					}
				}
			}
		}
	}
//	for (int i = 1; i <= n; i++) dis[i][i] = 0;
	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
			}
		}
	}
	cout << dis[1][n] << "\n";
	return 0;
}

T4

老师没讲我就不写吗?。

暴力思路纯板子大概贴个代码。

wrong code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, s, ju[100005];
struct cun {
	int t, id;
} c[100005];
bool f[100005];
struct node {
	int id, ju;
	bool operator < (const node A) const {
		return ju > A.ju;
	}
};
bool cmp (cun qwe, cun ewq) {
	return qwe.t < ewq.t;
}
vector<node> g[100005];
priority_queue<node> pq;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0); 
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		c[i].id = i;
		cin >> c[i].t;
	}
	int qi = 0;
	sort(c + 1, c + n + 1, cmp);
	for (int i = 1; i <= m; i++) {
		int u, v, w; cin >> u >> v >> w;
		u++; v++;
		g[u].push_back({v, w});
		g[v].push_back({u, w});
	}
	int q; cin >> q;
//	cout << q << "\n";
	while(q--) {
		memset(ju, 0x7f, sizeof(ju));
		int x, y, h; cin >> x >> y >> h;
		pq.push({x + 1, 0});
		x ++; y++;
		while(c[qi + 1].t <= h && qi <= n) {
			f[c[++qi].id] = 1;
		}
		while(!pq.empty()) {
			node t = pq.top(); pq.pop();
			if (t.ju >= ju[t.id]) continue;
			if (!f[t.id]) continue;
			ju[t.id] = t.ju;
			for (int i = 0; i < g[t.id].size(); i++) {
				pq.push({g[t.id][i].id, g[t.id][i].ju + t.ju});
			}
		}
		cout << (ju[y] > 1e9 ? -1 : ju[y]) << "\n";
	}
	return 0;
}

正解在于并非在每一个时刻,每一个节点都可以到达,所以应枚举目前所有可以到达的节点k,并以k为中转点进行更新。

成功优化掉一个 mm。

code
C++
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt, d[205], g[205][205];
int main(){
	memset(g, 0x3f, sizeof(g));
	cin >> n >> m;
	for(int i = 0; i < n; i++){
		cin >> d[i];
	}
	for(int i = 0; i < n; i++){
		g[i][i] = 0;
	}
	for(int i = 1; i <= m; i++){
		int a, b, c; cin >> a >> b >> c;
		g[a][b] = g[b][a] = c;
	}
	int q; cin >> q;
	while(q--){
		int a, b, c; cin >> a >> b >> c;
		while(d[cnt] <= c && cnt < n){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(g[i][j] > g[i][cnt] + g[j][cnt]){
						g[i][j] = g[j][i] = g[i][cnt] + g[j][cnt];
					}
				}
			}
			cnt++;
		}
		if(d[a] > c || d[b] > c){
			cout << "-1\n";
		} else {
			if (g[a][b] > 1e9) cout << "-1\n";
			else cout << g[a][b] << "\n";
		}
	}
	return 0;
}

T5

  1. 暴力枚举k个点分别作为起点跑dijkstra可以找到答案,
    时间O(k * m * logm);

  2. 考虑一对可能得到最短路径最小值候选特殊点对(u, v),u到v会经过
    原图中若干条边,设其中有一条是(x, y, w);

  3. u应该是能到达x的特殊点中最近的,从y出发能到达的特殊点v也是最近的;

  4. 对于正图g[i],以k个特殊点为起点跑“多源”dijkstra,存储dis1[i]表示从最近的
    特殊点到起点i的距离;

  5. 对于反图g2[i],以k个特殊点为起点跑“多源”dijkstra,存储dis2[i]表示能到达
    的最近的特殊点的距离;

  6. 枚举m条边,对于(x, y, w),dis1[x] + w + dis2[y]的路径表示对应的特殊点对(u, v)
    的最短路径一定经过(x, y, w),则答案ans = min(ans, dis1[x] + w + dis2[y]);

  7. 维护color1[i]和color2[i]表示具体是哪个特殊点到i最近,和i到哪个特殊点最近;

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int n, m, k, ju[N];
bool vis[N];
int dist1[N], dist2[N], color1[N], color2[N];
struct cutezrr {
	int u, v, w;
} a[N];
struct node {
	int id, ju;
	bool operator < (const node A) const {
		return ju > A.ju;
	}
};
vector<node> g1[N], g2[N];
void dijk (vector<node> g[], int d[], int color[]) {
	priority_queue<node> pq;
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; i++) {
        if (color[i] != 0) pq.push({i, 0});
	}
    while (!pq.empty()) {
        node u = pq.top(); pq.pop();
        if (vis[u.id]) continue;
        vis[u.id] = 1;
        for (int i = 0; i < g[u.id].size(); i++) {
            int v = g[u.id][i].id, w = g[u.id][i].ju;
            if (d[v] > d[u.id] + w) {
                d[v] = d[u.id] + w;
                color[v] = color[u.id]; 
                pq.push({v, d[v]});
            }
        }
    }
    return ; 
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0); 
	int t; cin >> t; while(t--) {
		cin >> n >> m >> k;
	    for (int i = 1; i <= n; i++) {
	        g1[i].clear(); g2[i].clear();
	        dist1[i] = dist2[i] = 1e18;
	        color1[i] = color2[i] = 0;
	    }
		for (int i = 1; i <= m; i++) {
			int u, v, w; cin >> u >> v >> w;
			g1[u].push_back({v, w});
			g2[v].push_back({u, w});
			a[i] = {u, v, w};
		}
		for (int i = 1; i <= k; i++) {
			int h; cin >> h;
			dist1[h] = dist2[h] = 0;
			color1[h] = color2[h] = h;
		}
		dijk(g1, dist1, color1);
	    dijk(g2, dist2, color2);
	    int ans = 1e18;
	    for (int i = 1; i <= m; i++) {
	        int u = a[i].u, v = a[i].v, w = a[i].w;
	        if (color1[u] != 0 && color2[v] != 0 && color1[u] != color2[v]) {
	            ans = min(ans, dist1[u] + w + dist2[v]);
	        }
	    }
	    cout << ans << "\n";
	}
	return 0;
}