1003. Emergency (25)

来源:互联网 发布:mysql 命令行导入csv 编辑:程序博客网 时间:2024/06/11 05:21

#include<cstdio> //<stdio.h>is used in c#include<cstdlib> //useless?#include<climits> // INT_MAX #include <iostream>using namespace std;const int MAX=501;int wei[MAX],visit[MAX],map[MAX][MAX];int mind,cnt,maxt,n;//n is moved to mainvoid init (int n){                //直接全部初始化也可以for (int i=0;i<n;i++){visit[i]=0;    for (int j=0;j<n;j++){map[i][j]=INT_MAX;}}}void dfs(int p, const int end, int dist, int weit){ //p will be changed from bgif (dist>mind) return; //影响时间if (p==end){if (dist<mind){mind=dist;cnt=1;maxt=weit;}else if (dist==mind){ //区分==和=cnt++;if (maxt<weit){maxt=weit;}}return;}else {for (int i=0;i<n;i++){//n should be claimed out of mainif (visit[i]==0&&map[p][i]!=INT_MAX){visit[i]=1;dfs(i,end,dist+map[p][i],weit+wei[i]);visit[i]=0;}}}}int main (){int m,bg,end;mind=INT_MAX;scanf("%d %d %d %d",&n,&m,&bg,&end);for (int i=0;i<n;i++){scanf("%d",&wei[i]);}init (n);while (m--){int x,y,w;scanf ("%d %d %d",&x,&y,&w);if (map[x][y]>w){map[x][y]=map[y][x]=w; //no-direction graph}} dfs (bg,end,0,wei[bg]);printf("%d %d",cnt,maxt);return 0;}

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



分析:

此题区别于以前的题目,开始设计算法。所以我也是借鉴别人的程序,来学习。基本上的思路有

1)利用最短路径dijkstra算法

2)利用深度搜索DFS算法

我是借鉴了后者,因为此题的要求是求得最短路径的条数。可以参考:http://blog.csdn.net/iaccepted/article/details/21451949

另外,此次自己写的时候debug了好久,虽然是细节,还是记录下:

1. if for 等的主体{}遗漏,需要良好的习惯;

2.==和=的搞错,这类bug很不好找啊。因为我是模仿别人的程序,所以可以用排查法。

3. 还有第一次mind我没有赋值,编译也通过了,然后就悲剧了。

因此,得抽时间学习如何有效debug以及良好的习惯啊。

从这题开始,估计难度应该都会提高了,加油加油!



0 0
原创粉丝点击