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 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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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算法求最短路+路径还原就好了

Mycode :

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
typedef pair<int,int> pir;
struct edge{int to;int cost;};
vector<edge> E[505];
int s,des;
int n,m;
int d[505];
int num[505];
vector<int> pre[505];
int dijkstra()
{
priority_queue<pir,vector<pir>,greater<pir> >q;
while(!q.empty())q.pop();
fill(d,d+n,inf);
d[s]=0;
pir tmp(0,s);
q.push(tmp);
while(!q.empty())
{ tmp=q.top();q.pop();
int v=tmp.second;
if(d[v]<tmp.first)continue;
for(int i=0;i<E[v].size();i++)
{
int j=E[v][i].to;
if(d[j]>d[v]+E[v][i].cost)
{
pre[j].clear();
d[j]=d[v]+E[v][i].cost;
pre[j].push_back(v);
q.push(pir(d[j],j));
}
else if(d[j]==d[v]+E[v][i].cost)
{
pre[j].push_back(v);
}
}
}
return d[des];
}
int ans1=0;int ans2=0;
void dfs(int now,int total)
{
if(now==s){ans1++;ans2=max(ans2,total+num[now]);return;}
for(int i=0;i<pre[now].size();i++)
{
dfs(pre[now][i],total+num[now]);
}
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d%d%d",&n,&m,&s,&des);
for(int i=0;i<n;i++)scanf("%d",&num[i]);
int p1,p2,cost;
edge tmp;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&p1,&p2,&cost);
tmp.to=p2;tmp.cost=cost;
E[p1].push_back(tmp);
tmp.to=p1;tmp.cost=cost;
E[p2].push_back(tmp);
}
dijkstra();
dfs(des,0);
printf("%d %d\n",ans1,ans2);
return 0;
}

PAT 甲练习 1003 Emergency的更多相关文章

  1. PAT 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

  2. PAT (Advanced level) 1003. Emergency (25) Dijkstra

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  3. PAT (Advanced Level) 1003. Emergency (25)

    最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostrea ...

  4. 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)

    题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...

  5. PAT甲级1003. Emergency

    PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...

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

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

  7. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  8. PAT 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  9. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

随机推荐

  1. H5网页唤醒app,判断app安装

    在阅读本文之前你首先应该对js有基本对掌握,并且对Scheme,intent有一定的理解.更多的是代码 上午给朋友做了一个产品引导页,但是需要判断ios系统的TestFlight是否安装,进行了goo ...

  2. 编写函数实现strcmp( )函数功能

    strcmp(字符串1,字符串2) 作用是比较字符串1和字符串2.两个字符串从左至右逐个字符比较(按照字符的ASCII码值的大小)(即减法比较),直到字符不同或者遇见’\0’为止 如果全部字符都相同, ...

  3. OPENCV运行的问题,自带的程序可以运行,但是自己制作的QT报错

    在PC上跑没问题 qmake 交叉编译后出来的文件 将OPCV和QT的镜像挂载后,在板子上运行程序, 能够出来这个界面,一点击按钮,提示如下错误. . 然而,百度网盘下载的代码.跟上面一样的操作,在板 ...

  4. Windows一键设置环境变量(以设置java环境变量为例)

    右击以管理员方式运行 JDKSetting.bat   @echo off color 0a echo.------------------------------------ echo.TODO:设 ...

  5. PLSQL PL/SQL Developer Oracle 使用技巧 常用设置 卡顿问题 病毒防范( 附带:配置文件)

    相关工具版本: PL/SQL Developer: 9.0.4.1644 Oracle : Oracle Database 10g Enterprise Edition Release 10.2.0. ...

  6. Introduction to Deep Learning Algorithms

    Introduction to Deep Learning Algorithms See the following article for a recent survey of deep learn ...

  7. luogu4677山区建小学题解--区间DP

    题目链接 https://www.luogu.org/problemnew/show/P4677 分析 这道题方法跟之前题不一样,我们相当于枚举一个左右端点来线性扩展,同时划分断点进行决策 \(f[i ...

  8. django form 和modelform样式设置

      目录 1.form通过attr设置属性 2.输入框设置表单状态 3.modelform的使用 4.结合modelform 使用for循环生成输入框 5.基于init构造方法设置样式 6.基本增删改 ...

  9. 14.SpringMVC核心技术-类型转换器

    类型转换器 在前面的程序中,表单提交的无论是 int 还是 double 类型的请求参数,用于处理该请求 的处理器方法的形参, 均可直接接收到相应类型的相应数据,而非接收到 String 再手工转换. ...

  10. IDEA乱码总结和处理

    工程乱码 打开File-Setting, 找到File Encodings这个选项,把encoding设置成你工程的编码即可,一般是UTF-8,如下图(红框的地方),然后重新rebuild一下,基本就 ...