PTA(Advanced Level)1003 Emergency


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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

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

题目翻译:

作为城市的紧急救援团队负责人,您将获得一张您所在国家的特殊地图。该地图显示了一些通过道路连接的分散城市。地图上标出了每个城市的救援队数量以及每对城市之间的每条道路的长度。当从其他城市接到您的紧急电话时,您的工作是尽快带领您的士兵前往该地点,同时,在途中尽可能多地动手。

每个输入文件包含一个测试用例。对于每个测试用例,第一行包含4个正整数:N(≤500)-城市数量(并且城市从0到N-1编号),M-道路数量,C 1和C 2-您当前所在的城市和必须保存的城市。下一行包含N个整数,其中第i个整数是第i个城市中救援队的数量。然后是M条线,每条线描述了一条具有三个整数c1,c2和L的道路,分别是一条道路和该条道路的长度所连接的一对城市。保证从C 1到C 2至少存在一条路径。

对于每个测试用例,在一行中打印两个数字:C 1和C 2之间最短路径的不同数量以及可以收集的最大救援团队数量。一行中的所有数字必须完全由一个空格分隔,并且在行尾不允许有多余的空格。

代码:

#include<iostream>
using namespace std;

#define MAXN 501
#define INF 0x3f3f3f3f

int map[MAXN][MAXN];//map[i][j]表示i到j的路径长度,INF表示没有直接相连的路
int dis[MAXN];//dis[i]表示从原点到节点i的最短路
bool visit[MAXN];//visit[i]记录i节点是否被访问过
int N,M;//存储城市个数与路径个数
int teams[MAXN];//team[i]表示i城市有的救援队的人数
int start,e;//起点城市与终点城市
int max_team[MAXN];//max_team[i]表示从原点到节点i可以获得的最大救援队人数
int num[MAXN];//num[i]表示到i节点的最短路的个数

void printMap(){
 for(int i=0;i<N;i++){
  for(int j=0;j<N;j++){
   cout<<map[i][j]<<" ";
  }
  cout<<endl;
 }
}

void init(){
 cin>>N>>M>>start>>e;
 for(int i=0;i<N;i++){
  cin>>teams[i];
 }
 //初始化 
 for(int i=0;i<N;i++){
  for(int j=0;j<N;j++){
   if(i!=j) map[i][j] = INF;
   else map[i][j] = 0;
  }
 }
 
 int x,y,d;
 //赋值
 for(int i=0;i<M;i++){
  cin>>x>>y>>d;
  map[x][y]=map[y][x]=d;
 } 
 
 for(int i=0;i<N;i++){
  visit[i] = false;
  dis[i] = map[start][i];
  max_team[i] = 0;
  num[i] = 0;
 }
 //初始化起点
 max_team[start] = teams[start];
 num[start]=1; 
 //printMap();
} 

void dijkstra(){
 while(1){
  int min = INF,index = -1;
  for(int i=0;i<N;i++){
   if(!visit[i]){
    if(min > dis[i]){
     min = dis[i];
     index = i;
    }
   }
  }
  if(index == -1) break;
  visit[index] = true;
  for(int i=0;i<N;i++){
   if(map[index][i]!=INF && !visit[i]){
    if(dis[i] > dis[index] + map[index][i]){//可以通过中间点i优化路径
     dis[i] = dis[index] + map[index][i]; //更新距离
     max_team[i] = max_team[index] + teams[i];//更新救援队数量
     num[i] = num[index];//继承路径数
    }else if(dis[i] == dis[index] + map[index][i]){//如果路径距离相等
     num[i] = num[index] + num[i];//加上到达上一点的路径数
     if(max_team[i] < max_team[index] + teams[i]){//看走哪条路,得到的救援队数量更多,要尽可能多
      max_team[i] = max_team[index] + teams[i];
     }
    }
   }
  }
 }
}

int main(){
 init();
 dijkstra();
 cout<<num[e]<<" "<<max_team[e]<<endl;
}
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值