1003. Emergency (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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最短路)的更多相关文章

  1. PAT甲级专题|最短路

    PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做, ...

  2. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  3. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  4. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  5. 图论 - PAT甲级 1003 Emergency C++

    PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...

  6. PAT甲级1111. Online Map

    PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...

  7. PAT甲级1087. All Roads Lead to Rome

    PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...

  8. PAT甲级1076. Forwards on Weibo

    PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...

  9. PAT甲级1018. Public Bike Management

    PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...

随机推荐

  1. 自制MVC框架原理介绍

    当初用jsp开发程序时,因为很多东西写在一起混淆的,项目做大或者变更的时候就会很吃力,联动性太大,有时修改视图的东西都可能会影响业务逻辑,分层不明确. 后来听说了Struts MVC,做过几个示例,层 ...

  2. SWFupload在IE9以上中的bug

    这几天在做图片上传的东西,是用swfupload是出现了再IE9下那选择文件的按钮无法点击的情况,在其他浏览器,例如Firefox.chrome都不会出现,后来google一下才发下这算是IE9以上和 ...

  3. Python读取键盘输入

    Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘.例如以下: raw_input input raw_input函数 raw_input() 函数从标准输入读取一个行.并返回 ...

  4. context.Request.Files post 上传问题件

    [无刷新上传] 要实现文件上传,form必须设置几个属性:1.action:设为要处理数据的页面地址:2.method:设为"post":3.enctype/encoding:必须 ...

  5. 当 ftp 遇上 http Proxy

    在asp.net 开发中,有时需要使用到ftp 上传文件, 如果客户电脑使用http proxy 上网, 那么,客户电脑在使用ftp上传文件时,可能会出现以下错误: 使用 HTTP Proxy 時,不 ...

  6. apache与和mysql重启命令

    修改linux服务器的http配置之后,必须重启Apache服务. 命令为: /etc/rc.d/init.d/httpd restart chown -R mysql:mysql 目录名 改变文件属 ...

  7. Registers

    https://github.com/JesusFreke/smali/wiki/Registers Introduction In dalvik's bytecode, registers are ...

  8. cacheManager载入问题

    net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please pr ...

  9. tensorflow 之模型的保存与加载(一)

    怎样让通过训练的神经网络模型得以复用? 本文先介绍简单的模型保存与加载的方法,后续文章再慢慢深入解读. #!/usr/bin/env python3 #-*- coding:utf-8 -*- ### ...

  10. jquery实现页面的搜索功能

    $(function(){ $("input[type=button]").click(function(){ var txt=$("input[type=text]&q ...