Shortest circuit (output path)

Source: Internet
Author: User
Tags min
5-5 Inter-City emergency rescue (25 points)

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 nn, MM, SS, DD, where NN (2\le n\le 5002≤n≤500) is the number of cities, by the way assume the city number is 0 ~ (N-1) (n−1), MM is the number of fast roads; SS is the city number of the departure point. DD is the city number of the destination.

The second line gives a positive integer of NN, where the number of Number II is a rescue team in the second city, separated by a space between the numbers. In the subsequent MM line, each line gives a quick road information, respectively: City 1, City 2, the length of the fast road, the middle with a space separate, 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 the shortest path and the maximum number of rescue teams that can be convened. The second line outputs the city number that passes through the path from SS to DD. The numbers are separated by a space, and the output cannot have extra spaces at the end. Input Sample:

4 5 0 3 (+) 0 1 1 1 3 2 0
3 3
0 2 2
2 3 2
Sample output:
2 60

0 1 3

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <
Stack> #define INF 9999999 using namespace std;
int n,m,ss,e;
Stack<int> St;
int map[505][505];
int vis[505],cost[505],dis[505],w[505],a[505],num[505];
	void Dij (int s) {fill (vis,vis+505,0);   Fill (a,a+505,s);   Storage path by fill (w,w+505,0); Cost Fill (dis,dis+505,inf);   Distance Fill (num,num+505,0);
	Number of path bars dis[s]=0;
		for (int i=0;i<n;i++) {int min=inf,u=-1;
				for (int j=0;j<n;j++) {if (vis[j]==0&&dis[j]<min) {u=j;
		}} vis[u]=1; for (int v=0;v<n;v++) {if (Vis[v]==0&&map[u][v]!=inf) {if (Dis[v]>dis[u]+map[u][v]) {di
						} else if (Dis[v]==dis[u]+map[u][v]) {if (W[v]<w[u]+cost[v]) {W[V]=W[U]+COST[V];
					A[v]=u; } Num[v]=num[v]+num[u];
				}}}}} int main () {scanf ("%d%d%d", &n,&m,&ss,&e);
	Fill (Map[0],map[0]+505*505,inf); 	for (int i=0;i<n;i++) scanf ("%d", &cost[i]);
		Cost for (int i=0;i<m;i++) {int x, y, Z;
		scanf ("%d%d%d", &x,&y,&z);
	} DIJ (ss);
	printf ("%d%d\n", num[e],w[e]);
		while (E!=a[e])//save path {St.push (e);
	} st.push (ss);
	int num1=0,sum=st.size ();
		while (!st.empty ())//output path {printf ("%d", ());
		if (num1<sum) printf ("");
	St.pop (); }

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: 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.