PAT 甲级 1003Emergency(Dijkstra最短路)
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
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 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
题目的意思要求求出最短路的个数,和点的权值最大的值
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue> using namespace std;
const int maxn=1e9;
struct Nod
{
int value;
int next;
int weight;
}edge[505*2];
int head[505];
int tot;
void add(int x,int y,int z)
{
edge[tot].value=y;
edge[tot].weight=z;
edge[tot].next=head[x];
head[x]=tot++;
}
int vis[505];
int d[505];
int num[505];
int res[505];
int w[505];
int n,c1,c2,m;
struct Node
{
int pos;
int dis;
Node(){};
Node(int pos,int dis)
{
this->pos=pos;
this->dis=dis;
}
friend bool operator <(Node a,Node b)
{
return a.dis>b.dis;
}
};
void Dijkstra(int st)
{
priority_queue<Node> q;
q.push(Node(st,0));
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
memset(res,0,sizeof(res));
for(int i=0;i<=500;i++)
d[i]=1e9;
d[st]=0;num[st]=1;res[st]=w[st];
while(!q.empty())
{
Node term=q.top();
q.pop();
if(vis[term.pos])
continue;
vis[term.pos]=1;
for(int i=head[term.pos];i!=-1;i=edge[i].next)
{
int y=edge[i].value;
if(d[y]>term.dis+edge[i].weight)
{
num[y]+=num[term.pos];
res[y]=res[term.pos]+w[y];
d[y]=term.dis+edge[i].weight;
q.push(Node(edge[i].value,d[y]));
}
else if(d[y]==term.dis+edge[i].weight)
{
num[y]+=num[term.pos];
res[y]=max(res[y],res[term.pos]+w[y]);
}
}
}
} int main()
{
scanf("%d%d%d%d",&n,&m,&c1,&c2);
for(int i=0;i<n;i++)
scanf("%d",&w[i]);
int x,y,z;
memset(head,-1,sizeof(head));
tot=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
Dijkstra(c1);
printf("%d %d\n",num[c2],res[c2]);
return 0;
}
PAT 甲级 1003Emergency(Dijkstra最短路)的更多相关文章
- PAT甲级专题|最短路
PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做, ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- 图论 - 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甲级1111. Online Map
PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
- PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- PAT甲级1076. Forwards on Weibo
PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
随机推荐
- echart初体验 动态加载数据
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Spring学习笔记(四)-- Spring事务全面分析
通过本系列的文章对Spring的介绍,我们对Spring的使用和两个核心功能IOC.AOP已经有了初步的了解,结合我个人工作的情况,因为项目是金融系 统.那对事务的控制是不可缺少的.而且是很严格的控制 ...
- [gj]三国攻势图
三国攻势图 参考: 估计你对三国故事有点兴趣,我给你看看图说三国(大概的):
- TextView中超链接拦截
TextView中的超链接点击时,其实是通过Intent方式的,因此会调用Activity中的startActivity(Intent intent)方法,所以可在此方法中做些简单的拦截操作 例如拦截 ...
- Android Studio怎样import module(针对非gradle)
相同的,非gradle编译的project和gradle编译的在import module上相同有一些差别. 包含操作上,显示上的一些差别,曾经的文章中,仅仅要没有标注"非gradle&qu ...
- 关于.pyc文件
Python会在执行.py文件的时候,将.py形式的程序编译成中间式文件(byte-compiled)的.pyc文件,这么做的目的就是为了加快下次执行文件的速度. 所以,在我们运行python文件的时 ...
- iOS9新特性
本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着有用户陆续升级 ...
- mysql替换成指定字符
,,, ), 'XXXX' )-- 隐藏从第四位开始的6个字符,包括第四个字符,替换成X
- OpenCV3.1.0+VS2013配置+Win10(64位)(转载)
OpenCV3.1.0+VS2013配置+Win10(64位) [环境]VS2013和MATLAB相互调用混合编程 Matlab 2016a和VS2013混合Dll编程步骤 更换了硬盘之后,重新配置了 ...
- STM32F10x_RTC秒中断
Ⅰ.概述 RTC(Real Time Clock)是实时时钟的意思,它其实和TIM有点类似,也是利用计数的原理,选择RTC时钟源,再进行分频,到达计数的目的. 该文主要讲述关于RTC的秒中断功能,这个 ...