PAT l2-001 Emergency Rescue

Source: Internet
Author: User
Tags printf
l2-001. Emergency rescue

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

Problem Solving Ideas:

The code is based on the Dijkstra algorithm to find the shortest, but the minimum time to go to the basis of taking as many rescue teams as possible. So in the shortest way is not the only case to determine which road can summon as many rescue teams.


The code is as follows:


#include <stdio.h> #include <string.h> #define INF 0x3f3f3f3f #define N 520 int mp[n][n], dis[n], MAXD[N];//MP number Group Storage graph Information dis array find shortest path Maxd array max rescue Team int path[n], pathnum[n], vis[n], Book[n];//path array marker current point precursor node Pathnum array record path number int N, M, St, en       ;    Vis stores the number of rescue teams around the book tag through the node void print (int x) {if (path[x] = =-1) return;
    Recursive output path print (path[x]);

printf ("%d", path[x]);} void Dijkstra (int v0) {for (int i = 0; i < n; i++) {Dis[i] = mp[v0][i];//Initializes the starting point to the point distance book[i   ] = 0;  Initialization of the points did not pass path[i] =-1;
    Each point precursor node is-1} pathnum[v0] = 1;
    Book[v0] = 1;
    Maxd[v0] = Vis[v0]; for (int i = 0; i < n; i++) {if (Mp[v0][i]! = INF && i! = v0) {Path[i] = V0
            ;
            Maxd[i] = Vis[i] + vis[v0];
        Pathnum[i] = 1;
        }} for (int v = 1; v < n; v++) {int u = v0, Minn = INF;
          for (int i = 0; i < n; i++) {  if (!book[i] && dis[i] < minn) {minn = dis[i];
            U = i;
        }} Book[u] = 1; for (int i = 0; i < n; i++) {if (!book[i]) {if (Dis[i] > Dis[u]
                    + Mp[u][i]) {dis[i] = Dis[u] + mp[u][i];
                    Maxd[i] = Maxd[u] + vis[i];
                    Pathnum[i] = Pathnum[u];
                Path[i] = u;
                    } else if (dis[i] = = Dis[u] + mp[u][i]) {Pathnum[i] + = Pathnum[u]; if (Maxd[i] < Maxd[u] + vis[i]) {maxd[i] = Maxd[u] + V
                        Is[i];
                    Path[i] = u; }}}}}} int main () {while (~scanf ("%d%d%d%d", &n,&m,&st,&amp
        EN)) {for (int i = 0; i < n; i++)    scanf ("%d", &vis[i]); for (int i = 0, i < n; i++) {for (int j = 0; J < N; j + +) {//initialization
                The weights between the points in the figure if (i = = j) Mp[i][j] = 0;
            else mp[i][j] = INF;
        }} int T1, t2, T3;
            for (int i = 0; i < m; i++) {scanf ("%d%d%d", &AMP;T1,&AMP;T2,&AMP;T3);
        if (Mp[t1][t2] > T3)//update minimum weight mp[t1][t2] = mp[t2][t1] = t3;
        } Dijkstra (ST);
        printf ("%d%d\n", Pathnum[en], Maxd[en]);
        Print (EN);
    printf ("%d\n", en);
} 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.