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. MySQL 并发事务问题以及事务的隔离级别

    一.并发事务处理带来的问题 相对于串行处理,并发事务(InnoDB)处理能大大增加数据库资源的利用率,提高数据库系统的事务吞吐量,从而可以支持更多用户. 但并发事务处理也会带来一些问题,主要有一下几种 ...

  2. Oracle 表分区介绍与使用

    什么是表分区 分区表是将大表的数据分成称为分区的许多小的子集,类型有FAT32,NTFST32,NTFS.另外,分区表的种类划分主要有:range,list,和hash分区.划分依据主要是根据其表内部 ...

  3. 图解Java继承内存分配

    图解Java继承内存分配   继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法. (3)子 ...

  4. C# 读取本地图片

    /// <summary> /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件 /// </summary> ...

  5. Redis 的订阅发布(PUB/SUB)示例

    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1"); ISubscriber sub ...

  6. 又谈F分布

    今天看到一篇不错的博文,有感,记录下来,相对来说讲到了本质,也很容易理解.https://www.cnblogs.com/think-and-do/p/6509239.html 首先,老生常谈,还是那 ...

  7. [转载]Pytorch详解NLLLoss和CrossEntropyLoss

    [转载]Pytorch详解NLLLoss和CrossEntropyLoss 来源:https://blog.csdn.net/qq_22210253/article/details/85229988 ...

  8. SpringBoot与缓存、消息、检索、任务、安全与监控

    一.Spring抽象缓存 Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口 ...

  9. bootsctrap4 datepicker时间选择插件

    现在网上基本都是v3的时间选择插件,花了点时间改了找了个v4能用的 bootstrap-datepicker <!DOCTYPE html> <html> <head&g ...

  10. 在线预览word、excel文件

    直接使用微软提供的在线预览服务. 免费 文件必须为网可访问地址,因为微软的服务器需要访问该文件