一、最短路
1.dijkstra算法 单源最短路
贪心(每次找最近点进行拓展)+ dp(选取最优方案)
先选取出发点st,拓展该点的所有出点,并更新出点的权值(即到st点的距离),再拓展所有出点中权值最小的出点,每个点只拓展一次
P1073 - 【模板】最短路(1) - ETOJ (eriktse.com)
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef double db;
const int N = 1e5 + 10;
struct edge {
int x, w; //x出点,w权重
};
vector<edge> g[N];
ll d[N], n, m;
void dijkstra(int st) {
memset(d, 0x3f, sizeof(ll) * (n + 1));
d[st] = 0;
bitset<N> vis;
for (int i = 1; i <= n; i++) {
int u = 1;
for (int j = 1; j <= n; j++) {
//去过u了就换点||没去过j&&j到源点的距离更短,就更新u
if (vis[u] || (!vis[j] && d[j] < d[u])) u = j;
}
//表示拓展过u了
vis[u] = true;
//此时d[u]已为所有未拓展点中距离最小的那个
for (auto &[v, w] : g[u]) {
//没拓展过v&&v到源点的距离比u的距离加上u到v的距离大,就更新d[v]
if (!vis[v] && d[v] > d[u] + w)d[v] = d[u] + w;
}
}
}
int main(){
qio
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w; cin >> u >> v >> w;
if (u != v) g[u].push_back({v, w});
}
dijkstra(1);
//d[n]的距离如果为无穷则无最短路,否则输出最短路
cout << (d[n] >= 0x3f3f3f3f3f3f ? -1 : d[n]) << '\n';
}
P1078 - 【模板】最短路(2) - ETOJ (eriktse.com)数据加强版
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef double db;
const int N = 2e5 + 10;
struct edge {
int x, w; //x出点,w权重
//按照w小的优先
bool operator < (const edge &u) const {
//如果两个的w相等则比较x(遍历时从小开始?),否则比较w
//pq默认通过小于号得到大根堆,所以此处w符号相反
return w == u.w ? x < u.x : w > u.w;
}
};
vector<edge> g[N];
ll d[N], n, m;
void dijkstra(int st) {
memset(d, 0x3f, sizeof(ll) * (n + 1));
d[st] = 0; //初始化起点
bitset<N> vis;//表示已经拓展过
priority_queue<edge> pq;
pq.push({st, d[st]}); //将起点作为拓展点
while (pq.size()) { //只要队列里还有点等待拓展
int x = pq.top().x;
pq.pop();
if (vis[x]) continue; //跳过已经访问过的点
vis[x] = true; //取出的点x已经得到了最短距离d[x]
for (auto &[y, w] : g[x]) {
//如果vis[y]说明y已经得到最短距离,无需更新
if (!vis[y] && d[y] > d[x] + w) {
d[y] = d[x] + w;
pq.push({y, d[y]});
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w; cin >> u >> v >> w;
if (u != v) g[u].push_back({v, w});
}
dijkstra(1);
//d[n]的距离如果为无穷则无最短路,否则输出最短路
cout << (d[n] >= 0x3f3f3f3f3f3f ? -1 : d[n]) << '\n';
}
2.floyd算法 多源最短路
使用邻接矩阵
三层循环:for(中转点k) for(入点i) for(出点j)
P1079 - 【模板】最短路(3) - ETOJ (eriktse.com)
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef double db;
const int N = 1e3 + 10;
ll d[N][N];
int main() {
int n, m, q; cin >> n >> m >> q;
memset(d, 0x3f, sizeof d);
for (int i = 1; i <= m; i++) {
ll u, v, w; cin >> u >> v >> w;
d[u][v] = min(d[u][v], w);
}
//初始化
for (int i = 1; i <= n; i++) d[i][i] = 0;
for (int k = 1; k <= n; k++) { //中转点在第一行
for (int i = 1; i <= n; i++) { //起点在第二行
for (int j = 1; j <= n; j++) { //终点在第三行
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
while (q--) {
int u, v; cin >> u >> v;
cout << (d[u][v] >= 0x3f3f3f3f3f3f ? -1 : d[u][v]) << '\n';
}
}
3.spfa
spfa可以用于处理负权边的情况,但容易达到O(nm)的复杂度而超时
P3371 【模板】单源最短路径(弱化版) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
const int N = 1e4 + 10;
const int M = 5e5 + 10;
int head[N], cnt;
struct {
int to, next;
ll w;
}edge[M];
ll d[N];
int n, m, st;
//前向星存边
void addedge(int u, int v, ll w){
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void spfa(int st){
for(int i = 1; i <= n; i++) d[i] = 2147483647;
d[st] = 0;
bitset<N> vis;
queue<int> q;
q.push(st);
vis[st] = true;
while(q.size()){
int x = q.front(); q.pop();
vis[x] = false;
for(int i = head[x]; i != -1; i = edge[i].next){
int y = edge[i].to; ll w = edge[i].w;
if(d[y] > d[x] + w){
d[y] = d[x] + w;
if(!vis[y]){
vis[y] = true;
q.push(y);
}
}
}
}
}
int main() {
cin >> n >> m >> st;
//初始化init
for(int i = 0; i < N; i++) head[i] = -1;
for(int i = 0; i < M; i++) edge[i].next = -1;
cnt = 0;
//前向星存边
for(int i = 1; i <= m; i++){
int u, v; cin >> u >> v;
ll w; cin >> w;
addedge(u, v, w);
}
spfa(st);
for(int i = 1; i <= n; i++){
cout << d[i] << ' ';
}
}
2023.12.23 CCPC北京市赛 A题
二、应用
1.最短路条数
P1144 最短路计数 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
增加一个计数的条件即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
const int mod = 100003;
struct edge{
int v;
ll w;
bool operator < (const edge & x) const{
return w == x.w ? v < x.v : w > x.w;
}
};
vector<edge> g[N];
ll d[N], ans[N];
void dijkstra(int st){
bitset<N> vis;
memset(d, 0x3f, sizeof d);
d[st] = 0, ans[st] = 1;
priority_queue<edge> pq;
pq.push({st, d[st]});
while(pq.size()){
int x = pq.top().v; pq.pop();
if(vis[x]) continue;
vis[x] = true;
for(auto &i : g[x]){
int y = i.v; ll w = i.w;
if(d[y] > d[x] + w){
d[y] = d[x] + w;
ans[y] = ans[x];
pq.push({y, d[y]});
}else if(d[y] == d[x] + w){
ans[y] = (ans[y] + ans[x]) % mod;
}
}
}
}
int main(){
int n, m; cin >> n >> m;
for(int i = 1; i <= m; i++){
int u, v; cin >> u >> v;
ll w = 1;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dijkstra(1);
for(int i = 1; i <= n; i++){
cout << ans[i] << '\n';
}
}
2.加边
如果要在两个点之间加一条边,则要看看是否满足dd[i] + d[j] + 1 >= dd[en] && dd[j] + d[i] + 1 >= dd[en],这样才能保证最短路长度不变,需要在起点st跑一次dijkstra,终点en再跑一次dijkstra
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
struct edge{
int v;
ll w;
bool operator < (const edge & x) const{
return w == x.w ? v < x.v : w > x.w;
}
};
vector<edge> g[N];
ll d[N], dd[N];
void dijkstra(int st){
bitset<N> vis;
memset(d, 0x3f, sizeof d);
d[st] = 0;
priority_queue<edge> pq;
pq.push({st, d[st]});
while(pq.size()){
int x = pq.top().v; pq.pop();
if(vis[x]) continue;
vis[x] = true;
for(auto &i : g[x]){
int y = i.v; ll w = i.w;
if(!vis[y] && d[y] > d[x] + w){
d[y] = d[x] + w;
pq.push({y, d[y]});
}
}
}
}
int main(){
int n, m, st, en; cin >> n >> m >> st >> en;
for(int i = 1; i <= m; i++){
int u, v; cin >> u >> v;
ll w = 1;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dijkstra(st);
for(int i = 1; i <= n; i++) dd[i] = d[i];
dijkstra(en);
int ans = -m;
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
if(dd[i] + d[j] + 1 >= dd[en] && dd[j] + d[i] + 1 >= dd[en]){
ans++;
}
}
}
cout << ans << '\n';
}
3.floyd更新操作
P1119 灾后重建 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<bits/stdc++.h>
using namespace std;
#define qio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef long long ll;
const int N = 2e2 + 10;
int f[N][N], a[N];
int n, m;
void update(int k){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
f[i][j] = f[j][i] = min(f[i][j], f[i][k] + f[k][j]);
}
}
}
int main() {
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
f[i][j] = 1e9;
}
}
for(int i = 0; i < n; i++) f[i][i] = 0;
for(int i = 1; i <= m; i++){
int u, v, w; cin >> u >> v >> w;
f[u][v] = f[v][u] = w;
}
int q, now = 0; cin >> q;
while(q--){
int u, v, t; cin >> u >> v >> t;
while(a[now] <= t && now < n){
update(now);
now++;
}
if(a[u] <= t && a[v] <= t){
if(f[u][v] == 1e9) cout << -1 << '\n';
else cout << f[u][v] << '\n';
}else{
cout << -1 << '\n';
}
}
}