pat 团体天梯赛 L2-001. 紧急救援
L2-001. 紧急救援
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。
输入格式:
输入第一行给出4个正整数N、M、S、D,其中N(2<=N<=500)是城市的个数,顺便假设城市的编号为0~(N-1);M是快速道路的条数;S是出发地的城市编号;D是目的地的城市编号。第二行给出N个正整数,其中第i个数是第i个城市的救援队的数目,数字间以空格分隔。随后的M行中,每行给出一条快速道路的信息,分别是:城市1、城市2、快速道路的长度,中间用空格分开,数字均为整数且不超过500。输入保证救援可行且最优解唯一。
输出格式:
第一行输出不同的最短路径的条数和能够召集的最多的救援队数量。第二行输出从S到D的路径中经过的城市编号。数字间以空格分隔,输出首尾不能有多余空格。
输入样例:
4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2
输出样例:
2 60
0 1 3 思路:dijkstra算法。设立num[i]和w[i]表示从出发点到i结点拥有的路的条数,以及能够找到的救援队的数目,当判定dis[u] + e[u][v] < dis[v]的时候,不仅仅要更新dis[v],还要更新num[v] = num[u], w[v] = weight[v] + w[u]; 如果dis[u] + e[u][v] == dis[v],还要更新num[v] += num[u],而且判断一下是否权重w[v]更小,如果更小了就更新w[v] = weight[v] + w[u];
再设立一个pre[i]表示最短路径的前一个结点,在dis[u] + e[u][v] <= dis[v]的时候更新pre[v] = u 用优先队列的最短路做法过不了第二个测试点,没想明白怎么回事,代码贴着:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
#define INF 0x3f
#define EPS 1e-5
#define pi cos(-1)
const int N_MAX = +;
struct edge {
int to, cost;
edge() {}
edge(int to,int cost):to(to),cost(cost) {}
};
struct P {
int first, second;//first是最短路径,second 是顶点编号
P() {}
P(int first,int second):first(first),second(second) {}
bool operator < (const P&b)const {
return first < b.first;
}
};
int N, M, S, D;
int V;
int d[N_MAX];
int Prev[N_MAX];//求前驱结点
vector<edge>G[N_MAX];
int num[N_MAX];//从出发点到点i的最短路径条数
int w[N_MAX];//从出发点到点i的最大救援队数目
int weight[N_MAX];
void dijkstra(int s) {
priority_queue<P>que;
fill(d, d + V, INF);
fill(Prev,Prev+V,-);
d[s] = ;
num[s] = ;
w[s] = weight[s];
que.push(P(,s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (d[v] < p.first)continue;
for (int i = ; i < G[v].size();i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
num[e.to] = num[v];
w[e.to] = w[v] + weight[e.to];
Prev[e.to] = v;
que.push(P(d[e.to], e.to));
}
else if (d[e.to] == d[v] + e.cost) {
num[e.to] += num[v];
if (w[e.to] < w[v] + weight[e.to]) {
w[e.to] = w[v] + weight[e.to];
Prev[e.to] = v;
}
}
}
}
} vector<int>get_path(int t) {
vector<int>path;
for (; t != -;t=Prev[t]) {
path.push_back(t);
}
reverse(path.begin(),path.end());
return path;
} int main() {
cin >> N >> M >> S >> D;
V = N;
for (int i = ; i < N;i++) {
cin >> weight[i];
}
for (int i = ; i < M;i++) {
int from, to, cost;
cin >> from >> to >> cost;
G[from].push_back(edge(to, cost));
G[to].push_back(edge(from, cost));
}
dijkstra(S);
cout << num[D] << " " << w[D]<< endl;
vector<int>path = get_path(D);
for (int i = ; i < path.size();i++) {
cout << path[i];
if (i + == path.size())cout << endl;
else cout << " ";
}
return ;
}
AC代码(bellman——ford):
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
#define INF 0x3f
#define EPS 1e-5
#define pi cos(-1)
const int N_MAX = +;
int N, M, S, D;
int V;
int cost[N_MAX][N_MAX];
bool used[N_MAX];
int d[N_MAX];
int Prev[N_MAX];//求前驱结点
int num[N_MAX];//从出发点到点i的最短路径条数
int w[N_MAX];//从出发点到点i的最大救援队数目
int weight[N_MAX];
void dijkstra(int s) {
memset(d, 0x3f, sizeof(d));
memset(used, , sizeof(used));
memset(Prev, -, sizeof(Prev));
d[s] = ;
num[s] = ;
w[s] = weight[s];
while () {
int v = -;
for (int u = ; u < V;u++) {
if (!used[u] && (v == - || d[u] < d[v])) {
v = u;
}
}
if (v == -)break;
used[v] = true;
for (int u = ; u < V;u++) {
if (d[u] > d[v] + cost[v][u]) {
d[u] = d[v] + cost[v][u];
Prev[u] = v;//
num[u] = num[v];
w[u] = w[v] + weight[u];
}
else if (d[u] == d[v] + cost[v][u]) {//!!
num[u] += num[v];
if (w[u] < w[v] + weight[u]) {
w[u] = w[v] + weight[u];
Prev[u] = v;
}
}
}
}
} vector<int>get_path(int t) {
vector<int>path;
for (; t != -;t=Prev[t]) {
path.push_back(t);
}
reverse(path.begin(),path.end());
return path;
} int main() {
cin >> N >> M >> S >> D;
V = N;
for (int i = ; i < N;i++) {
cin >> weight[i];
}
memset(cost, 0x3f, sizeof(cost));
for (int i = ; i < M;i++) {
int from, to, c;
cin >> from >> to >> c;
cost[from][to] = c;
cost[to][from] = c;
}
dijkstra(S);
cout << num[D] << " " << w[D]<< endl;
vector<int>path = get_path(D);
for (int i = ; i < path.size();i++) {
cout << path[i];
if (i + == path.size())cout << endl;
else cout << " ";
}
return ;
pat 团体天梯赛 L2-001. 紧急救援的更多相关文章
- pat 团体天梯赛 L3-007. 天梯地图
L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...
- pat 团体天梯赛 L3-015. 球队“食物链”
L3-015. 球队“食物链” 时间限制 1000 ms 内存限制 262144 kB 代码长度限制 8000 B 判题程序 Standard 作者 李文新(北京大学) 某国的足球联赛中有N支参赛球队 ...
- pat 团体天梯赛 L1-039. 古风排版
L1-039. 古风排版 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 中国的古人写文字,是从右向左竖向排版的.本题就请你编写 ...
- pat 团体天梯赛 L2-012. 关于堆的判断
L2-012. 关于堆的判断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的小顶堆H[] ...
- pat 团体天梯赛 L3-010. 是否完全二叉搜索树
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- pat 团体天梯赛 L3-009. 长城
L3-009. 长城 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 邓俊辉(清华大学) 正如我们所知,中国古代长城的建造是为了抵御外 ...
- pat 团体天梯赛 L2-011. 玩转二叉树
L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...
- pat 团体天梯赛 L2-010. 排座位
L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...
- pat 团体天梯赛 L2-007. 家庭房产
L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产 ...
随机推荐
- java面向对象思想1
1.面向对象是面向过程而言.两者都是一种思想.面向过程:强调的是功能行为.(强调过程.动作)面向对象:将功能封装进对象,强调了具备了功能的对象.(强调对象.事物)面向对象是基于面向过程的.将复杂的事情 ...
- unix环境高级编程一书中部分错误处理函数
#include <unistd.h> #include <errno.h> #include <string.h> #include <stdio.h> ...
- phpstorm —— Xdebug 的配置和使用
给phpstorm 配置Xdebug(Xdebug 是 PHP 的一个扩展, 用于帮助调试和开发.它包含一个与 ide 一起使用的单步调试器.它升级了 PHP 的 var_dump () 功能) 这篇 ...
- Nginx读书笔记
... upstream proxy_svrs { server http://192.168.1.1:8001/uri/; server http://192.168.1.2:8001/uri/; ...
- OpenFaceswap 入门教程(3): 软件参数篇!
OpenFaceswap 的使用可以说是非常简单,只要稍加点拨就可以学会,厉害一点的人根本不需要教程,直接自己点几下就知道了.看了前面安装篇和使用篇.我想大多数人应该会了. 当学会了使用之后,你可能对 ...
- Centos7(Linux)下安装VMware12
https://blog.csdn.net/u012605477/article/details/65627234
- Air Pollution【空气污染】
Air Pollution Since the 1940s, southern California has had a reputation for smog. 自20世纪40年代以来,南加利福尼亚 ...
- stm32基本定时器timer6的原理与使用
/********************基本定时器 TIM 参数定义,只限 TIM6.7************/ /* 一.定时器分类 STM32F1 系列中,除了互联型的产品,共有 8 个定时器 ...
- JS 对于回调函数的理解,和常见的使用场景应用,使用注意点
很经常我们会遇到这样一种情况: 例如,你需要和其他人合作,别人提供数据,而你不需要关注别人获取或者构建数据的方式方法. 你只要对这个拿到的数据进行操作. 这样,就相当于我们提供一个外在的函数,别人 ...
- ECharts的x轴和y轴均使用数值类型
今天有个需求,就是需要ECharts的x轴和y轴都要使用数值类型,即xAxis.type和yAxis.type均为value,然后我按照我以为的方式修改了下,发现图崩了 发现问题: 然后我打开了ECh ...