PAT-甲级-1003(Emergency)

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 Specification:

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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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个点,从0 ~ n-1编号,给定每个点的权值,再给m条路,以及距离,问最短路有多少条,在这些最短路中哪条路上的权值最大?是多少?

#include <bits/stdc++.h>
#define fin freopen("in.txt", "r", stdin)
using namespace std;
typedef long long ll;
const int maxn = 5e2+5;
struct node {
    int t, l;   //team, length
    node(int x, int y) : t(x), l(y) {}

};

struct P {
    int t, l;    //target, length
    P(int x, int y) : t(x), l(y) {}

};

bool v[maxn];

int ans_road = 0, ans_team = 0;
vector<node> road;    //存结果,这时候结构体里是:t:num_team, l: length
int s, e;             //起始点、目标点
vector<node> p[maxn]; //存图,这时候结构体里是:t: target, l: length
int team[maxn];
//dfs找所有路径
//cur表示当前点,t表示当前获得的权值,len表示路径长度
void dfs(int cur, int t, int len) {
    if(cur == e) {
        road.push_back(node(t, len));   //将所有可行路径放进road里面
        return ;
    }
    for(int i = 0; i < p[cur].size(); i++) {
        if(!v[p[cur][i].t]) {
            v[p[cur][i].t] = true;
            dfs(p[cur][i].t, t + team[p[cur][i].t], len + p[cur][i].l);
            v[p[cur][i].t] = false;
        }
    }
} 

void output() {
    int min_road = 10000000;
    //找最短路径距离
    for(int i = 0; i < road.size(); i++) min_road = min(min_road, road[i].l);   
    //找最短路径条数以及最大权值
    for(int i = 0; i < road.size(); i++) if(road[i].l == min_road) ans_road++, ans_team = max(ans_team, road[i].t);
    cout << ans_road << " " << ans_team << "\n";
}

int main() {
    int n, m;
    cin >> n >> m >> s >> e;
    for(int i = 0; i < n; i++) cin >> team[i];
    for(int i = 0; i < m; i++) {
        int u, v, l;
        cin >> u >> v >> l;
        p[u].push_back(node(v, l)), p[v].push_back(node(u, l));
    }
    dfs(s, team[s], 0);
    output();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值