PAT 甲练习 1003 Emergency
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, 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 Specification:
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
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的更多相关文章
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- 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 ...
- PAT (Advanced Level) 1003. Emergency (25)
最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostrea ...
- 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)
题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...
- PAT甲级1003. Emergency
PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
- 图论 - PAT甲级 1003 Emergency C++
PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...
- PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
随机推荐
- Python 闭包、迭代器、生成器、装饰器
Python 闭包.迭代器.生成器.装饰器 一.闭包 闭包:闭包就是内层函数对外层函数局部变量的引用. def func(): a = "哈哈" def func2(): prin ...
- k8s之资源指标API部署metrics-server
1.部署metrics-server 从v1.8开始,引入了新的功能,即把资源指标引入api,资源指标:metrics-server,自定义指标:prometheus,k8s-prometheus-a ...
- MyBatis 示例-主键回填
测试类:com.yjw.demo.PrimaryKeyTest 自增长列 数据库表的主键为自增长列,在写业务代码的时候,经常需要在表中新增一条数据后,能获得这条数据的主键 ID,MyBatis 提供了 ...
- 怎样使用yum安装nginx
yum install -y nginx 以上.
- 怎样终止HTTP请求
使用 xhr.abort() var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://www.example.com/page.php', tr ...
- JS基础_对象的方法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- scala的泛型浅析
1. scala泛型浅析 package com.dtspark.scala.basics /** * 1,scala的类和方法.函数都可以是泛型. * * 2,关于对类型边界的限定分为上边界和下边界 ...
- docker第一篇 容器技术入门
Container 容器是一种基础工具,泛指任何可以容纳其它物品的工具. Linux Namespaces (docker容器技术主要是通过6个隔离技术来实现) namespace 系统调用参数 ...
- 如何部署struts开发环境
1 首先登陆http://archive.apache.org/dist/struts/source/页面,会看到struts的下载页面 2 下载struts的最新版本struts2-2.2.1-sr ...
- LLVM新建全局变量
在LLVM中,有原生的AST Clone,却没有一个比较好的Stmt copy功能,基于Scout在LLVM上进行的修改,我们实现了自己的Stmt Clone功能. 要进行Stmt Clone,肯定需 ...