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

 

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int INF = 10000001;//无限大数
const int MAX = 5001;//最大点数

struct Node
{
    //下一个节点 节点 路径长度
    int next,to,len;
}e[MAX];

vector<int> rescue;//储存每个城市的救援人数
bool vis[MAX] = {false};
int d[MAX];//路径
int w[MAX] = {0};//点权 救援人数
int num[MAX] = {0};//条数

int head[MAX];
int cnt = 0;

//构建邻接表
void add(int x,int y,int length){
    e[cnt].next = head[x];
    e[cnt].to = y;
    e[cnt].len = length;
    head[x] = cnt;

    cnt++;
}

void Dijkstra(int StartCity,int N){
    fill(d,d + MAX,INF);//填充大数

    d[StartCity] = 0;//StartCity -> StartCity的最短路径
    w[StartCity] = rescue[StartCity];//StartCity的救援人数
    num[StartCity] = 1;//路径条数

    for(int i = 0;i < N;i++){
        int u = -1,min = INF;
        for(int j = 0;j < N;j++){
            if(vis[j] == false && d[j] < min){//查找路径最短的
                min = d[j];
                u = j;
            }
        }

        if(d[u] == -1)return;//如果没有课访问的 结束
        vis[u] = true;//u点标志已访问

        for(int j = head[u];j != -1;j = e[j].next){//遍历u的邻接表
            if(vis[e[j].to] == false && d[u] + e[j].len <= d[e[j].to]){
                if(d[u] + e[j].len < d[e[j].to]){//要路径更短的
                    d[e[j].to] = d[u] + e[j].len;//更短路径
                    w[e[j].to] = rescue[e[j].to] + w[u];//点权 救援++

                    //更新
                    num[e[j].to] = num[u];//求最短的路径有几种走法 并不是一条最短的路径有几条小的路径
                }else if(d[u] + e[j].len == d[e[j].to]){//相等时
                    if(w[u] + rescue[e[j].to] > w[e[j].to])w[e[j].to] = w[u] + rescue[e[j].to];//要救援更多的

                    num[e[j].to] += num[u];//e[j].to 原本的最短条数 + num[u]
                }
            }
        }
    }
}
int main(int argc, char const *argv[]) {
    //城市数 无向边 起始城市 目标城市
    int N,M,StartCity,AimCity;

    scanf("%d%d%d%d",&N,&M,&StartCity,&AimCity);

    //输入物资
    for(int i = 0;i < N;i++){
        int a;
        scanf("%d",&a);

        rescue.push_back(a);
    }

    fill(head,head + MAX,-1);//将head填满-1;

    for(int i = 0;i < M;i++){
        int x,y,length;
        scanf("%d%d%d",&x,&y,&length);

        //构建邻接表 无向图两次
        add(x,y,length);
        add(y,x,length);
    }

    Dijkstra(StartCity,N);//迪杰斯特拉搜索最短路径

    printf("%d %d",num[AimCity],w[AimCity]);

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值