PAT甲级1003. Emergency (25)

来源:互联网 发布:gta5男角色捏脸数据 编辑:程序博客网 时间:2024/05/20 11:24

题目链接

https://www.patest.cn/contests/pat-a-practise/1003

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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output
2 4

思路分析

题目的意思就是一张图,各边都有权值而且个点也有权值,求出给定两点之间最短路的条数并输出此路径上的点权值和的最大值。就是求出最短路的条数,若有多条相同的最短路输出最大的那个点权值和。

很简单的题目,一个dfs就可以很好的解决掉。

用到的数组

map[][]邻接矩阵保存边的权值

visited[]判断该节点是否访问过

city_value[]保存节点权值

代码

#include<iostream>#include<cstring>using namespace std;const int N = 500;int city_value[N];int map[N][N];bool visited[N];int city_num,road_num,start,end_;int Min_path = 100000;//记录最小path长度int Min_count = 0;//记录最小path个数int Max_num = 0;//记录最大人数void dfs(int start,int length,int num){if(start == end_){if(length > Min_path)//判断1 return ;if(length < Min_path)//判断2{Min_path = length;Min_count = 1;Max_num = num;}else//最小path相同 Min_count++{Min_count++;Max_num = Max_num > num?Max_num:num;}//出错原因思考 判断1和判断2 颠倒顺序//else的匹配问题 假设先有 判断1 则else的条件肯定为 length = Min_path的条件//假设 先有 判断2 且判断2 执行了 此时到判断1时 与else匹配 else的条件为 length<=Min_path 刚才Min_count =1 ,现在Min_count++ 为2 }if(length > Min_path)//剪枝return ;for(int k = 0;k < city_num ; k++)//开始遍历所有与start城市相连的城市{if(map[start][k] == -1 || visited[k] == true)continue;visited[k] = true;dfs(k,length+map[start][k],num+city_value[k]);visited[k] = false;//注意}}int main(){memset(visited,0,sizeof(visited));memset(city_value,0,sizeof(city_value));memset(map,-1,sizeof(map));//三个数组初始化cin>>city_num>>road_num>>start>>end_;for(int i = 0;i < city_num;i++)cin>>city_value[i];for(int j = 0;j < road_num;j++){int a,b,len;cin>>a>>b>>len;map[a][b] = map[b][a] = len;}visited[start] = true;dfs(start,0,city_value[start]);visited[start] = false;cout<<Min_count<<" "<<Max_num<<endl;return 0;}


原创粉丝点击