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

题目大意:求最短路的数量和最短路经过的最大点权

思路:改造dijkstra算法,

  • sumw[i] 表示到达节点i的最短路的最大点权和
  • num[i] 表示到达节点i的最短路的数量
  • 初始化时,都赋值为0
  • 对于start节点,sumw就是其点权,num就是1

核心松弛操作代码如下:如果小于的话,直接把sum加上那个点权,然后num就是原来的值,

如果相等的话,num加上另一路最短路的num值,sum如果也比他大就更新

if(!vis[v]&&dis[v]>dis[u]+cost)
{
dis[v]=dis[u]+cost;
sumw[v]=sumw[u]+weight[v];
num[v]=num[u];///有说道
Q.push(qnode(v,dis[v]));
}
else if(!vis[v]&&dis[v]==dis[u]+cost)
{
num[v]+=num[u];
if(sumw[u]+weight[v]>sumw[v])
{
sumw[v]=sumw[u]+weight[v];
}
Q.push(qnode(v,dis[v]));
}

完整AC代码:

#include<bits/stdc++.h>
#define de(x) cout<<#x<<" "<<(x)<<endl
#define each(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
const int maxn=500+5;
const int inf=0x3f3f3f3f;
int dis[maxn];
int weight[maxn];
int sumw[maxn];
int num[maxn];
bool vis[maxn];
struct Edge
{
int v,c;
Edge(int v,int c):v(v),c(c){}
};
vector<Edge>G[maxn];
struct qnode
{
int v,c;
qnode(int v=0,int c=0):v(v),c(c){}
bool operator<(const qnode&r)const
{
return c>r.c;
}
};
void Dijkstra(int n,int start)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;i++)
{
dis[i]=inf;
sumw[i]=0;
num[i]=0;
}
priority_queue<qnode>Q;
while(!Q.empty())Q.pop();
dis[start]=0;
sumw[start]=weight[start];
num[start]=1;
Q.push(qnode(start,0));///居然忘了push进去了
qnode temp;
while(!Q.empty())
{
temp=Q.top();
Q.pop();
int u=temp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].v;
int cost=G[u][i].c;
if(!vis[v]&&dis[v]>dis[u]+cost)
{
dis[v]=dis[u]+cost;
sumw[v]=sumw[u]+weight[v];
num[v]=num[u];///有说道
Q.push(qnode(v,dis[v]));
}
else if(!vis[v]&&dis[v]==dis[u]+cost)
{
num[v]+=num[u];
if(sumw[u]+weight[v]>sumw[v])
{
sumw[v]=sumw[u]+weight[v];
}
Q.push(qnode(v,dis[v]));
}
}
} }
/*
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
*/
int main()
{
int n,m;
int from,to;
cin>>n>>m>>from>>to;
each(i,0,n-1)cin>>weight[i];
each(i,1,m)
{
int a,b,c;
cin>>a>>b>>c;
G[a].push_back(Edge(b,c));
G[b].push_back(Edge(a,c));
}
Dijkstra(n,from);
printf("%d %d\n",num[to],sumw[to]);
return 0;
}

PAT-1003 Emergency (25 分) 最短路最大点权+求相同cost最短路的数量的更多相关文章

  1. 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 ...

  2. 1003 Emergency (25分) 求最短路径的数量

    1003 Emergency (25分)   As an emergency rescue team leader of a city, you are given a special map of ...

  3. PAT 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  4. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  5. 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)

    题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...

  6. 1003 Emergency (25分)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  7. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  8. PAT 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

  9. PAT 1003 Emergency[图论]

    1003 Emergency (25)(25 分) As an emergency rescue team leader of a city, you are given a special map ...

随机推荐

  1. 粒子群优化算法及其java实现

    憋了两周终于把开题报告憋出来了,再一次证明自己不适合搞学术,哎--,花了点时间把报告中提到的粒子群算法看了看,看了些资料,用java跑起来. 算法简介 粒子群算法最先由Barnhart博士和Kenne ...

  2. Cesium官方教程6--相机

    相机(Camera) 相机控制了场景的观察视角.有很多相机操控方法,比如旋转.缩放.平移以及飞行定位.Cesium默认支持使用鼠标和触摸事件控制相机.Cesium也提供了一套可编程的相机控制API.这 ...

  3. DMA详解

    1.DMA由来DMA(Direct Memory Access,直接存储器访问).在DMA出现之前,CPU与外设之间的数据传送方式有程序传送方式.中断传送方式.CPU是通过系统总线与其他部件连接并进行 ...

  4. swoole的websockte例子

    服务器的环境,建议用bt.cn的一键环境 服务端: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2019\5\2 ...

  5. Chrome和火狐插件让数以百万计用户隐私数据泄露

      https://tech.163.com/19/0721/12/EKK1PRAU00097U7R.html   网易科技讯7月21日消息,据国外媒体报道,流行浏览器诸如广告拦截等扩展功能,已经遭利 ...

  6. Spring MVC Action参数类型 List集合类型(简单案例)

    题目:定义一个员工实体(Employee),实现批量添加员工功能,在表单中可以一次添加多个员工,数据可以不持久化 1,新建一个项目 2, 然后选择Maven框架选择 maven-archetype-w ...

  7. 常规函数模块CALL in new task 报错

    使用START NEW TASK, 函数需要是远程调用模块. 错误:FUNCTION module  ' ZMMFM0021'  cannot be used for 'remote' CALLS. ...

  8. InfluxDB权限认证机制

    一.介绍 权限认证机制,顾名思义,就是对 InfluxDB 数据库添加权限访问控制,在默认情况下,InfluxDB 的权限认证机制是关闭的,也就是说所有用户都有所有权限. 老规矩,直接实践上手,下图是 ...

  9. spark在windows的配置

    在spark-env.cmd添加一行 FOR /F %%i IN ('hadoop classpath') DO @set SPARK_DIST_CLASSPATH=%%i 修改:log4j.prop ...

  10. 解决用cmd编译运行java时的错误

    最近上java课程,平时都是用IDEA打代码的,但老师要我们用cmd编译运行,于是在IDEA撸完代码用cmd编译,但却老是编译不出来,有很多乱码.,提示着“错误:GBK的不可映射字符”,又试了几次,着 ...