Dijstra 紧急救援 -2025/11/4

#include<bits/stdc++.h>
using namespace std;
#define PII pair<int,int>
const int N = 510;
const int INF = 1e9;
int n,m,s,d;
int num[N],dis[N],last[N],sum[N],path[N];
int vis[N];

int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	vector<PII> v[N];
	cin >> n >> m >> s >> d;
	for(int i = 0; i < n; i++) cin >> num[i];
	int from,to,w;
	for(int i = 1; i <= m; i++) {
		cin >> from >> to >> w;
		v[from].push_back({to,w});
		v[to].push_back({from,w});
	}
	
	for(int i = 0; i <= 510; i++){
		dis[i] = 0x3f;
		last[i] = -1;
		sum[i] = num[i];
		path[i] = 1;
	}
	dis[s] = 0;
	
	priority_queue<PII,vector<PII>,greater<PII> > q;
	q.push({0,s});
	
	while(!q.empty()){
		auto u = q.top();
		q.pop();
		int distance = u.first, ver = u.second;
		if(vis[u.second]) continue;
		vis[u.second] = 1;
		
		for (auto [to, w] : v[ver]) {
			if(dis[to] > distance + w){
				last[to] = ver;
				dis[to] = distance + w;
				path[to] = path[ver];
				sum[to] = sum[ver] + num[to];
				q.push({dis[to],to});
			}else if(dis[to] == distance + w){
				path[to] += path[ver];
				if(sum[to] < num[to] + sum[ver]){
					sum[to] = num[to] + sum[ver];
					last[to] = ver;
					q.push({dis[to],to});
				}
			}
		}
	}
	
//	for(int i = 0; i <= 3; i ++){
//		cout << "last[i]::" << last[i] << "\n"; 
//	}
	
	
	cout << path[d] << " " << sum[d] << "\n";
	
	vector<int> ans;
	int g = d;
	while(g != -1){
		ans.push_back(g);
		g = last[g];
	}
	for(int i = ans.size() - 1; i >= 0; i--){
		if(i != ans.size() - 1) cout << " ";
		cout << ans[i];
	}
	
	return 0;
}

posted @ 2025-11-05 21:15  XYu1230  阅读(2)  评论(0)    收藏  举报