PAT甲级1003 Dijikstra Dfs

1003 Emergency

分数 25

切换布局

作者 CHEN, Yue

单位 浙江大学

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 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 Specification:

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

代码长度限制

16 KB

时间限制

400 ms

内存限制

中文题目
作为城市的紧急救援团队负责人,你将获得一张你所在国家的特殊地图。

该地图显示了一些通过道路连接的分散城市,道路是双向的。

地图上标出了每个城市的救援队数量以及每对城市之间的每条道路的长度。

当其他城市发出紧急求援信息时,你的工作是尽快带领你的士兵前往该地点,同时,在途中尽可能多地调动救援帮手。

输入格式
第一行包含四个整数 N,表示城市数量(城市编号从 0 到 N−1),M 表示道路数量,C1 表示你当前所在的城市编号,C2 表示发出紧急求援信息的城市编号。

第二行包含 N 个整数,其中第 i 个整数表示城市 i 的救援队数量。

接下来 M 行,每行包含三个整数 c1,c2,Li,表示城市 c1 和城市 c2 之间存在一条道路相连,道路长度为 Li。

数据保证 C1 和 C2 之间至少存在一条路径相连。

输出格式
共一行,两个整数,第一个整数表示 C1 和 C2 之间最短路的数量,第二个整数表示走最短路的情况下,能聚集到的救援队最大数量。

数据范围
2≤N≤500,
1≤M≤600,
1≤Li≤200,
每个城市包含的救援人员数量不超过 200。
 

#include<bits/stdc++.h>
using namespace std;

int n,m,c1,c2;
int sum[500];//人数

int distmat[500][500];//距离图 邻接矩阵
vector<int> v[500];//结点图 邻接表
int mindist[500];//最短路结果结点

int zyrenshu;//最优人数
int zypathsize=0;//最短路径数量

//dijikstra,得到所有节点到起始点的最优路径
    //1.一共有n个结点,更新过程n次节点
    //2.(1)更新节点最短距离
    //  (2)更新节点的将要遍历的下一个节点(最短距离节点)
void Dijikstra(){
    //防回数组
    int visited[500] = {0};
    int last = c1;
    mindist[last] = 0;
    for(int i=0;i<n;i++){
        visited[last] = 1;
        //更新下一层节点最短距离
        for(int j=0;j<n;j++) mindist[j] = min(mindist[j],mindist[last]+distmat[last][j]);
        //更新下一层节点的最短距离节点,并更新为last
        int k=-1;
        for(int j=0;j<n;j++) if(!visited[j]&&(k<0||mindist[j]<mindist[k])) k=j;
        last = k;
    }
}

//dfs 找相同长度路径下的到原点路途最大人数路径,引用返回最大路径长度和,
void dfs(int cur,int curdist,int currenshu){
    if(curdist>mindist[cur]) return;//用dijikstra进行剪枝,贪心思路
    if(cur==c2){
        zypathsize++;
        zyrenshu = max(currenshu,zyrenshu);
    }else for(int a:v[cur]) dfs(a,curdist+distmat[cur][a],currenshu+sum[a]);
}

int main(){
    cin>>n>>m>>c1>>c2;
    memset(distmat,0x3f,sizeof(distmat));
    memset(mindist,0x3f,sizeof(mindist));
    for(int i=0;i<n;i++) cin>>sum[i];
    
    int v1,v2,l;
    for(int i=0;i<m;i++){
        cin>>v1>>v2>>l;
        distmat[v1][v2] = l;
        distmat[v2][v1] = l;
        v[v1].push_back(v2);
        v[v2].push_back(v1);
    }
    
    Dijikstra();//返回每个点到达原点的最短路径
    dfs(c1,0,sum[c1]);
    cout<<zypathsize<<" "<<zyrenshu;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值