Pat L2-001. Emergency relief Dijkstra deformation + record path

Source: Internet
Author: User

As the head of a city's emergency rescue team, you have a special national map. The map shows a number of scattered cities and some quick roads connecting the city. The number of rescue teams in each city and the length of each fast road connecting two cities are marked on the map. When other cities have an emergency call for you, your task is to lead your rescue team to the scene as quickly as possible, while gathering as many rescue teams along the way.

Input Format:

Enter 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 assuming the city number is 0~ (N-1), M is the number of fast roads, S is the city number of the origin, and D is the city number of the destination. The second line gives n positive integers, where number i is the number of rescue teams in City I, separated by a space between the numbers. In the subsequent M-line, each line gives a quick path of information, namely: City 1, City 2, the length of the fast road, the middle with a space separated, the numbers are integers and not more than 500. The input guarantees that rescue is feasible and the optimal solution is unique.

output Format:

The first line outputs the number of different shortest paths and the maximum number of rescue teams that can be convened. The second line outputs the city number that passes through the path from S to D. The numbers are separated by a space, and the output cannot have extra spaces. Input Sample:

4 5 0 3 (+) 0 1 1 1 3 2 0
3 3
0 2 2
2 3 2
Sample output:
2
0 1 3
	Feel this is a particularly good problem, the shortest-circuiting deformation has two weights one is the weight of the point is the weight of the edge. and asked us to record a lot of things,
Note: The path of the output required in the title is guaranteed to be the same as the one that can muster the largest number of rescue teams.
      For the record path: We open a path[] array records What is the previous point of the current point I;
      Array pathnum[] records the number of shortest paths
      Tol[] represents the maximum number of rescue teams that can be called from the starting point to the end point.
      W[] Array is the number of rescue teams per point
The rest is the Dijkstra template, but note that if the current path can be relaxed by this time, we need to update the rest of it to relax.
Path,pathnum,tol,dis Otherwise, if the distance is equal, we have to judge whether his second weight is tol[] (the weight of the point) need to be updated,
In other words, the shortest path is the prerequisite for finding the largest number of rescue teams to muster.
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #define INF
0x3f3f3f3f using namespace std;
const int n=555;
int dis[n],path[n],w[n],pathnum[n],tol[n],book[n];
int mp[n][n];
int n,m,s,d;
		void print (int x)//recursive output path {if (path[x]==-1) {printf ("%d", x);
	return;
	} print (Path[x]);
	printf ("%d", x);
return;
	} void Dijkstra () {memset (book,0,sizeof (book));
	memset (tol,0,sizeof (tol));
	int i,j;
	for (i=0;i<n;i++) Dis[i]=inf;
	dis[s]=0;
	Path[s]=-1;
		tol[s]=w[s];//initialization of the starting point pathnum[s]=1;//the number of shortest paths is 1 for (i=0;i<n;i++) {int u,minn=inf;
				for (j=0;j<n;j++) {if (!book[j]&&dis[j]<minn) {u=j;
			MINN=DIS[J];
		}} book[u]=1;
				for (j=0;j<n;j++) {if (Dis[j]>dis[u]+mp[u][j])//relaxation when each array is updated {dis[j]=dis[u]+mp[u][j];
				Path[j]=u;
				TOL[J]=TOL[U]+W[J];	
			Pathnum[j]=pathnum[u]; } else if (Dis[j]==dis[u]+mp[u][j])//equals update {pathnum[j]+=pathnum[u];//should be both the shortest path and the IF (tol[j]<tOL[U]+W[J])//update of the point weight value {tol[j]=tol[u]+w[j];
				Path[j]=u;
	}}}}} int main () {int x, y, Z;
	scanf ("%d%d%d%d", &n,&m,&s,&d);
	for (int i=0;i<n;i++) scanf ("%d", &w[i]);
	Memset (Mp,inf,sizeof (MP));
		for (int i=0;i<m;i++) {scanf ("%d%d%d", &x,&y,&z);
		Mp[x][y]=z;
	Mp[y][x]=mp[x][y];
	} Dijkstra ();
	printf ("%d%d\n", pathnum[d],tol[d]); 
	Print (d);
	
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.