PAT - 1003 Emergency (25)

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, C1 C2
N - 城市的数量(城市编号 0 ~ N-1)
M - 道路的数量
C1 - 起点
C2 - 终点
第二行:每个城市救援队的数量
剩下:c1 c2 L
连接城市 c1 与城市 c2 的道路的长度为 L
【输出】C1 到 C2 的最短距离和救援队数量

【数据范围】N <= 500

L没有给范围???

【解题思路】Dijkstra 模板稍微改一下。在更新最短路的时候加一个判断,路径相等但是救援队数量相比较多的情况也可以更新。


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 500+10;
const int INF = 0x3f3f3f3f;
int mat[MAXN][MAXN];
int val[MAXN];
int dis[MAXN], ans[MAXN], path[MAXN];
bool vis[MAXN]; 
int n, m, c1, c2;
void dj() {
    fill(dis, dis+MAXN, INF);
    dis[c1] = 0;
    ans[c1] = val[c1];
    path[c1] = 1;
    for (int i = 0; i < n; i++) {
        int v = -1, minn = INF;
        for (int j = 0; j < n; j++) {
            if (!vis[j] && dis[j] < minn) {
                minn = dis[j];
                v = j;
            }
        }
        if (v == -1) break; 
        vis[v] = 1;
        for (int j = 0; j < n; j++) {
            if (vis[j]) continue;
            if (dis[v] + mat[v][j] < dis[j]) {
                dis[j] = dis[v] + mat[v][j];
                path[j] = path[v];
                ans[j] = ans[v] + val[j]; 
            } else if (dis[v] + mat[v][j] == dis[j]) {
                path[j] += path[v];
                if (ans[v] + val[j] > ans[j])
                    ans[j] = ans[v] + val[j]; 
            }
        }
    }
}

int main() {
    scanf("%d%d%d%d", &n, &m, &c1, &c2);
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
        mat[i][j] = INF;
    for (int i = 0; i < n; i++)
        scanf("%d", &val[i]);
    for (int i = 0; i < m; i++) {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        mat[a][b] = mat[b][a] = c;
    }
    dj();
    printf("%d %d\n", path[c2], ans[c2]);

    return 0;
}

提交时间状态分数题目编译器耗时
2018/7/6 22:33:46答案正确251003C++ (g++)5 ms
2018/7/6 22:27:29部分正确91003C++ (g++)5 ms
2018/7/6 21:59:57答案错误01003C++ (g++)4 ms

模板背得乱七八糟……硬生生 debug 半小时 ˃̣̣̥᷄⌓˂̣̣̥᷅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值