pat1003Emergency (25)

56 篇文章 0 订阅

题意分析:

(1)给出城市的道路分布、各道路的长度、以及各城市拥有的救援队伍,找出从出发点到事发点最短路径数以及沿途能够集结到最多的救援队伍数。

(2)按照题意理解,此题属于求最短路径的题目,可以采用深度优先搜索,但需要注意回溯,动态更新最短路径以及能够集结的最多救援队伍数;当然也可以先求出最短路径之后再求最多队伍数,本题采用边搜索边更新。

(3)采用数组来记录最短路径,数组中元素记录的是第i个节点的父节点,在计算最短路径长度和最多救援队伍的时候,采用递归的思路

可能坑点:

(1)切记!!!最多救援队伍数并不是全局的,而是限制在最短路径上的。因此当最短路径被更新的时候,最大救援队伍数是需要强制更新的,最短路径数为1;当前最短路径等于最短路径时,此时若当前最多集结队伍数大于最多队伍数,则更新(有点绕口。。。。);其余情况不更新。

#include <iostream>
#include <string.h>
#include <limits.h>
using namespace std;
int rescue[500]={0};
int map[500][500]={0};
int min_path=INT_MAX;
int current_min_path=INT_MAX;
int current_rescue=0;
int min_num=0;
int max_rescue=0;
int preCity[500]={-2};
bool visit[500]={0};
int N,M,C1,C2;
//return the current length of the path
int get_length(int end)
{
    if(preCity[end]==-1)return 0;
    else return get_length(preCity[end])+map[preCity[end]][end];
}
//return the current rescue teams
int get_rescue(int end)
{
    if(preCity[end]==-1)return rescue[end];
    else return get_rescue(preCity[end])+rescue[end];
}
void DFS(int from,int to)
{
    if(from==to)
    {
        current_min_path=get_length(to);
        current_rescue=get_rescue(to);
        if(current_min_path<=min_path)
        {
            if(current_min_path<min_path)
            {
                min_num=1;
                max_rescue=current_rescue;
            }
            else
            {
                min_num++;
                if(max_rescue<current_rescue)max_rescue=current_rescue;
            }
            min_path=current_min_path;
        }
        return;
    }
    visit[from]=1;
    for(int i=0;i<N;i++)
    {
        if(!visit[i]&&map[from][i])
        {
            //visit[i]=1;
            preCity[i]=from;
            DFS(i,to);
            visit[i]=0;
        }
    }
}

int main()
{
    //memset(rescue,0,sizeof(rescue));
    //memset(map,0,sizeof(map));
    cin>>N>>M>>C1>>C2;
    int i=0,j=0;
    while(i<N)cin>>rescue[i++];
    int city1,city2,length;
    while(j<M)
    {
        cin>>city1>>city2>>length;
        map[city1][city2]=length;
        map[city2][city1]=length;
        j++;
    }
    preCity[C1]=-1;
  //  visit[C1]=1;
    DFS(C1,C2);
    cout<<min_num<<" "<<max_rescue<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值