PAT甲级1003Emergency

 题目地址:

编程题 - PAT (Advanced Level) Practice (pintia.cn)

介绍

寻找路径最短的种类数并输出最短路径的最多救援队数量

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.

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long  
#define ull unsigned long long
int i,j,n,m;
int num1[1001][1001];
int kinds_num=0;
int kinds=99999;
bool num2[1001];
int where_num[501];
int kinds_amount=-1;
void dfs(int now1,int sum,int num)//sum=road_length num=team
{
    
    if(now1==m)
    {
        //memset(num2,0,sizeof(num2));
        //kinds[sum]++;
        if(sum<kinds)
        {
            kinds=sum;
            kinds_num=1;
            kinds_amount=num;
        }
        else if(sum==kinds)
        {
            kinds_num++;
            kinds_amount=max(kinds_amount,num);
        }
        return;
    }
    num2[now1]=1;
    for(int a1=0;a1<=i-1;a1++)
    {
        if(num2[a1]==0&&num1[now1][a1]!=0)
        {
            num2[a1]=1;
            dfs(a1,sum+num1[now1][a1],num+where_num[a1]);
            num2[a1]=0;
        }
    }
}
void solve() {
	
	memset(num1,0,sizeof(num1));
	memset(num2,0,sizeof(num2));
    //memset(kinds,0,sizeof(kinds));
    //memset(kinds_amount,0,sizeof(kinds_amount));
    cin>>i>>j>>n>>m;
    int p1,p2;
    int i1,j1;

    for(int p1=0;p1<=i-1;p1++)
    {
        cin>>where_num[p1];
    }
    for(int p1=0;p1<=j-1;p1++)
    {
        
        cin>>i1>>j1>>p2;
        num1[i1][j1]=p2;
        num1[j1][i1]=p2;
    }
    dfs(n,0,where_num[n]);
    //int *ssr=lower_bound(kinds,kinds+501,1);
    cout<<kinds_num<<" "<<kinds_amount;
} 

signed main() {

    ll t = 1; 
    // std::cin >> t;
    while (t--) {
        solve();
    }
}

### 关于 PAT 甲级 1024 题目 PAT (Programming Ability Test) 是一项编程能力测试,其中甲级考试面向有一定编程基础的学生。对于 PAT 甲级 1024 题目,虽然具体题目描述未直接给出,但从相似类型的题目分析来看,这类题目通常涉及较为复杂的算法设计。 #### 数据结构的选择与实现 针对此类问题,常用的数据结构包括但不限于二叉树节点定义: ```cpp struct Node { int val; Node* lchild, *rchild; }; ``` 此数据结构用于表示二叉树中的节点[^1]。通过这种方式构建的二叉树能够支持多种遍历操作,如前序、中序和后序遍历等。 #### 算法思路 当处理涉及到图论的问题时,深度优先搜索(DFS)是一种常见的解题策略。特别是当需要寻找最优路径或访问尽可能多的节点时,结合贪心算法可以在某些情况下提供有效的解决方案[^2]。 #### 输入输出格式说明 根据以往的经验,在解决 PAT 类型的问题时,输入部分往往遵循特定模式。例如,给定 N 行输入来描述每个节点的信息,每行按照如下格式:“Address Data Next”,这有助于理解如何解析输入并建立相应的数据模型[^4]。 #### 数学运算示例 有时也会遇到基本算术表达式的求值问题,比如分数之间的加减乘除运算。下面是一些简单的例子展示不同情况下的计算结果: - \( \frac{2}{3} + (-2) = -\frac{7}{3}\) -2) = -\frac{4}{3}\) - \( \frac{2}{3} ÷ (-2) = -\frac{1}{3}\) 这些运算是基于样例提供的信息得出的结果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值