L2-001 城市间紧急救援

L2-001 城市间紧急救援

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

输入格式:

输入第一行给出4个正整数NMSD,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0 ~ (N−1);M是快速道路的条数;S是出发地的城市编号;D是目的地的城市编号。

第二行给出N个正整数,其中第i个数是第i个城市的救援队的数目,数字间以空格分隔。随后的M行中,每行给出一条快速道路的信息,分别是:城市1、城市2、快速道路的长度,中间用空格分开,数字均为整数且不超过500。输入保证救援可行且最优解唯一。

输出格式:

第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从SD的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct dijkstra{
	vector<ll>dis;
	vector<ll>pre;
	vector<ll>sum;
	int cnt_path=0,d=0;
	dijkstra() {}
	dijkstra(vector<vector<pair<int,ll>>> &adj, vector<ll> &cnt, int s, int d){
		this->d=d;
		dis.assign(adj.size(),(ll)1e18); 
		pre.assign(adj.size(),-1);
		sum.assign(adj.size(),0);
		priority_queue<tuple<int,ll,ll>> q;
		q.emplace(0,s,cnt[s]);
		dis[s]=0;
		pre[s]=s;
		sum[s]=cnt[s];
		while(!q.empty()){
			auto t = q.top();
			q.pop();
			if(dis[get<1>(t)] != get<0>(t)) continue;
			if(dis[get<1>(t)] == get<0>(t) && get<1>(t) == d) cnt_path++;
			for(auto edge : adj[get<1>(t)]){
				if(dis[edge.first] > dis[get<1>(t)] + edge.second){
					if(edge.first == d) cnt_path=0;
					sum[edge.first]=sum[get<1>(t)] + cnt[edge.first];
					dis[edge.first] = dis[get<1>(t)] + edge.second;
					pre[edge.first] = get<1>(t);
					q.emplace(dis[edge.first], edge.first, sum[edge.first]);
				}else if(dis[edge.first] == dis[get<1>(t)] + edge.second ){
					if(sum[edge.first]<sum[get<1>(t)] + cnt[edge.first]){
						sum[edge.first]=sum[get<1>(t)] + cnt[edge.first];
						pre[edge.first] = get<1>(t);
					}
					q.emplace(dis[edge.first], edge.first, sum[get<1>(t)] + cnt[edge.first]);
				}
				
			}
		}
	}
	void print_path(int s, int t){
		if(pre[t] == -1) return (void)(cout<<"error");
		if(s == t){
			if(t != d) cout<<t<<" ";
			else cout<<t;
			return;
		}
		print_path(s,pre[t]);
		if(t != d) cout<<t<<" ";
		else cout<<t;
	}
};
int main(){
	cout.sync_with_stdio(false);
	cout.tie(nullptr);
	int n, m, s, d;
	cin>>n>>m>>s>>d;
	vector<ll>cnt(n);
	for(int i=0;i<n;i++) cin>>cnt[i];
	vector<vector<pair<int,ll>>> adj(n);
	for(int i=0;i<m;i++){
		int a,b;ll c;
		cin>>a>>b>>c;
		adj[a].emplace_back(b,c);
		adj[b].emplace_back(a,c);
	}
	dijkstra ans(adj, cnt, s, d);
	cout<<ans.cnt_path<<" "<<ans.sum[d]<<'\n';
	ans.print_path(s,d);
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值