【PAT 甲级】1003 Emergency (25)(25 分)(dfs,dijkstra)

题目链接

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

题意:
N个城市,M条路,给定N个城市中的救援人员个数,M条连接城市的路和对应距离,求C1到C2救援的最短路共有多少条,有多少救援人员。

思路:最短路
1.dfs
2.dijkstra算法

代码【dfs】:

#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 5e2+5;
const ll INF = 0x3f3f3f3f;

int num[N];
int numShorPath,MaxAmou,d=INF;
struct Edge {
    int v,w;
    Edge() {}
    Edge(int v,int w):v(v),w(w) {}
};
vector<Edge>g[N];
bool vis[N];
void init() {
    for(int i=0; i<N; i++) {
        vis[i]=0;
    }
}
void dfs(int u,int v,int dist,int MA) {
    if(u==v) {
        if(dist==d) {
            numShorPath++;
            if(MA>MaxAmou) {
                MaxAmou=MA;
            }
        } else if(dist<d) {
            numShorPath=1;
            d=dist;
            MaxAmou=MA;
        }
        return ;
    }
    for(int i=0; i<g[u].size(); i++) {
        Edge next=g[u][i];
        int to=next.v,w=next.w;
        if(vis[to])
            continue;
        vis[to]=1;
        dfs(to,v,dist+w,MA+num[to]);
        vis[to]=0;
    }
}
int main() {
    int n,m,C1,C2;
    cin>>n>>m>>C1>>C2;
    for(int i=0; i<n; i++)
        cin>>num[i];
    init();
    for(int i=0; i<m; i++) {
        int u,v,w;
        cin>>u>>v>>w;
        g[u].push_back({v,w});
        g[v].push_back({u,w});
    }
    dfs(C1,C2,0,num[C1]);
    cout<<numShorPath<<" "<<MaxAmou<<endl;
    return 0;
}

2.dijkstra算法
代码:

猜你喜欢

转载自blog.csdn.net/feng_zhiyu/article/details/80854581
今日推荐