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
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
For each test case, print in one line two numbers: the number of diferent 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条路相连,每个城市都有一些救援队,给定起点城市和C1和终点城市C2,求从起点到终点最短路径的条数以及最短路径上能够调动来的救援队的数目。
解题思路:和一般求最短路的题目不同,这里所求的是最短路的数目和最短路上点的权值和(将城市看做点,那么每个城市中救援队的数目就可以看成点的权值)。其实既然是最短路,无非就是那几个算法,这里我们来考虑最短路数目不唯一的原因,从起点到终点的边的权值和是一样的,但经过的点是不一样的,这可以用Dijkstra来对每一个点逐步贪心,判断是否需要纳入到最短路结点的集合中。这里用dis[i]表示从起点C1到i点最短路的路径长度,用num[i]表示从起点到i点最短路的个数,用w[i]表示从起点到i点救援队的数目之和。当判定dis[u] + e[u][v] < dis[v]的时候,也就是说新纳入的u点可以减短到v的路径长度,是符合要求的,不仅仅要更新dis[v],还要更新num[v] = num[u], w[v] = weight[v] + w[u]; 如果dis[u] + e[u][v] ==dis[v],也就是说新纳入的u点,经过u到达v的路径长度和不经过u到v的路径长度一致,这就产生了两条最短路了,这时候要更新num[v] += num[u],⽽且判断⼀下是否权重w[v]更⼩,如果更⼩了就更新w[v] = weight[v] + w[u];
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const int inf= ;
using namespace std;
int n,m,c1,c2;
int e[][];
int w[];//救援队数目之和
int dis[];//从起点到i点最短路径的长度
int num[];//最短路径的条数
int weight[];//第i个城市救援队数目,点权
bool vis[];
int main()
{
int a,b,c,i,j,mins,u,v;
scanf("%d%d%d%d",&n,&m,&c1,&c2);
for(i=; i<n; i++)
{
scanf("%d",&weight[i]);
}
fill(e[], e[] + * , inf);
fill(dis, dis + , inf);
for(i=; i<m; i++)//建图
{
scanf("%d%d%d",&a,&b,&c);
e[a][b]=e[b][a]=c;
}
dis[c1]=;
w[c1]=weight[c1];
num[c1]=;//至少会有一条最短路
for(i=; i<n; i++)//遍历所有的点
{
u=-;//因为是正权通路,这里设置为-1
mins=inf;
for(j=; j<n; j++)//找到距离已纳入点集合中的最近点
{
if(vis[j]==false&&dis[j]<mins)
{
u=j;
mins=dis[j];
}
//printf("%d\n",mins);
} if(u==-)
{
break;
}
vis[u]=true;//标记该点以纳入S集
for(v=; v<n; v++)//更新刚纳入的u点与其他尚未纳入点之间的距离
{
if(vis[v]==false&&e[u][v]!=inf)
{
if(dis[u]+e[u][v]<dis[v])//Dijkstra
{
dis[v]=dis[u]+e[u][v];//更新边权
num[v]=num[u];//最短路条数不会变
w[v]=w[u]+weight[v];//最短路沿路的救援队数
}
else if(dis[u]+e[u][v]==dis[v])//出现边权相等
{
num[v]=num[v]+num[u];//产生了两条分路
if(w[u]+weight[v]>w[v])//取点权最大的表示为到v的救援队数目
{
w[v]=w[u]+weight[v];
}
}
}
}
}
printf("%d %d",num[c2],w[c2]);
return ;
}
PAT 1003 Emergency 最短路的更多相关文章
- PAT 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003 Emergency
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of ...
- PAT 1003 Emergency[图论]
1003 Emergency (25)(25 分) As an emergency rescue team leader of a city, you are given a special map ...
- PAT 1003. Emergency 单源最短路
思路:定义表示到达i的最短路径数量,表示到达i的最短径,表示最短路径到达i的最多人数,表示从i到j的距离, 表示i点的人数.每次从u去更新某个节点v的时候,考虑两种情况: 1.,说明到达v新的最短路径 ...
- 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 C++
PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...
- PAT甲级1003. Emergency
PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
随机推荐
- SoC的软件开发流程,主要包含一些Linux下的操作命令
该笔记主要记录SoC的软件开发流程,主要包含一些Linux下的操作命令 1. 编写design file .c .h 2. 编写makefile 可执行文件名,交叉编译环境,compile fl ...
- hdu 2955 Robberies (01背包好题)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 【CHRIS RICHARDSON 微服务系列】微服务架构中的进程间通信-3
编者的话 |本文来自 Nginx 官方博客,是微服务系列文章的第三篇,在第一篇文章中介绍了微服务架构模式,与单体模式进行了比较,并且讨论了使用微服务架构的优缺点.第二篇描述了采用微服务架构的应用客户端 ...
- Caffe源码-Net类(上)
Net类简介 Net类主要处理各个Layer之间的输入输出数据和参数数据共享等的关系.由于Net类的代码较多,本次主要介绍网络初始化部分的代码.Net类在初始化的时候将各个Layer的输出blob都统 ...
- 【SHOI 2007】善意的投票
Problem Description 幼儿园里有 \(n\) 个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾 ...
- SAP B1:如何在水晶报表中插入二维码
动态二维码API接口地址:http://www.liantu.com/api.php?text=x备注: 动态网址内可自定义相应的字段拼接(如图5为 [批号]+[质检员]字段) 若API接口链接失效, ...
- 微信小程序—支付宝身份验证(支付宝小程序)
查看应用:https://open.alipay.com/platform/keyManage.htm 这里找到您调用接口的应用 支付宝身份验证快速接入:https://docs.open.alip ...
- Gemini.Workflow 双子工作流高级教程:对外API控制引擎:总述
前言: 双子工作流提供了一套对外的API,用于控制整体系统运转,下面就来看看介绍,其实很简单的. 对外API控制引擎总介: Gemini.Workflow 双子工作流,对外提供的API,都在Gemin ...
- Eureka 入门,带视频
疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] 前言 Crazy ...
- swoole如何使php永久运行
有需要学习交流的友人请加入交流群的咱们一起,有问题一起交流,一起进步!前提是你是学技术的.感谢阅读! 点此加入该群jq.qq.com soole可以通过开启守护进程使PHP永久运行. 守护进程化.设 ...