PAT (Advanced Level) Practice 1003 Emergency

1003 Emergency

分数 25
作者 CHEN, Yue
单位 浙江大学

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

题目翻译:

1003.突发事件(25)

作为一个城市紧急援救队的指挥者,你得到了一个国家的特殊地图。地图上分散着几座城市,城市间用道路连接着。每个城市援救队的数量以及两座城市之间每条道路的长度已经在地图上标出。当某些城市发生了突发事件,需要你的帮助时,你的工作是带领你的队伍尽快的赶到事发现场,与此同时,召集尽可能多的在路上的队伍。

输入

每个输入文件包含一个测试实例。每个实例的第一行有四个正整数:N(<= 500)是城市的个数(城市的编号从0到N-1),M是道路的个数,C1和C2分别是你现在所在的城市以及你必须去救援的城市。下一行有N个整数,第i个整数是第i个城市中救援队的数量。然后下面有M行,每行表示一条道路。每一行有三个整数c1,c2和L,分别表示道路连接的两个城市以及道路的长度。保证C1到C2之间存在至少一条路径。

输出

对于每个测试实例,在一行中输出两个数字:C1和C2之间不同的最短路径的个数,你能聚集起来的最多的救援队数量。

一行中的所有数字必须被一个空格分隔开,在每行的结尾不允许出现空格。

代码

#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>

using namespace std;

int paths,c2,dist[500][500],minDist[500],teams;
vector<int> cities,v[500];

void dfs(int cur,int curlen,int curteam) {
  if (curlen>minDist[cur])return;//剪枝,避免死循环以及无必要比较
  if (cur==c2) {
    if(curlen==minDist[cur]){
      paths++;
      if (curteam>teams) teams=curteam;
    }else if(curlen<minDist[cur]){
      minDist[cur]=curlen;
      paths=1;
      teams=curteam;
    }
  }else{
    if(curlen<minDist[cur]) minDist[cur]=curlen;
    for (int i=0;i < v[cur].size();i++) {
      int j=v[cur][i];
      dfs(j,curlen+dist[cur][j],curteam+cities[j]);
    }
  }
}

int main(){
    int n,m,c1;
    cin>>n>>m>>c1>>c2;
    int temp;
    while (n--) {
      cin>>temp;
      cities.emplace_back(temp);
    }
    for (int i = 0; i < 500; i++) minDist[i]=0x7FFFFFFF;
    while (m--) {
      int temp1,temp2,distance;
      cin>>temp1>>temp2>>distance;
      v[temp1].emplace_back(temp2);
      v[temp2].emplace_back(temp1);
      dist[temp1][temp2]=dist[temp2][temp1]=distance;
    }
    dfs(c1,0,cities[c1]);
    cout << paths << ' ' << teams;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值