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. WebViewJavaScriptBridge的原理解析

    理解WebViewJavaScriptBridge原理 前提条件都是需要bridge在OC实例化,然后二者的互调才可以进行下去 _bridge = [WebViewJavascriptBridge b ...

  2. 牛客小白月赛5 G 异或(xor) 【找规律】

    题目链接: https://www.nowcoder.com/acm/contest/135/g 题目描述 从前,Apojacsleam家的水族箱里,养了一群热带鱼. 在这几条热带鱼里,Apojacs ...

  3. git 命令汇总

    本地库处理 git init 初始化仓库 git clone [地址] 下载项目 git status 查看当前暂存等状态 git add 添加暂存 cat .git/config 查看git配置 l ...

  4. 搭建zipkin参数配置

    Environment Variables zipkin-server is a drop-in replacement for the scala query service. yaml confi ...

  5. JZOJ 3388. 【NOIP2013模拟】绿豆蛙的归宿

    3388. [NOIP2013模拟]绿豆蛙的归宿 (Standard IO) Time Limits: 1000 ms  Memory Limits: 131072 KB  Detailed Limi ...

  6. django开发傻瓜教程-1-安装和HelloWorld

    安装 sudo pip install Django 新建项目 django-admin startproject XXX 启动项目 进入主目录下 python manage.py runserver ...

  7. Bomb HDU - 3555 (数位DP)

    Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...

  8. 水题:HDU-1088-Write a simple HTML Browser(模拟题)

    解题心得: 1.仔细读题,细心细心...... 2.题的几个要求:超过八十个字符换一行,<br>换行,<hr>打印一个分割线,最后打印一个新的空行.主要是输出要求比较多. 3. ...

  9. Spring---环境搭建与包介绍

    jar包下载路径 首先需要下载Spring框架 spring-framework-5.0.0.RELEASE-dist,官方地址为https://repo.spring.io/release/org/ ...

  10. Go语言之并发编程(二)

    通道(channel) 单纯地将函数并发执行是没有意义的.函数与函数间需要交换数据才能体现并发执行函数的意义.虽然可以使用共享内存进行数据交换,但是共享内存在不同的goroutine中容易发生竞态问题 ...