PAT A1003 Emergency

本文探讨了在一个由城市和道路组成的网络中寻找最短路径并沿途收集最多资源的算法。通过Dijkstra算法的改进版,不仅计算了从起点到终点的最短距离,还考虑了路径上的资源数量,实现了在最短时间内聚集最多资源的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提交时间状态分数题目编译器耗时用户
2018/11/28 16:39:05

答案正确

251003C++ (g++)5 msDirichlet
测试点结果耗时内存
0答案正确3 ms376KB
1答案正确3 ms404KB
2答案正确3 ms384KB
3答案正确5 ms504KB
4答案正确5 ms384KB
5答案正确4 ms384KB

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

易错点已在代码中标注;

#include <iostream>
#include <algorithm>
#include <cstring>
//易错点:
//1.判断==
//2.既有路径条数又有最大物资时,更新最短路径结构改变
//3.访问过要标注vis[u]=true;
using namespace std;

const int inf=1000000000;
const int maxn=505;
struct Node{
    int data;
    int lenth;
};
vector<Node> Adj[maxn];
int n,num[maxn],weight[maxn],w[maxn];//城市个数,路径条数,每城人数,当前最大人数
int m,c1,c2;//路径条数,起点,终点
int d[maxn];//路径
bool vis[maxn] = {false};

void Dijkstra(int c){
    //初始化
    fill(num,num+maxn,0);
    fill(w,w+maxn,0);
    fill(d,d+maxn,inf);
    num[c]=1;
    w[c]=weight[c];
    d[c]=0;
    //找出当前未遍历的距离最近的点
    for(int i=0; i<n; i++){
        int MIN=inf, u=-1;
        for(int j=0; j<n; j++){
            if(vis[j]==false && MIN>d[j]){
                u=j;
                MIN=d[j];
            }
        }
        if(u==-1) return;
        vis[u]=true;//这里易忘
        //验证是否可以优化
        for(int j=0; j<Adj[u].size(); j++){
            int v=Adj[u][j].data;
            if(vis[v]==false){
                if(d[v]>d[u]+Adj[u][j].lenth){
                    d[v]=d[u]+Adj[u][j].lenth;
                    w[v]=w[u]+weight[v];
                    num[v]=num[u];
                }
                else if(d[v]==d[u]+Adj[u][j].lenth){//这里易错,路径条数与人数无关
                    if(w[v]<w[u]+weight[v]){
                        w[v]=w[u]+weight[v];
                    }
                    num[v]+=num[u];
                }
            }
        }
    }
}

int main()
{
    cin>>n>>m>>c1>>c2;
    for(int i=0; i<n; i++){
        cin>>weight[i];//输入城市人数
    }
    int p1,p2,L;//路径两端及其长度
    Node tmp;
    for(int i=0; i<m; i++){//建图
        cin>>p1>>p2>>L;
        tmp.data=p2;
        tmp.lenth=L;
        Adj[p1].push_back(tmp);
        tmp.data=p1;
        Adj[p2].push_back(tmp);
    }

    Dijkstra(c1);//找出从c1出发所有最短路径

    cout<<num[c2]<<" "<<w[c2];
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值