PAT 解题报告 1003. Emergency (25)

本文介绍了一种结合最短路径算法与资源收集的问题解决方法。具体地,使用Dijkstra算法寻找两点间的所有最短路径,并计算这些路径上可收集的最大资源数量。文章提供了两种实现思路,一种基于Dijkstra算法,另一种采用深度优先搜索(DFS)。同时给出了完整的代码示例。

摘要生成于 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

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, C1 and C2 - 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 c1, c2 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 C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, 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

题目描述:

抽象出来就是给你一张图,给你起点终点,着到起点到终点的所有最短路径,每个点上有一个数,代表这个点上的搜救队的数量,要求输出这些最短路径中你能召集到的最大的搜救队的数量。

算法分析:

思路1、dijsktra

简单题,最短路径算法(e.g. Dijastra),但是要稍微变化一下,要记录所有的的最短路径,所以从终点回溯上去可能有多于一个的parent。那么就在每个node上面挂一个 list好了,记录所有的parents. 得到后可以从终点DFS到起点,记录下每条最短路径上的搜救队的数量和。保存最大的那个输出即可。

思路2、DFS

注意点:

这个题目其实就是最短路径的小小变形,注意最原始的最短路径算法通过记录每个node最短路径上的parent来保存一条这样的最短路径,这里话就 是相当于,要多点判断,当新的路径和当前路径相同长度时,也要记录下来,但是后面如果找到更短的,那么之前的路径就要清空,记录路径的代码稍微注意一点就 没问题了。

Dijkstra

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

#define INF 0x3f3f3f3f
#define MX 501

int mp[MX][MX];
int v[MX];
int dist[MX];
int amount[MX];
int teams[MX];
int pathcount[MX];
int N,M,start,en;

void dijkstra(int s){
    amount[s] = teams[s];
    dist[s] = 0;
    pathcount[s] = 1;

    while (1){
        int u, dmin=INF;
        for (int i=0; i<N; i++){
            if (v[i]==0 && dist[i]<dmin){
                dmin = dist[i];
                u = i;
            }
        }
        if (dmin==INF || u==en) break;
        v[u] = 1;
        for (int i=0; i<N; i++){
            if(v[i]==0){
                if (dist[i] > dist[u] + mp[u][i]){
                    dist[i] = dist[u] + mp[u][i];
                    amount[i] = amount[u] + teams[i];
                    pathcount[i] = pathcount[u];
                }else if (dist[i] == dist[u] + mp[u][i]){
                    pathcount[i] += pathcount[u];
                    if (amount[i] < amount[u] + teams[i])
                        amount[i] = amount[u] + teams[i];
                }
            }
        }
    }
}

int main()
{
    scanf("%d%d%d%d", &N,&M,&start,&en);
    for (int i=0; i<N; i++)
    {
        scanf("%d", &teams[i]);
    }
    for (int i=0; i<N; i++)
    {
        dist[i] = INF;
        for (int j=0; j<N; j++)
            mp[i][j] = INF;
    }
    for (int i=0; i<M; i++)
    {
        int c1, c2, L;
        scanf("%d%d%d", &c1,&c2,&L);
        mp[c1][c2] = mp[c2][c1] = L;
    }

    dijkstra(start);
    printf("%d %d", pathcount[en], amount[en]);

    return 0;
}

 

DFS

#include<cstdio>
#include<cstring>

#define INF 0x7FFFFF
int u[502]={0};
int teams[502]={0};
int dist[502];
int mp[502][502];
int n,m,st,en;
int shortNum=0,maxteam=0,mindist=INF;

void dfs(int s,int dis,int team){//到达S结点时的距离,teams
    if(s==en){
        if(dis<mindist){
            mindist=dis;
            shortNum=1;
            maxteam=team;
        }else if(dis==mindist){
            shortNum++;
            if(team>maxteam) maxteam=team;
        }
        return;
    }
    u[s]=1;
    for(int i=0;i<n;i++){
        if(u[i]==0 && mp[s][i]>0){
            dfs(i,dis+mp[s][i],team+teams[i]);
        }
    }
    u[s]=0;
}

int main(){
    freopen("in.txt","r",stdin);
    int i;
    scanf("%d%d%d%d",&n,&m,&st,&en);
    for(i=0;i<n;i++) scanf("%d",&teams[i]);
    memset(mp,-1,sizeof(mp));
    for(i=0;i<m;i++){
        int t1,t2,dis;
        scanf("%d%d%d",&t1,&t2,&dis);
        mp[t1][t2]=mp[t2][t1]=dis;
    }

    //u[st]=1;
    dfs(st,0,teams[st]);

    printf("%d %d\n",shortNum,maxteam);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值