PAT1003. Emergency (25)

来源:互联网 发布:excel输入数据 编辑:程序博客网 时间:2024/06/18 03:48

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


这个题卡了我一个星期,以为会使用迪杰斯特拉,就把dfs,bfs,迪杰斯特拉,普里姆全部过了一遍,然而一个dfs就可以解决。

#include<iostream>#include<algorithm>#include<string>using namespace std;const int maxn = 501;const int mx = 0x3f3f3f3f;int mp[maxn][maxn];bool vis[maxn];int val[maxn];int n, m, s, e, temp, cnt, num;     ///cnt为最短路径数量, num为点权值最大void init()         ///初始化{    for(int i=0;i<n;++i)        vis[i] = false;    for(int i=0;i<n;++i)        for(int j=0;j<n;++j)            mp[i][j] = mx;}void dfs(int st, int dis, int v)        ///st为当前点,dis为已经在地图的距离, v为当前点的权值{    if(st == e)             ///如果到达e点,结束    {        if(dis < temp)        {            temp = dis;            cnt = 1;            num = v;        }        else if(dis == temp)        {            cnt++;            if(num < v)                num = v;        }        return;    }    if(dis > temp)      ///剪枝        return;    for(int i=0;i<n;++i)    {        if(mp[st][i] != mx && !vis[i])        {            vis[i] = true;            dfs(i, dis+mp[st][i], v+val[i]);            vis[i] = false;        }    }}int main(){    ios::sync_with_stdio(false);    cin>>n>>m>>s>>e;    init();    for(int i=0;i<n;++i)        cin>>val[i];    for(int i=0;i<m;++i)    {        int a, b, c;        cin>>a>>b>>c;        if(mp[a][b] > c)            mp[a][b] = mp[b][a] = c;    }    temp = mx;    cnt = 0;    num = 0;    dfs(s, 0, val[s]);    cout<<cnt<<" "<<num<<endl;    return 0;}


原创粉丝点击