L2-001. 紧急救援

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。

输入格式:

输入第一行给出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. 紧急救援的更多相关文章

  1. pat 团体天梯赛 L3-007. 天梯地图

    L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...

  2. pat 团体天梯赛 L3-015. 球队“食物链”

    L3-015. 球队“食物链” 时间限制 1000 ms 内存限制 262144 kB 代码长度限制 8000 B 判题程序 Standard 作者 李文新(北京大学) 某国的足球联赛中有N支参赛球队 ...

  3. pat 团体天梯赛 L1-039. 古风排版

    L1-039. 古风排版 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 中国的古人写文字,是从右向左竖向排版的.本题就请你编写 ...

  4. pat 团体天梯赛 L2-012. 关于堆的判断

    L2-012. 关于堆的判断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的小顶堆H[] ...

  5. pat 团体天梯赛 L3-010. 是否完全二叉搜索树

    L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...

  6. pat 团体天梯赛 L3-009. 长城

    L3-009. 长城 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 邓俊辉(清华大学) 正如我们所知,中国古代长城的建造是为了抵御外 ...

  7. pat 团体天梯赛 L2-011. 玩转二叉树

    L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...

  8. pat 团体天梯赛 L2-010. 排座位

    L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...

  9. pat 团体天梯赛 L2-007. 家庭房产

    L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产 ...

随机推荐

  1. Symmetric Difference-freecodecamp算法题目

    Symmetric Difference 1.要求 创建一个函数,接受两个或多个数组,返回所给数组的对等差分(symmetric difference) 例子:给出两个集合 (如集合 A = {1, ...

  2. MySQL - GROUP_CONCAT 使用方法

    如上图,我想把结果集中的三行链接成一行,则可这样写:   总结: GROUP_CONCAT函数默认是用','逗号链接,如果你加上第二个参数,则以',第二个参数值'逗号+第二个参数值链接,如下图     ...

  3. vue-cli的build的文件夹下没有dev-server.js文件,怎么配置mock数据

    因为最新版本的vue-cli已经放弃dev-server.js,只需在webpack.dev.conf.js配置就行 新版webpack.dev.conf.js配置如下: 在const portfin ...

  4. 背景透明度处理 兼容IE

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 打开POST传参的弹出窗口

    //穿件 function openPostPopWindow(url,param,target){ var $form = $("<form></form>&quo ...

  6. python面向对象之反射和内置方法

    一.静态方法(staticmethod)和类方法(classmethod) 类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性) 静态方法:让类里的方法直接被 ...

  7. Python 文件操作Error: binary mode doesn't take an encoding argument

    Python 报错:ValueError: binary mode doesn't take an encoding argument 在运行文件操作相关功能时报错:ValueError: binar ...

  8. exec , 元类,__new__, __call__ , 单例模式 , 异常

    1,类也是对象 ''' 动态语言 可以在运行期间 动态生成类 修改对象属性 静态语言 ''''' ''' type(object_or_name, bases, dict) type(object) ...

  9. 动态规划:HDU2571-命运

    解题心得: 1.其实是一个简单的动态规划加上贪心的思想,思路简单,只需要求每一步的最大值就可以了,但是要注意读懂题. 2.走的规则:从左上角开始走,达到右下角,只能向右走一步,或者向下走一步,或者走列 ...

  10. luogu2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

    ref #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...