1003. Emergency (25)
2016-03-29 18:35
399 查看
1003. Emergency (25)
QuestionAs 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 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
把之前写的在修改一下,这样子更加直观一点儿。
思路很简单的“深度优先搜索”。
我的解法
#include <iostream> #include <climits> //不能是<limits> using namespace std; const int MAX = 501; int C_N, R_N, FROM, TO; int teamsInCity[MAX]; int map[MAX][MAX]; bool visited[MAX]; int shortestLen = INT_MAX; int mostTeams = 0; int shortedNo = 0; int currentLen = 0; int currentTeams = 0; void depthSearch(int city_in) { //搜索结束1:到达目的城市 if (city_in == TO) { if (currentLen < shortestLen) { shortedNo = 1; mostTeams = currentTeams; shortestLen = currentLen; }else if (currentLen == shortestLen) { shortedNo++; if (currentTeams > mostTeams) { mostTeams = currentTeams; } } return; } for (int i = 0; i < C_N; ++i) { if (map[city_in][i] != INT_MAX && !visited[i]) { currentLen += map[city_in][i]; currentTeams += teamsInCity[i]; visited[i] = true; depthSearch(i); currentLen -= map[city_in][i]; currentTeams -= teamsInCity[i]; visited[i] = false; } } } int main(int argc, const char * argv[]) { // 5 6 0 2 cin >> C_N >> R_N >> FROM >> TO; //每一个城市中的team的数量:1 2 1 5 3 for (int i = 0; i < C_N; ++i) { cin >> teamsInCity[i]; } //初始化每个城市间的地图 for (int i = 0; i < C_N; ++i) { for (int j = 0; j < C_N; ++j) { map[i][j] = INT_MAX; } } //绘制地图 int from, to, len; for (int i = 0; i < R_N; ++i) { cin >> from >> to >> len; map[from][to] = map[to][from] = len; } //已经去过的城市 for (int i = 0; i < C_N; ++i) { visited[i] = false; } visited[FROM] = true; //搜索开始 currentTeams = teamsInCity[FROM]; depthSearch(FROM); cout << shortedNo << " " << mostTeams; return 0; }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析