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. echart初体验 动态加载数据

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. unity5, UI Button "On Button Down"

    unity5自带的UI Button的Inspector面板中只有On Click事件,如果我们想让一个按钮响应On Button Down事件该怎么办呢?方法是: 点Add Component-&g ...

  3. Java服务CPU占用高问题定位方法

    1. 概述 提供一种简单的方法来定位CPU高的问题. 找到CPU高的进程,比如232543: 执行top -H -p pid,找到占用CPU最高的线程号,比如232544,转换成16进制38c60: ...

  4. Atitit. 软件开发中的管理哲学--一个伟大的事业必然是过程导向为主 过程导向 vs 结果导向

    Atitit. 软件开发中的管理哲学--一个伟大的事业必然是过程导向为主    过程导向 vs 结果导向 1. 一个伟大的事业必然是过程导向为主 1 1.1. 过程的执行情况(有明确的执行手册及标准) ...

  5. C#将URL中的参数转换成字典Dictionary<string, string>

    /// <summary> /// 将获取的formData存入字典数组 /// </summary> public static Dictionary<String, ...

  6. @property (nonatomic,retain)中的nonatom和retain的意思

    转自:http://yuanshoupeng2005.blog.163.com/blog/static/68235027201235113952886/ http://baike.baidu.com/ ...

  7. 0049 MyBatis关联映射--一对一关系

    世上的事务总不是孤立存在的,表现在Java类里面,则是类与类之间的关系,比如继承is-a.依赖use-a.关联has-a,反映在数据库中,则是表与表之间的关系,比如外键 关联关系存在着以下几种类型:一 ...

  8. [搬运]CORBA中BOA和POA的含义

    先来BOA,搬自:http://www.cl.cam.ac.uk/research/dtg/attarchive/omniORB/doc/2.8/omniORB2/node6.html The Bas ...

  9. java web 打水印

    /** * 把图片印刷到图片上 * * @param pressImg -- * 水印文件 * @param targetinp -- * 目标文件 * @param x * --x坐标 * @par ...

  10. JAVA中使用P和Q分量计算N和D进行RSA运算

    最近在使用Java中需要使用PQ形式的私钥进行RSA加解密运算,本来以为Java中应该很多类似的例子,发现所有的例子都是从ND形式的私钥,竟然没有人用分量P和Q计算N和D进行运算.对Java使用RSA ...