Ladder Tournament L2-001

Ladder Tournament L2-001

  • As the head of a city’s emergency rescue team, you have a special map of the country. On the map, there are multiple scattered cities and some express roads connecting the cities. The number of rescue teams in each city and the length of each expressway connecting the two cities are marked on the map. When other cities have emergency calls to you, your task is to lead your rescue team as soon as possible to the place where the incident occurred, and at the same time, gather as many rescue teams as possible along the way.

    Input format: Input the first line to give 4 positive integers N, M, S, D, where N (2≤N≤500) is the number of cities. By the way, assume that the number of the city is 0 ~
    (N−1); M is the number of expressways; S is the city number of the departure place; D is the city number of the destination.

    The second line gives N positive integers, where the ith number is the number of rescue teams in the ith city, and the numbers are separated by spaces. In the subsequent M lines, each line gives information about an express road, namely: city 1, city 2, and the length of the express road, separated by spaces, and the numbers are all integers and do not exceed 500. The input ensures that the rescue is feasible and the optimal solution is unique.

    Output format: The
    first line outputs the number of shortest paths and the maximum number of rescue teams that can be assembled. The second line outputs the city numbers that pass through the path from S to D. The numbers are separated by spaces, and no extra spaces are allowed at the end of the output.

The optimized dijkstra of priority queue can be used.
Note: The title is an undirected graph. After reading an edge, you need to create a reverse edge

#include<bits/stdc++.h>
using namespace std;
const int N=505*2,M=N*N,INF=1<<30;
typedef pair<int,int>pii;
priority_queue<pii,vector<pii>,greater<pii> >q;
int n,m,s,D,d[N],first[N],qnext[M],u[M],v[M],w[M],h[N],a[N],fa[N],num[N];
bool vis[N];
void print(int x){
    
    
	if(x==fa[x]){
    
    
		cout<<x;
		return;
	}
	print(fa[x]);
	cout<<" "<<x;
}
int main(){
    
    
	cin>>n>>m>>s>>D;
	for(int i=0;i<n;i++){
    
    
		cin>>h[i];
		a[i]=h[i];
		d[i]=INF;
	}
	for(int i=1;i<=m*2;i+=2){
    
    
		cin>>u[i]>>v[i]>>w[i];
		u[i+1]=v[i],v[i+1]=u[i],w[i+1]=w[i];
		qnext[i]=first[u[i]];
		first[u[i]]=i;
		qnext[i+1]=first[u[i+1]];
		first[u[i+1]]=i+1;
	}
	d[s]=0,num[s]=1,fa[s]=s;
	q.push((pii){
    
    0,s});
	while(!q.empty()){
    
    
		pii x=q.top();
		q.pop();
		if(vis[x.second]) continue;
		vis[x.second]=1;
		for(int i=first[x.second];i;i=qnext[i]){
    
    
			int y=v[i];
			if(d[x.second]+w[i]<d[y]){
    
    
				d[y]=d[x.second]+w[i];
				a[y]=h[y]+a[x.second];
				fa[y]=x.second;
				num[y]=num[x.second];
				q.push((pii){
    
    d[y],y});
			}
			else if(d[x.second]+w[i]==d[y]){
    
    
				if(a[y]<h[y]+a[x.second])
					a[y]=h[y]+a[x.second],fa[y]=x.second;
				num[y]+=num[x.second];
			}
		}
	}
//	if(d[D]==INF) return 0;
	cout<<num[D]<<" "<<a[D]<<endl;
	print(D);
	return 0;
} 

Guess you like

Origin blog.csdn.net/u013455437/article/details/109189786