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 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<cstdio>
#include<cstring>

#define INF 0x7FFFFF
int u[502]={0};
int teams[502]={0};
int dist[502];
int mp[502][502];
int n,m,st,en;
int shortNum=0,maxteam=0,mindist=INF;

void dfs(int s,int dis,int team){//到达S结点时的距离,teams
    if(s==en){
        if(dis<mindist){
            mindist=dis;
            shortNum=1;
            maxteam=team;
        }else if(dis==mindist){
            shortNum++;
            if(team>maxteam) maxteam=team;
        }
        return;
    }
    u[s]=1;
    for(int i=0;i<n;i++){
        if(u[i]==0 && mp[s][i]>0){
            dfs(i,dis+mp[s][i],team+teams[i]);
        }
    }
    u[s]=0;
}

int main(){
    int i;
    scanf("%d%d%d%d",&n,&m,&st,&en);
    for(i=0;i<n;i++) scanf("%d",&teams[i]);
    memset(mp,-1,sizeof(mp));
    for(i=0;i<m;i++){
        int t1,t2,dis;
        scanf("%d%d%d",&t1,&t2,&dis);
        mp[t1][t2]=mp[t2][t1]=dis;
    }

    //u[st]=1;
    dfs(st,0,teams[st]);

    printf("%d %d\n",shortNum,maxteam);
    return 0;
}

DFS的变形

这道题非常好,可以进一步加深人对DFS的理解。

一般情况下,进行DFS需要int marked[],用于记录是否被访问过,以防止节点被重复访问。

而这道题,需要C2被重复访问,因为你要知道最短路径是什么,所以就不能使用marked数组了。

鉴于该图可能有环,所以一定要确保栈中节点不会被重复访问到,所以需要

    u[s]=1;
    for(int i=0;i<n;i++){
        if(u[i]==0 && mp[s][i]>0){
            dfs(i,dis+mp[s][i],team+teams[i]);
        }
    }
    u[s]=0;

u[s]=1,表明该点已在栈中,只有将该点的后继都访问完之后,才能设置u[s]=0,表明该点已不在栈中

memset

该算法中mp[i][j]为-1时表示不可达,用

memset(mp,-1,sizeof(mp));

来进行设置,这里非常巧,因为如果可能有负权边的话,就不能用-1来表示了,而应该用

memset(mp,0xFF,sizeof(mp));

来设置

算法的正确性

分为两个case,一个是最后能找到C2,则

s==en

为true,更新过最短路径和最多team数之后

return;

另一个是最后找不到C2,则

u[i]==0 && mp[s][i]>0

为false,依次回溯过之后,算法终止。