PAT (Advanced Level) Practice 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条边的无向图,求给定起始点到终点的最小路径花费最小路径数目,并输出最大所经点权值和
思路:500个点的dijkstra应该可过,设置三个数组,dis[i]表示起始点到第i个点的最小值,cnt[i]表示起始点到达第i个点的最短路条数,person[i]就是起始点到达第i个点最大所经点权和,val[i][j]表示i到j的边权,num[i]表示第i个点点权~
在dijktstra运行过程中每次循环是找出取出的一个未被标记并且到起始点距离最小的点u,然后以u为起始点找u的出边所连的点去更新dis数组,在这中间
如果dis[u]+val[u][v]<dis[v],那么就更新dis[v],且cnt[v]=cnt[u],person[v]=person[v]+person[u];
如果dis[u]+val[u][v]=dis[v],那么cnt[v]=cnt[v]+cnt[u],person[v]=max(person[v],person[u]+num[i]);
最后输出cnt[终点]和person[终点]就好~
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 505
#define lb(x) (x&(-x))
const double eps = 1e-6;
using namespace std;
inline ll read()
{
char ch = getchar(); ll s = 0, w = 1;
while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
return s * w;
}
inline void write(ll x)
{
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 + 48);
}
ll n,m,c1,c2;
ll val[maxn][maxn],num[maxn],dis[maxn],cnt[maxn],person[maxn],vis[maxn];
//dis[i]表示到达i点的最小距离,cnt[i]表示到达i点最短路的条数,person[i]表示到达i点能召集最大人手数
inline void dijkstra()
{
fill(dis,dis+maxn,inf);
dis[c1]=0,cnt[c1]=1,person[c1]=num[c1];
for(rg i=0;i<n;i++)
{
ll u=-1,minn=inf;
for(rg j=0;j<n;j++)
{
if(dis[j]<minn&&!vis[j])
{
minn=dis[j],u=j;
}
}
if(u==-1)break;
vis[u]=1;
//cout<<u<<endl;
for(rg v=0;v<n;v++)
{
if(val[u][v]!=inf&&!vis[v])
{
if(val[u][v]+dis[u]<dis[v])
{
dis[v]=val[u][v]+dis[u];
person[v]=person[u]+num[v];
cnt[v]=cnt[u];
}
else if(val[u][v]+dis[u]==dis[v])
{
person[v]=max(person[v],person[u]+num[v]);
cnt[v]+=cnt[u];
}
}
}
}
}
int main()
{
cin>>n>>m>>c1>>c2;
fill(val[0],val[0]+maxn*maxn,inf);
for(rg i=0;i<n;i++)num[i]=read();
for(rg i=0;i<m;i++)
{
ll a,b,c;
a=read(),b=read(),c=read();
val[a][b]=val[b][a]=c;
}
dijkstra();
cout<<cnt[c2]<<" "<<person[c2]<<endl;
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Oracle非图形化界面创建数据库
- Mycat分库分表全解析 Part 5 Mycat 分片规则介绍
- PHP 命名空间与类自动加载实现
- mysqldump命令详解 Part 3-备份单表
- mysqldump命令详解 Part 7- -single-transaction 参数的使用
- 基于 Symfony 组件封装 HTTP 请求响应类
- 通过 PHP 原生代码实现 HTTP 路由器
- MySQL组复制(MGR)全解析 Part 10 MGR新增节点
- 基于 gorilla/sessions 在 Go 语言中管理 Session
- 通过 PHP 原生代码实现 HTTP 控制器
- Go 视图模板篇(一):模板引擎的定义、解析与执行
- Mycat分库分表全解析 Part 6 Mycat 全局序列号
- 通过 PHP 原生代码实现视图模板引擎的解析和渲染
- [MySQL故障处理]记一次innobackupex导致的从库无法同步的问题
- [Oracle 故障处理]记一次DG数据文件无法创建的问题