Group programming Ladder-practice set l2-1. Emergency rescue Dijstra single Source Shortest path expansion application

Source: Internet
Author: User

L2-1. Emergency response time limit of MS
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Chen

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 320 30 40 100 1 11 3 20 3 30 2 22 3 2
Sample output:
2 600 1 3



In fact, the Dijstra on the basis of a few more arrays, and then update d when the attention of the update of a few other arrays on the line.



#include <cstdio> #include <cstring> #include <iostream> #include <cstdlib>using namespace std; const int MAXN = ten +, INF = 0x3f3f3f3f;int N, m, S, D;//val for the number of people, the shortest distance from D to S, M save graph, cntp current point Shortest path number//num save to current point the maximum number of people to call, path to save As Front-point precursor for final output path int VAL[MAXN], D[MAXN], M[MAXN][MAXN], CNTP[MAXN], NUM[MAXN], path[maxn];bool vis[maxn];void dijstra (int s {//Initialize for (int i = 0; i < N; i++) {d[i] = INF;} for (int i = 0; i < N; i++) {path[i] = i;} memset (cntp, 0, sizeof (CNTP)), memset (num, 0, sizeof (num)), memset (Vis, false, sizeof (VIS));d [s] = 0;cntp[s] = 1;num[s] = VA l[s];for (int i = 0; i < N; i++) {if (i! = s) {D[i] = m[s][i];if (m[s][i]! = INF) {Cntp[i] = 1;num[i] = Val[i] + num[s] ;p Ath[i] = s;}}} Vis[s] = true;//initialization completed for (int t = 0; t < N-1; t++) {int minn = INF, index = s;for (int i = 0; i < N; i++) {if (!vi S[I] && minn > D[i]) {//Note!vis[i]minn = D[i];index = i;}} Vis[index] = true;for (int i = 0; i < N; i++) {if (!vis[i] && m[index][i] < INF){//debug Add!vis[i]//do not judge Vis[i] go to heavy words will make the next sentence D[index] + m[index][i] = = D[i] that is//d[index] + 0 = = D[i] (i = = index) is true, repeat if (d[  Index] + m[index][i] = = D[i]) {Cntp[i] + Cntp[index];//debug is not a simple cntp[i]++,index may even follow multiple paths if (Num[index] + val[i) > Num[i]) {num[i] = Val[i] + num[index];p ath[i] = index;}} Here does not judge!vis[i] does not matter, because even if the index = = i, D[index] < d[i] must not be established else if (D[index] + m[index][i] < D[i]) {Cntp[i] = Cntp[ind EX]; Debug Recalculate Shortest Path number d[i] = D[index] + m[index][i];num[i] = Val[i] + num[index];p ath[i] = index;}}}} int main () {scanf ("%d%d%d%d", &n, &m, &s, &d); for (int i = 0; i < N; i++) {scanf ("%d", &val[i]);} for (int i = 0, i < n; i++) {for (int j = 0; J < N; J + +) {M[i][j] = (i = = J 0:inf);}} for (int i = 0; i < M; i++) {int u, V, w;scanf ("%d%d%d", &u, &v, &w); M[u][v] = m[v][u] = w;} Dijstra (S);p rintf ("%d%d\n", Cntp[d], num[d]); int PR[MAXN], I, Len; Use PR equivalent to the function of a stack, according to the path output the final result i = D; Len = 0;while (i! = S) {pr[len++] = I;i = Path[i];}pr[len++] = i;for (int j = len-1; J >= 0; j--) {printf (j = = len-1?) "%d": "%d", Pr[j]);} Puts (""); return 0;}




Group programming Ladder-practice set l2-1. Emergency rescue Dijstra single Source Shortest path expansion application

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.