PAT Advanted 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, 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 Specification:

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

算法标签:DFS

源代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int n, m, c1, c2; // 城市数 Road数  源城  目的城
int tnum[510], dis[500][500], mindis[500]; // 两城市路径长度
int paths, max_num; //最短路径长度   最多召集人数
vector<int> v[500]; // 邻接城

void dfs(int curcity, int curlen, int num) //  当前所在城市  路径长度  召集人数
{
    if(curlen > mindis[curcity])    return; // 当前路径长度不是最小  剪枝
    if(curcity == c2) //到达目的地
    {
        if(curlen == mindis[curcity]) //当前路径是最短路径之一
        {
            paths ++;
            max_num = max(max_num, num); // 更新最多召集人数
        }
        else // 当前路径为最短路径
        {
            paths = 1;
            mindis[c2] = curlen; //更新目的地最短路径长度
            max_num = num; //因为最短路径更新  顺带召集人数重置为当前路径人数
        }
    }
    else  //没有到达目的地
    {
        if(curlen < mindis[curcity])    mindis[curcity] = curlen;
        for(int i = 0 ; i < v[curcity].size() ; i ++)
        {
            int j = v[curcity][i]; //遍历邻接城市
            dfs(j, curlen + dis[curcity][j], num + tnum[j]);
        }
    }
    
}

int main()
{
    int j, k, l;
    cin >> n >> m >> c1 >> c2;
    //每个城市救援队数量
    for(int i = 0 ; i < n ; i ++)   cin >> tnum[i];
    for(int i = 0 ; i < m ; i ++)
    {
        cin >> j >> k >> l;
        v[j].push_back(k);
        v[k].push_back(j);
        dis[j][k] = dis[k][j] = l;
    }
    for(int i = 0 ; i < n ; i ++)   mindis[i] = 1e9;
    
    dfs(c1, 0, tnum[c1]);
    
    cout << paths << ' ' << max_num;
    
    return 0;
}
posted @   D_coding_blog  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示