3.1.3 单源最短路的扩展应用

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Iperf 3.1.3是一种网络测量工具,主要用于测试网络带宽、网络吞吐量以及网络延迟等指标。它可以在客户机和服务器之间进行测试,并提供实时测试结果。Iperf 3.1.3支持TCP和UDP协议,可以通过指定测试带宽、数据包大小、测试时间和测试流量等参数,来进行不同类型的网络性能测试。Iperf 3.1.3的主要特点包括支持多平台、易于使用、确保数据传输的准确性和可测量性,并能产生详细的测试报告。通过使用Iperf 3.1.3,可以快速地定位网络故障,并优化网络性能,是网络工程师和网络管理员进行网络优化的必备工具。 ### 回答2: iperf是一款常用的网络带宽测量工具,它可以测试网络基础设施的性能指标,在网络管理、网络调优、应用质量评估、故障排查等方面都具有不可替代的作用。iperf 3.1.3是整个iperf工具集系列中的最新版本,与前几个版本相比,它增强了IPv6的支持,改进了Windows平台的兼容性问题,修复了带宽检测不准确的bug等方面的缺陷。同时,iperf 3.1.3还提供了丰富的命令行选项,可以设置多种测试条件,例如带宽限制、数据包大小、测试时长、多路并发等。在使用上,只需在一台运行iperf的主机上启动iperf服务端,然后在需要测量带宽的另一台客户端计算机上运行iperf客户端,通过与服务器进行数据交换,生成测试报告。iperf 3.1.3支持TCP和UDP两种传输协议,可以测试线性和非线性网络的性能,包括带宽、时延、丢包率、吞吐量等重要指标,提供可视化显示和报告输出,方便用户理解和分析结果。总的来说,iperf 3.1.3是一款功能强大、易于使用、可靠性高的网络性能测试工具,在实际应用中具有广泛的适用性和可靠性,是网络管理和优化工作必备的利器。 ### 回答3: Iperf 3.1.3是一种网络测速工具,可以测试计算机网络之间的最大带宽。它可以帮助用户实现最大化带宽,确保网络系统的稳定性和性能。 Iperf 3.1.3使用客户端/服务器模式工作,提供实时流量统计信息。这种网络测速工具可以测试TCP和UDP协议,通过缓存大小来确定最大带宽。 Iperf 3.1.3可以通过多种参数进行定制。其中一些参数包括测试持续时间,测试并发连接数量,缓存大小,打印报告的时间间隔,以及应该生成哪种类型的报告。 总之,Iperf 3.1.3是一款强大的网络测速工具,可以用于测试计算机网络的性能。它提供了许多参数,可以用于定制测试以满足用户的需求,并提供实时流量统计信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值