[PTA] between city emergency

As head of emergency rescue team of a city, you have a special map of the country. Show on the road map has more rapid dispersion of the city and some of the connections to the city. And a number of rescue teams per each city connecting the two cities fast road length is marked on the map. While other cities have emergency phone call to you, your mission is to lead your rescue team rushed to the incident as soon as possible, at the same time along the way to convene as many rescue teams.

Input formats:

Input of the first line gives four positive integers N, M, S, D, where N (2≤N≤500) is the number of the city, the city assumed way numbered 0 ~ (N-1); M is fast several roads; S is the point of departure cities numbered; D is the number of urban destinations.

The second line gives a positive integer N, which is the number of i-th i rescue team of cities, separated by spaces between numbers. Subsequent M rows, each row gives a fast way of information, namely: 1 urban, urban length of 2, fast road, separated by a space intermediate, figures are an integer of not more than 500. Input to ensure a viable and optimal solution unique rescue.

Output formats:

The number of shortest path first line of output and maximum number of rescue teams able convened. The second line is output from D to S path through the city number. Between numbers separated by spaces, the end of the output can not have extra space.

Sample input:

4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2

Sample output:

2 60
0 1 3

100 ++ AND

#include<iostream>
#include <bits/stdc++.h>
#define Inf 0x3f3f3f3f
using namespace std;
int n,m,s,d;
int mp[505][505];//存放路径
int pace[505];//最短路径条数
int vis[505];//标记是否访问过
int num[505];
int sum[1001];//
int pre[1001];//前驱路径

void Dijkstra(){
	pace[s]=vis[s]=1;

	for(int i=0;i<n-1;i++){
		int MIN=Inf,f=-1;
		/***找距离源点最近的,且未被访问**/
		for(int j=0;j<n;j++){
			if(!vis[j]&&mp[s][j]<MIN){
				MIN=mp[s][j];
				f=j;
			}
		}
		if(f==-1) break;
		vis[f]=1;

		for(int j=0;j<n;j++){
			if(!vis[j]&&mp[s][j]>mp[s][f]+mp[f][j]){
				mp[s][j]=mp[s][f]+mp[f][j];
				pre[j]=f;
				pace[j]=pace[f];
				sum[j]=sum[f]+num[j];
			}
			else if(!vis[j]&&mp[s][j]==mp[s][f]+mp[f][j]){
				pace[j]+=pace[f];
				if(sum[j]<sum[f]+num[j]){
					pre[j]=f;
					sum[j]=sum[f]+num[j];
				}
			}
		}
	}
}
int main(){
	int city1,city2;
	cin>>n>>m>>s>>d;
	memset(vis,0,sizeof vis);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(i!=j) mp[i][j]=mp[j][i]=Inf;
	for(int i=0;i<n;i++){
		cin>>num[i];
		sum[i]=num[i];
		pace[i]=1;
	}
	for(int i=0;i<m;i++){
		cin>>city1>>city2;
		cin>>mp[city1][city2];
		mp[city2][city1]=mp[city1][city2];
	}
	Dijkstra();
	cout<<pace[d]<<" "<<sum[d]+num[s]<<endl;


	int road[1001];
	int cur=d,cnt=0;
	while(pre[cur]!=0){
		road[cnt++]=pre[cur];
		cur=pre[cur];
	}
	//输出
	cout<<s;
	for(int i=cnt-1;i>=0;i--)
		cout<<" "<<road[i];
	cout<<" "<<d;
	return 0;
}

Published 200 original articles · won praise 99 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_43889841/article/details/104070822