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. 【Python基础】09_Python中的元组

    1.元组的定义 Tuple (元组)与列表类似,元组的元素 不能修改 元组通常保存 不同类型 的数据 元组用()定义 info_tuple = ("张三", 18, 1.75) 定 ...

  2. S02_CH03_EMIO实验Enter a post title

    S02_CH03_EMIO实验 3.1 EMIO 和MIO的对比介绍 上次讲到MIO的使用,初步熟悉了EDK的使用,这次就来说说EMIO的使用.如你所见zynq的GPIO,分为两种,MIO(multi ...

  3. WPF 位图处理相关类

    位图的存储方式 开始之前,先了解下位图的存储方式 位图的像素都分配有特定的位置和颜色值.每个像素的颜色信息由RGB组合或者灰度值表示,根据位深度,可将位图分为1.4.8.16.24及32位图像等.每个 ...

  4. AtCoder Beginner Contest 144 题解

    传送门 $cf$ 自闭了,打 $abc$ 散散心 A - 9x9 ...这个有什么好讲的吗,题目看懂就会做了 #include<iostream> #include<cstdio&g ...

  5. 牛客 197C 期望操作数

    大意: 给定$x,q$, 每步操作$x$等概率变为$[x,q]$中任意一个数, 求变为$q$的期望操作数. 很容易可以得到$f(x,q)=\frac{\sum\limits_{i=x+1}^qf(i, ...

  6. dl in image process

    deep learning目前为止无论在分类还是检测上,都是整体的处理,而不会出现像sift这样的局部特征,这个问题或许如果能克服掉,能让检测效果更进一大步.

  7. 常用javascript内置对象——String对象

    创建 String 对象的语法: 1:new String(s); :2:String(s); :3:直接赋值 String中属性 String中方法 <script> window.on ...

  8. Partial的应用

    Partial是局部类型的意思.允许我们将一个类.结构或接口分成几个部分,分别实现在几个不同的.cs文件中.C#编译器在编译的时候仍会将各个部分的局部类型合并成一个完整的类 局部类型的注意点1. 局部 ...

  9. c#泛型约束(转载)

    博客地址:https://www.cnblogs.com/zhengwk/p/5541921.html 六种类型的约束: T:结构 类型参数必须是值类型.可以指定除 Nullable 以外的任何值类型 ...

  10. 使用高德地图JS获取当前位置和经纬度

    先看效果,我做的是这样的,可以按地图位置来返回当前你点的位置(图一,二),也可以根据输入框的自动搜索(图三,四) HTML的代码: <div> <input type="t ...