【PAT 甲级】1003 Emergency (25)(25 分)(dfs,dijkstra)

本文介绍了一种寻找两个城市间最短救援路径及最大救援队伍数量的方法。利用DFS和Dijkstra算法,计算不同最短路径的数量及沿途能集结的最大救援人数。

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

题目链接

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

题意:
N个城市,M条路,给定N个城市中的救援人员个数,M条连接城市的路和对应距离,求C1到C2救援的最短路共有多少条,有多少救援人员。

思路:最短路
1.dfs
2.dijkstra算法

代码【dfs】:

#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 5e2+5;
const ll INF = 0x3f3f3f3f;

int num[N];
int numShorPath,MaxAmou,d=INF;
struct Edge {
    int v,w;
    Edge() {}
    Edge(int v,int w):v(v),w(w) {}
};
vector<Edge>g[N];
bool vis[N];
void init() {
    for(int i=0; i<N; i++) {
        vis[i]=0;
    }
}
void dfs(int u,int v,int dist,int MA) {
    if(u==v) {
        if(dist==d) {
            numShorPath++;
            if(MA>MaxAmou) {
                MaxAmou=MA;
            }
        } else if(dist<d) {
            numShorPath=1;
            d=dist;
            MaxAmou=MA;
        }
        return ;
    }
    for(int i=0; i<g[u].size(); i++) {
        Edge next=g[u][i];
        int to=next.v,w=next.w;
        if(vis[to])
            continue;
        vis[to]=1;
        dfs(to,v,dist+w,MA+num[to]);
        vis[to]=0;
    }
}
int main() {
    int n,m,C1,C2;
    cin>>n>>m>>C1>>C2;
    for(int i=0; i<n; i++)
        cin>>num[i];
    init();
    for(int i=0; i<m; i++) {
        int u,v,w;
        cin>>u>>v>>w;
        g[u].push_back({v,w});
        g[v].push_back({u,w});
    }
    dfs(C1,C2,0,num[C1]);
    cout<<numShorPath<<" "<<MaxAmou<<endl;
    return 0;
}

2.dijkstra算法
代码:

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define drep(i,n,a) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a,n,sizeof(a))
#define lowbit(i) ((i)&(-i))
typedef long long ll;
typedef unsigned long long ull;
const int INF=0x3f3f3f3f;
const double eps = 1e-6;
const int N = 5e2+5;

int n,m,s,e;
int G[N][N],num[N];
int pathNum[N],maxNum[N];///pathNum存储七点到所有点的路径数
///maxNum存储起点到所有点的最大人手
int dis[N];///dis存储从起点到所有点的最短路径的边权
bool vis[N];
void dijkstra() {
    fill(dis,dis+N,INF);
    dis[s]=0,pathNum[s]=1,maxNum[s]=num[s];
    while(1) {
        /*不断更新起点到其他点的最短路径,并更新最短路径中的点权*/
        int minD=INF,v=-1;
        for(int i=0; i<n; i++) {
            if(!vis[i]&&dis[i]<minD) {
                minD=dis[i];
                v=i;
            }
        }
        if(v==-1) break;
        vis[v]=1;
        for(int i=0; i<n; i++) {
            if(vis[i]||G[v][i]==INF) continue;///无法到达或者已经访问
            int tmpDis=dis[v]+G[v][i];
            int tmpNum=maxNum[v]+num[i];
            if(tmpDis<dis[i]) {
                dis[i]=tmpDis;
                maxNum[i]=tmpNum;
                pathNum[i]=pathNum[v];
            } else if(tmpDis==dis[i]) {
                pathNum[i]+=pathNum[v];
                if(tmpNum>maxNum[i]) {
                    maxNum[i]=tmpNum;
                }
            }
        }

    }
}
int main() {
    scanf("%d%d%d%d",&n,&m,&s,&e);
    for(int i=0; i<n; i++) {
        scanf("%d",&num[i]);
    }
    fill(G[0],G[0]+N*N,INF);
    for(int i=0; i<m; i++) {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        G[u][v]=G[v][u]=w;
    }
    dijkstra();
    printf("%d %d",pathNum[e],maxNum[e]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值