PAT l2-1 Emergency Rescue (SPFA + multiple Shortest path)

Source: Internet
Author: User

Main idea: 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 assume the city number is 0~ (N-1), M is the number of fast roads, S is the city number of the origin, 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 row gives information on a fast path, namely: City 1, City 2, the length of the fast Road


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.


Idea: How to judge the most short-circuit problem, just start to think is in the if (dis[v]== dis[u]+q[i].w) and V==d when the shortest number of points + +, but so want to not guarantee that this is the quickest. We can set a state array state[][] to store the shortest number of bars per point, and then state[][0] to update the number of path bars after the shortest circuit, 1 is the number of bars that have not been updated.


#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <map> #

Include<algorithm> #define INF 0x3f3f3f3f #define LL __int64 #include <queue> using namespace std;
int n,m,s,d,arr[510],dis[510],num[510],pre[510];
int head[510*510],cnt,state[510*5][2],ans[510]; struct node{int w,to,next;}
Q[505*500];
BOOL vis[510];
    void Add (int a,int b,int c) {q[cnt].to = b;
    Q[CNT].W = C;
    Q[cnt].next = Head[a];
Head[a] = cnt++;
    } void Spfa () {memset (vis,false,sizeof (VIS));
    memset (pre,-1,sizeof (pre));

    memset (dis,inf,sizeof (dis));
    Dis[s] = 0;
    Vis[s] = true;
        for (int i = 0;i < n;++ i) {num[i] = Arr[i];
    State[i][0] = state[i][1] = 0;
    } queue<int>q; while (!
    Q.empty ()) Q.pop ();
    Q.push (s);
   State[s][0] = 1;
    Pre[s] =-1; while (!
        Q.empty ()) {int u = q.front ();
        Q.pop (); vis[u] = false; for (int i = head[u];~i;i=q[i].next){int v = q[i].to;
                if (Dis[v] > Dis[u] + q[i].w) {Pre[v] = u;
                DIS[V] = Dis[u] +Q[I].W;
                STATE[V][1] = state[v][0];
                State[v][0] = state[u][0];
                NUM[V] = Num[u] + arr[v];
                    if (!vis[v]) {Vis[v] = true;
                Q.push (v);
                }} else if (dis[v] = = Dis[u] + q[i].w) {state[v][1] = state[v][0];
                State[v][0] + = (state[u][0]-state[u][1]);
                    if (Num[v] < num[u]+ arr[v]) {Num[v] = Num[u] + arr[v];

                PRE[V] = u;
                        } if (!vis[v]) {Vis[v] = true;
                Q.push (v);
    }}}}} int main () {int j,k,i;
        while (~SCANF ("%d%d%d%d", &n,&m,&s,&d)) {memset (head,-1,sizeof (head));
      CNT = 0;  for (i = 0;i < n;++ i) {scanf ("%d", &arr[i]);
        } int a,b,c;
            for (i = 0;i < m;++ i) {scanf ("%d%d%d", &a,&b,&c);
            ADD (A,B,C);
        ADD (B,A,C);
        } SPFA ();
        printf ("%d%d\n", state[d][0],num[d]);
        int s = 0;
        for (I=d;~i;i=pre[i]) {ans[s++]=i; } for (i = s-1;i >= 0;i--) {printf (i==s-1? ")
        %d ":"%d ", ans[i]);
    } puts ("");
} 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.