您的位置:首页 > 其它

天梯-L2-001. 紧急救援

2016-07-12 13:32 225 查看

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


1.题面

https://www.patest.cn/contests/gplt/L2-001

2.题意

没啥好说的就是求一个最短路,然后每个城市都有一个消防员的数量,只要路过就会把这批消防员带上,当存在多个最短路的时候希望你能带上尽量多的消防员

题目要求输出最短路径的个数和能带上的最多消防员的个数以及从起点到终点的路径

3.思路

简单地跑一下最短路,同时记录每个点的前驱节点就可以了,要注意更新前驱节点的条件

4.代码

/*****************************************************************
> File Name: Cpp_Acm.cpp
> Author: Uncle_Sugar
> Mail: uncle_sugar@qq.com
> Created Time: 2016年07月12日 星期二 03时21分21秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

const int debug = 1;
const int size = 1000 + 10;
const int INF = INT_MAX>>1;
typedef long long ll;
typedef pair<int,int> dist;

ll w[size];
int p[size];

int PreNode[size];
struct edge{
int nxt,wgt;
edge(){}
edge(int _nxt,int _wgt):nxt(_nxt),wgt(_wgt){}
};
vector<edge> g[size];
int dst[size];
int path[size];
void PrintPath(int no){
if (PreNode[no] == -1)
cout << no;
else {
PrintPath(PreNode[no]);
cout << ' ' << no;
}
}
int main()
{
std::ios::sync_with_stdio(false);cin.tie(0);
int i,j;
int n,m,s,d;
cin >> n >> m >> s >> d;
for (i=0;i<n;i++) cin >> w[i];
while (m--){
int a,b,c;
cin >> a >> b >> c;
g[a].push_back(edge(b,c));
g[b].push_back(edge(a,c));
}
priority_queue<dist,vector<dist>,greater<dist> > pri_que;
pri_que.push(dist(0,s));
fill(dst,dst+n,INF>>1);
fill(p,p+n,0);
dst[s] = 0;path[s] = 1;p[s] = w[s];PreNode[s] = -1;
while (!pri_que.empty()){
dist T = pri_que.top();pri_que.pop();
int t = T.second,d = T.first;
if (dst[t] < d) continue;
for (i=0;i<g[t].size();i++){
edge& f = g[t][i];
if (dst[f.nxt] > dst[t] + f.wgt){
p[f.nxt] = p[t] + w[f.nxt];
dst[f.nxt] = dst[t] + f.wgt;
pri_que.push(dist(dst[f.nxt],f.nxt));
PreNode[f.nxt] = t;
path[f.nxt] = path[t];
}else if (dst[f.nxt] == dst[t] + f.wgt){
if (p[f.nxt] < p[t] + w[f.nxt]){
p[f.nxt] = p[t] + w[f.nxt];
PreNode[f.nxt] = t;
}
path[f.nxt] += path[t];
}
}
}
cout << path[d] << ' ' << p[d] << endl;
PrintPath(d);
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息