1003 Emergency (25 分)

本文深入探讨了Dijkstra算法的原理与应用,通过具体的代码示例,详细讲解了如何使用Dijkstra算法解决最短路径问题。同时,文章还介绍了算法的时间复杂度和空间复杂度,并提供了实际场景中的应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
const int inf = 0x3fffffff;
const int maxn = 510;
int cost[maxn][maxn];
int d[maxn];
int npath[maxn];
int hands[maxn];
int used[maxn];
int team[maxn];

void dijkstra(int s, int end, int n){
    fill(d, d+n, inf);
    fill(used, used+n, 0);
    fill(hands, hands+n, 0);
    fill(npath, npath+n, 0);
    d[s] = 0; hands[s] = team[s]; npath[s] = 1;
    while(true){
        int v = -1;
        for(int u = 0; u < n; u++){
            if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
        }
        if( v == -1) break;
        used[v] = 1;
        for(int u = 0; u < n; u++){
            if(d[u] > d[v] + cost[v][u]){
                d[u] = d[v] + cost[v][u];
                npath[u] = npath[v];
                hands[u] = hands[v] + team[u];
            }else if(d[u] == d[v] + cost[v][u]){
                npath[u] += npath[v];
                hands[u] = max(hands[u], hands[v]+team[u]);
            }
        }
    }
    printf("%d %d\n", npath[end], hands[end]);
}

int main(){
    int n, m, start, end;
    scanf("%d%d%d%d", &n, &m, &start, &end);
    for(int i = 0; i < n; i++)
        scanf("%d", &team[i]);
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) cost[i][j] = inf;
    int c1, c2, x;
    for(int i = 0; i < m; i++){
        scanf("%d%d%d", &c1, &c2, &x);
        cost[c1][c2] = cost[c2][c1] = x;
    }
    dijkstra(start, end, n);
        return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值