1003 Emergency (25分) 求最短路径的数量
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 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
题意:n个城市m条路,每个城市有救援小组,联通城市之间的距离已知。
给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和最大的那个 题解:和求Disjktra多重权值一样,
在更新中间节点路径更短的时候,顺带更新救援小组的数量
当最短路径相同的时候,更新最短路径条数
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 1000000
using namespace std;
int a[][];
int num[];//每个村庄有多少个救援队
int dis[];//起点S到村庄j的最短距离
int val[];//起点S到村庄j可以聚集救援队的数量
int vis[];
int cnt[];//起点S到村庄j的最短路径有多少条
void dijkstra(int start, int n)
{
int i, j, k, min;
for (i = ; i < n; i++)//(初始化)存放起点到其余顶点的距离
dis[i] = a[start][i]; dis[start] = ;
val[start] = num[start];
cnt[start] = ; for (i = ; i < n; i++)
{
min = MAX;
k = -;
for (j = ; j < n; j++) //求出初始起点s直接到j点距离最短的点的下标值
{
if (vis[j]== && min > dis[j])
{
min = dis[j];
k = j;
}
}
vis[k] = ;
if (k == -)
return;
for (j = ; j <= n; j++)
{
if (dis[j] > dis[k] + a[k][j])//若找到其他途径比从1号顶点直接到目的顶点的距离短,则替换掉
{
cnt[j]=cnt[k];
dis[j] = dis[k] + a[k][j];
val[j]=num[j]+val[k];
} else if (dis[j] == dis[k] + a[k][j])//如果距离相同
{
cnt[j]=cnt[j]+cnt[k];
val[j] = max(val[j],val[k] + num[j]);//更新救援队数量
}
}
}
} int main()
{
int n, m;
int i;
int s, e;
cin>>n>>m>>s>>e;
for(int i=;i<n;i++)
cin>>num[i];
int t1,t2,t3;
memset(vis, , sizeof(vis));
memset(cnt, , sizeof(cnt));
memset(cnt, , sizeof(val));
memset(a, MAX, sizeof(a));//初始化所有点的距离/花费为无穷大
for (i = ; i < m; i++)
{
scanf("%d%d%d", &t1, &t2, &t3);
if (a[t1][t2] > t3)//去重
a[t1][t2] = a[t2][t1] = t3;
}
dijkstra(s, n);
printf("%d %d\n", cnt[e], val[e]);
return ;
}
1003 Emergency (25分) 求最短路径的数量的更多相关文章
- PAT 1003 Emergency (25分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)
题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...
- 1003 Emergency (25分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- PAT 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- 1003 Emergency (25)(25 point(s))
problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
随机推荐
- web学习---html,js,php,mysql一个动态网页获取流程
使用bootstrap的cms模版系统搭建了一个信息管理系统.通过这个系统学习动态网页获取的工作流程. 抓包分析一个页面的数据请求流程如下图所示: 同样,对于需要向数据库插入数据,可以使用ajax接口 ...
- 【做题笔记】P2251 质量检测
一看题就知道是线段树裸题了.可是,对于每个 \(i\) ,对应的 \(Q\) 序列的下标是多少呢?应该查询的区间又是什么呢? 找规律: \(i\ \ \ \ \ \ \ \ m\) \(1\ \Rig ...
- Could not initialize class net.sourceforge.tess4j.TessAPI 解决方法
java.lang.NoClassDefFoundError: Could not initialize classnet.sourceforge.tess4j.TessAPI 主要原因是在Windo ...
- python报错使用yum命令报错File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: SyntaxError: invalid syntax问题
参考链接:https://blog.csdn.net/ltz150/article/details/77870735 1.背景: CentOS 7升级Python到3.6.2后,需要在/usr/bin ...
- Flask 学习之flask入门
一.Flask的简单介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请 ...
- TensorFlow:使用inception-v3实现各种图像识别
程序来自博客: # https://www.cnblogs.com/felixwang2/p/9190740.html上面这个博客是一些列的,所以可以从前往后逐一练习. # https://www.c ...
- Java面向对象编程 -5
代码块 在程序之中使用"{}"定义的结构就称为代码块,而后根据代码块出现的位置以及定义的关键字的不同, 代码块可以分为 普通代码块 构造代码块 静态代码块 同步代码块 其中同步代码 ...
- SDNU_ACM_ICPC_2020_Winter_Practice_2nd
A - [The__Flash]的矩阵 给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大. Input输入数据的第一行为一个正整数T,表示有T组测试数据.每一组测试数据 ...
- Linux - 常用GUI软件
1. gdebi -- 可以代替Ubuntu software安装软件 2. System monitor -- 监控流量 3. uget -- 下载软件 4. Okular -- pdf reade ...
- js中substr、substring、indexOf、lastIndexOf的用法
substr substr(start,length)表示从start位置开始,截取length长度的字符串 var str="imgs/header_2.jpg"; consol ...