1003 Emergency(25 分)

本文介绍了一个紧急救援场景下的最短路径算法实现。该算法需要找到从当前城市到目标城市的最短路径,并尽可能多地集结救援队伍。输入包括城市数量、道路连接情况及救援队伍分布等信息,输出则是不同最短路径的数量及最大可集结的救援队伍数量。

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

 

1003 Emergency(25 分)

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C​1​​ and C​2​​ - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c​1​​, c​2​​ and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output:

2 4

记得以前做过一次这道题,但是忘得差不多了,好久没碰题目了,感觉生疏的许多,费了好大劲才ac5555555555 

#include<stdio.h>
int mp[505][505]; //模拟地图
int book[505]; //标记数组
int dis[505]; //记录边的数组
int count[505];  //路的数目
int w[505];    //救援队,权值
int city[505];  //初始化的救援队
int n,m,c1,c2;
int inf = 0X3f3f3f3f;
void init()
{
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            if(i == j){
                mp[i][j] = 0;
            }
            else{
                mp[i][j] = inf;
            }
        }
    }
    for(int i = 0;i < n;i++){
        book[i] = 0;
        count[i] = 0;
        w[0] = 0;
    }
}
void dij()
{

    count[c1] = 1;
    w[c1] = city[c1];
    book[c1] = 1;
    for(int i = 0;i < n;i++){
        dis[i] = mp[c1][i];
        if(dis[i] != inf && i != c1){
            w[i] = w[c1] + city[i];
            count[i] = 1;
        }
    }
    for(int i = 0;i < n-1;i++){
        int min = inf;
        int u;
        for(int j = 0;j < n;j++){
            if(book[j] == 0 && dis[j] < min){
                min = dis[j];
                u = j;
            }
        }
        book[u] = 1;
        for(int v = 0;v < n;v++){
            if(mp[u][v] < inf){
                if(!book[v] && dis[v] > dis[u] + mp[u][v]){
                    dis[v] = dis[u]+mp[u][v];
                    w[v] = w[u] + city[v];//更新救援队的数量
                    count[v] = count[u]; //更新到v点的路径数目
                }
                else if(!book[v] && dis[v] == dis[u] + mp[u][v]){
                    count[v] += count[u];
                    if(w[v] < w[u] + city[v]){
                        w[v] = w[u] + city[v];
                    }
                }
            }
        }
    }
}
int main()
{
    scanf("%d %d %d %d",&n,&m,&c1,&c2);
    init();
    for(int i = 0;i  < n;i++){
        scanf("%d",&city[i]);
    }
    for(int i = 0;i < m;i++){
        int u,v,l;
        scanf("%d %d %d",&u,&v,&l);
        mp[u][v] = mp[v][u] = l;
    }
    dij();
    printf("%d %d\n",count[c2],w[c2]);
    return 0;
}

 

/**  * @file ProximitySwitchControl.cpp  * @brief 此程序用于通过接近开关控制四个电磁阀的开启关闭顺序,并设置每个电磁阀的单独开启时间  */ #include <Servo.h> // // 接近信号输入引脚// 运行状态枚举 enum SystemState {   IDLE,   OPENING_VALVES,   HOLDING_OPEN,   CLOSING_VALVES,   EMERGENCY_STOP }; const int proximityPin = 6;    // // 电磁阀控制引脚 const int valvePins[] = {9,10,11,12};   /**  * @brief 定义存储电磁阀控制引脚的数组  */ // 开启顺序(对应valvePins索引) int openOrder[] = {0,1,2,3};     /**  * @brief 定义电磁阀的开启顺序数组  */ // 关闭顺序(对应valvePins索引) int closeOrder[] = {3,1,2,0};   /**  * @brief 定义电磁阀的关闭顺序数组  */ // 运行状态标志 bool isRunning = false; /**  * @brief 表示当前控制序列是否正在运行的标志,初始为false  */ // 上一次接近开关状态 bool lastProxState = HIGH; /**  * @brief 用于存储上一次读取的接近开关状态  */ // 消抖时间戳 unsigned long debounceTime = 0; /**  * @brief 记录上次检测到接近开关状态变化的时间,用于消抖处理  */ // 每个电磁阀的单独开启时间(毫秒) int openTimes[] = {500, 500, 500, 900};   /**  * @brief 定义每个电磁阀的单独开启时间数组  */ /**  * @brief 初始化设置函数  */ void setup() {   // 循环设置电磁阀引脚为输出,并初始化为低电平   for(int i=0; i<4; i++) {     pinMode(valvePins[i], OUTPUT);     digitalWrite(valvePins[i], LOW);   }   /**    * @brief 为每个电磁阀引脚设置模式为输出,并将初始状态设置为低电平    */   // 设置接近开关引脚为上拉输入   pinMode(proximityPin, INPUT_PULLUP);   /**    * @brief 将接近开关引脚设置为上拉输入模式    */ } /**  * @brief 主循环函数  */ void loop() {   int currentProxState = digitalRead(proximityPin);   /**    * @brief 读取当前接近开关的状态    */   // 带消抖的下降沿检测   if(!isRunning && (millis() - debounceTime) > 50){     /**      * @brief 检查是否处于未运行状态且经过了足够的消抖时间      */     if(lastProxState == HIGH && currentProxState == LOW){       /**        * @brief 检测到接近开关从高电平变为低电平(下降沿)        */       isRunning = true;       debounceTime = millis();       /**        * @brief 设置运行标志为true,并更新消抖时间        */       // 运行控制序列       if(runSequence()) {           /**          * @brief 调用控制序列函数,如果成功则进行后续处理          */         isRunning = false;         /**          * @brief 控制序列完成,将运行标志设置为false          */       } else {         /**          * @brief 控制序列运行失败,进行错误处理          */         // 可以在这里添加错误处理代码,如输出错误信息或采取其他措施         Serial.println("Control sequence failed.");       } 在这个代码中增加漫反射光电感应开关作为安全检测触发点的功能
最新发布
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值