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 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, 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 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 C1​ to C2​ .

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

思路

这题其实挺简单的,而且我也算是二刷了。第一次做的时候套了《2013年王道论坛计算机考研机试指南》中的Dijkstra的代码,但是实质上啥也不懂,就是硬背。虽然那本书讲得也很好,但是相比于《算法笔记》来说,我觉得对我这种没啥基础的理解起来比较难。《算法笔记》用一个攻占城市的例子来讲解Dijkstra算法,这种方法我觉得很棒,对于理解算法的思想有很大的帮助,然后理解完算法的核心思想之后再看对应的代码的话,就会非常舒服。

因此,我这次用的是《算法笔记》里的代码模板写的(尽量不要硬背哈,理解性地记忆会发现Dijkstra的核心代码其实也就这么点,然后再根据题目要求改改就行了)。

题目的大致意思是,给你几个城市(顶点)和几个城市之间的边的距离(边权),每个城市呢还有它自己的救援队(点权),然后再给出一个起点和终点,让你算出起点和终点之间最短距离有几条(样例的话就是2条)以及这些最短路径中能收集到的物资最大数是多少(也就是问点权和最大是多少,样例的话是4)。

因此,只要在优化更新d[v]的时候,把相应的标尺条件加进去就行了。

具体是这样的:

for(int j=0;j<G[u].size();j++){
	int v = G[u][j].v;
    if(vis[v]==false){
    	if(d[u]+G[u][j].dis<d[v]){//第一标尺是距离
    		d[v] = d[u]+G[u][j].dis;//更新d[v]
            w[v] = w[u]+weight[v];
            num[v] = num[u];
        }
        else if(d[u]+G[u][j].dis==d[v]){//第二标尺是物资
            if(w[u]+weight[v]>w[v]) w[v] = w[u]+weight[v];
            num[v] += num[u];
        }
    }
}

这里讲一下我做题的时候发生的错误:
①不要忘了加vis[v]==false这句话,在得到顶点u访问其相邻顶点v的时候,一定要先判断v是否已被访问;
②如果像这个题目一样有多个要求(又要记录最短路径条数,又要记录最短路径上的点权最大之和),那就干脆全部分开来写,判断条件只写距离。我一开始第二个条件写:

else if(d[u]+G[u][j].dis==d[v]&&w[u]+weight[v]>w[v]){
	w[v] = w[u]+weight[v];
	num[v] += num[u];
}

然后就WA了两个测试点,原因是这么写的话,最短路径的条数更新会有问题,按原理来说,只需要d[u]+G[u][j].dis==d[v]就更新最短路径的条数,而我这么写的话,还要多满足w[u]+weight[v]>w[v]这个条件,才会更新最短路径的条数。
其他就没什么问题啦,这题也是个很基础的Dijkstra的题目~

代码

#include<cstdio>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
const int maxn = 501;
const int INF = 123123123;
struct node{
    int v;//目标点
    int dis;//边权
};
int num[maxn] = {0};//最短路径条数
int weight[maxn] = {0};//每个城市的物资数
int w[maxn] = {0};//记录从起点到每个城市的最大物资
int d[maxn] = {0};
bool vis[maxn] = {false};
vector<node> G[maxn];
void Dijkstra(int n, int s){//顶点数n,起点s
    fill(d, d+maxn, INF);//初始化所有点的最短距离都是INF
    d[s] = 0;
    num[s] = 1;//s到s有一条最短路
    w[s] = weight[s];
    for(int i=0;i<n;i++){
        int u = -1;
        int MIN = INF;
        for(int j=0;j<n;j++){
            if(vis[j]==false&&d[j]<MIN){
                u = j;
                MIN = d[j];
            }
        }
        if(u==-1) return;
        vis[u] = true;
        for(int j=0;j<G[u].size();j++){
            int v = G[u][j].v;
            if(vis[v]==false){
                if(d[u]+G[u][j].dis<d[v]){//第一标尺是距离
                    d[v] = d[u]+G[u][j].dis;//更新d[v]
                    w[v] = w[u]+weight[v];
                    num[v] = num[u];
                }
                else if(d[u]+G[u][j].dis==d[v]){//第二标尺是物资
                    if(w[u]+weight[v]>w[v]) w[v] = w[u]+weight[v];
                    num[v] += num[u];
                }
            }
        }
    }
}
int main(){
    int N, M, C1, C2;//顶点数N,边数M,起点C1,终点C2
    scanf("%d%d%d%d", &N, &M, &C1, &C2);
    for(int i=0;i<N;i++) scanf("%d", &weight[i]);
    for(int i=0;i<M;i++){
        int a, b, tmpdis;
        scanf("%d%d%d", &a, &b, &tmpdis);
        node tmp;
        tmp.dis = tmpdis;
        tmp.v = b;
        G[a].push_back(tmp);
        tmp.v = a;
        G[b].push_back(tmp);//因为是无向图,所以两端都要放
    }
    Dijkstra(N, C1);
    printf("%d %d", num[C2], w[C2]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值