1003. Emergency (25)

来源:互联网 发布:手机端 时间轴 源码 编辑:程序博客网 时间:2024/06/06 09:03

1003. Emergency (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
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

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
用了个小深搜,这里有一个小技巧值得再去研究,就是深搜里面的那个if语句,每当发现更小的路径,那么就把最短路径的条数重新初始化为1,很有智慧。
#include<bits/stdc++.h>#define INF 99999999using namespace std;int N,M,C1,C2;int value[501];int chess[501][501];int book[501];int minlen=INF,count1=0,maxvalue=-1;void DFS(int s,int len){int sum=0;int i,j;if(s==C2){for(i=0;i<N;i++){if(book[i]==1)sum+=value[i];}if(len<minlen){count1=1;minlen=len;maxvalue=sum;}else if(len==minlen){count1++;if(sum>maxvalue) maxvalue=sum;}return ;  //这个return加不加都不影响结果,return加了其实对于这道题唯一的帮助就是减少时间复杂度,算是一个小优化。}for(i=0;i<N;i++){if(book[i]==1) continue;if(chess[s][i]==INF) continue;book[i]=1;DFS(i,len+chess[s][i]);book[i]=0;}}int main(){cin>>N>>M>>C1>>C2;int i,j,sx,sy,st;for(i=0;i<N;i++)cin>>value[i];for(i=0;i<N;i++)for(j=0;j<N;j++)chess[i][j]=INF;for(i=0;i<M;i++){cin>>sx>>sy>>st;chess[sx][sy]=chess[sy][sx]=st;}book[C1]=1;DFS(C1,0);cout<<count1<<' '<<maxvalue<<endl;return 0;}



0 0
原创粉丝点击