前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT(甲级)1003.Emergency(25)

PAT(甲级)1003.Emergency(25)

作者头像
lexingsen
发布2022-02-25 08:01:29
1900
发布2022-02-25 08:01:29
举报
文章被收录于专栏:乐行僧的博客

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.

输入格式: 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.

输出格式: For each test case, print in one line two numbers: the number of different shortest paths between C1 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.

输入样例:

代码语言:javascript
复制
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

输出样例:

代码语言:javascript
复制
Case #1:
2 4

题目分析:求解单源最短路径的条数,以及在某条路径上的最大点权之和。本题的图是无向图,如果代码里写成了有向图会出现测试点错误。代码参考晴神的模板。

AC代码:

代码语言:javascript
复制
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;


const int maxv = 510;
const int inf = 0x3fffffff;
int weight[maxv];
int d[maxv],num[maxv],w[maxv];
bool visited[maxv] = {false};
int n, m;

struct Node{
    int v, dis;
    Node(int v, int dis){
        this->v = v;
        this->dis = dis;
    }
};
vector<Node> G[maxv];

void dijkstra(int s){
    for(int i=0; i<maxv; ++i){
        d[i] = inf;
        w[i] = 0;
        num[i] = 0;
    }

    d[s] = 0;
    w[s] = weight[s];
    num[s] = 1; 
    for(int i=0; i<n; ++i){
        int u=-1,MIN=inf;
        for(int j=0; j<n; ++j){
            if(visited[j]==false && d[j]<MIN){
                MIN = d[j];
                u = j;
            }
        }
        if(u==-1)return;
        visited[u] = true;

        for(int j=0; j<G[u].size(); ++j){
            int v = G[u][j].v;
            if(visited[v]==false && d[u] + G[u][j].dis < d[v]){
                d[v] = d[u]+G[u][j].dis;
                w[v] = w[u] + weight[v];
                num[v] = num[u];
            }
            else if(visited[v]==false && 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 st,ed;//源点和汇点
    scanf("%d%d%d%d", &n, &m, &st, &ed);
    for(int i=0; i<n; ++i){
        scanf("%d", &weight[i]);
    }

    int u,v,we;
    for(int i=0; i<m; ++i){
        scanf("%d%d%d",&u,&v,&we);
        G[u].push_back(Node(v,we));
        G[v].push_back(Node(u,we));
    }
    dijkstra(st);
    printf("%d %d\n",num[ed], w[ed]);
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档