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 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
随机推荐
- “ResGen.exe”已退出,代码为2 问题处理
这属于VS2010不能编译.Net3.5的问题 用VS2010创建了一个.Net 3.5的Winform项目,结果编译失败,这个问题也算是第二次碰到了,真纠结···这次不再偷懒了,把解决方法记录下来吧 ...
- SSL and SSL Certificates Explained
Secure Sockets Layer (SSL) and Transport Layer security (TLS ) are protocols that provide secure com ...
- Heap 3214 LIS题解
依据问题转换成最长不降子序列问题. 10^9的输入数据计算起来还是挺花时间的.由于这里仅仅能使用O(nlgn)时间复杂度了. 只是证明是能够算出10^9个数据的. 由于时间限制是5s. #includ ...
- python3 logging 日志记录模块
#coding:utf-8 import logginglogging.basicConfig(filename='log1.log', format='%(asctime)s -%(name)s-% ...
- php-fig组织psr标准
psr规范 基本代码规范,本篇规范制定了代码基本元素的相关标准,以确保共享的PHP代码间具有较高程度的技术互通性. 关键词 “必须”(MUST). “一定不可.一定不能”(MUST NOT). “需要 ...
- unity, UGUI Text outline
UGUI Text的勾边效果是通过添加component实现的: Add Component->UI->Effects->Outline 参考:http://www.cnblogs. ...
- 定时检测Memcached进程是否存在,若不存在自动启动它
由于一台WEB服务器的Memcached死掉而导致在访问网站的某些页面时候打不开.下面脚本会自动检测Memcached的进程,如果挂掉则自动重启Memcached服务. vim memcached_c ...
- memcache操作实例
实例一: <?php //使用memcache类来操作 $mm = new Memcache(); $mm->addServer("192.168.70.114",11 ...
- apache设置头
Apache 及开启压缩及Header信息隐藏:http://centilinux.blog.51cto.com/1454781/792820
- atitit.验证码识别step4--------图形二值化 灰度化
atitit.验证码识别step4--------图形二值化 灰度化 1. 常见二值化的方法原理总结 1 1.1. 方法一:该方法非常简单,对RGB彩色图像灰度化以后,扫描图像的每个像素值,值小于12 ...