PAT-1003 Emergency (25 分) 最短路最大点权+求相同cost最短路的数量
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最短路的数量的更多相关文章
- 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 ...
- 1003 Emergency (25分) 求最短路径的数量
1003 Emergency (25分) As an emergency rescue team leader of a city, you are given a special map of ...
- 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 (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)
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[图论]
1003 Emergency (25)(25 分) As an emergency rescue team leader of a city, you are given a special map ...
随机推荐
- Git的概念和基本使用
概念篇 1. Git简介: 鉴于有些同学可能还不知道Git是什么,我首先对Git做个简短的介绍.Git就是类似于svn的一个版本控制工具,他其实和hg更像一些,hg也是一个分布式版本控制工具,可以说g ...
- ios 新建app iphone 、 ipad or universal ?
很久没有关注这个新建app的 时候 选什么的问题了, 因为我们一般在公司 都是 已经建立好的app 直接 在那上面开发. 所以很久不建立新app 遇到新的app需要你自己去创建的时候 可能就会 有突 ...
- 002-创建型-00-简单工厂【非23种GOF设计模式】
一.概述 简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实 ...
- pycharm重命名文件
先右键要重命名的文件,然后按照下图操作:
- Python扫描器-常用库-Request
1.常用库-Request 1.1. 介绍 #安装:pip3 install requests #各种请求方式:常用的就是requests.get()和requests.post() >> ...
- iOS-app清除缓存
一直寻寻觅觅找app的清除缓存的方法,发现:并没有什么固定的方法,你既然有做对应的缓存机制,这个机制就应该有清除缓存的方法.例如如果你使用某个第三方的图片库,这个库有缓存机制,那么它就应该提供对应的清 ...
- Egret入门学习日记 --- 第一篇 (引擎的选择)
第一篇 (引擎的选择) 我人比较笨,得慢慢学,我就一点一点来好了. 首先,我个人喜欢游戏.网页开发相对游戏开发来说,网页开发实在太枯燥了,没劲.所以打算转游戏开发了. 游戏开发要选择游戏引擎,我去看了 ...
- 【ARM-Linux开发】嵌入式操作系统上的小型数据库移植SQLite
近段时间在学数据库,因为自身需求,所以注重研究了点嵌入式sqlite数据库,SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品 ...
- linux防火墙学习
iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分.可以直接配置,也可以通过许多前端和图形界面配置.语法: iptables(选项)(参数)1,命令选项-t<表 ...
- tomcat性能优化参数
线上环境使用默认tomcat配置文件,性能很一般,为了满足大量用户的访问,需要对tomcat进行参数性能优化,具体优化的地方如下: Linux内核的优化 服务器资源JVM 配置的优化 Tomcat参数 ...