PTA between city emergency (Dijkstra)

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

Than tourism planning that road more complex topics, that question need only count the length of the shortest path and minimum cost, and the need to record the path of this question, but also the need to record the number of shortest paths of equal length.
At first I misread the question, that the number of passes required output point of ...

Use path [i] denotes the i pre path, pathcnt [i] denotes the i from s number of shortest path, res [i] represents the number convened from s i to the rescue team.
Reference: Data Structures and Algorithms title set (Chinese) - between 7-35 city emergency (25 points)

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<list>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 550
#define INF1 0x3f3f3f3f
#define INF2 0xc0c0c0c0
int n,m,s,d;

int dis[N],res[N],pathcnt[N],path[N],vis[N],edge[N][N],num[N];

void dijkstra(){
	
	for(int i=0;i<n;i++) dis[i] = edge[s][i];
	int e = s;path[s] = -1;dis[e] = 0;pathcnt[e]=1;
	while(e!=d){ 
		vis[e] = 1;
		for(int j=0;j<n;j++){
			if(!vis[j]){
				if(dis[j]>dis[e]+edge[e][j]){
					dis[j]=dis[e]+edge[e][j];
					res[j] = num[j] + res[e];
					pathcnt[j] = pathcnt[e];
					path[j] = e;
				}
				else if(dis[j]==dis[e]+edge[e][j]){
					pathcnt[j] += pathcnt[e];
					if(res[j]<num[j] + res[e]){
						res[j] = num[j] + res[e];
						path[j] = e;
					}
					
				}
			}	
		}
		int mindis = INF1;e = n;
		for(int j=0;j<n;j++){
			if(!vis[j]&&dis[j]<mindis){
				mindis = dis[j];
				e = j;
			}
		}
		if(e==n) break;
	}
	
}
int main() {
	cin >> n >> m >> s >> d;
	for(int i=0;i<n;i++) {
		scanf("%d",num+i); res[i] = num[i];
	}
	int x,y,z,h;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			edge[i][j]=edge[i][j]=INF1;
		}
		edge[i][i] = 0;
	}
	for(int i=0;i<m;i++){
		scanf("%d%d%d",&x,&y,&z);
		edge[x][y]=edge[y][x]= z;
	}
	dijkstra();
	vector<int> p;
	h = d;
	while(h!=-1){
		p.push_back(h);
		h = path[h];
	}
	cout << pathcnt[d] << " " << res[d] << endl; 
	for(int i=p.size()-1;i>=0;i--){
		if(i==p.size()-1) cout << p[i];
		else cout << " " << p[i];
	}
	return 0;
}
Published 79 original articles · won praise 37 · views 8878

Guess you like

Origin blog.csdn.net/SinclairWang/article/details/104156922