Sdutoj 2498 AoE The shortest path on the network SPFA

Source: Internet
Author: User

Description

     a acyclic directed graph is called a acyclic graph (Directed acyclicgraph), or DAG graph.
    AOE (activity on edge) network: As the name implies, use the edge to represent the active network, of course, it is also a DAG. Unlike AOV , activities are represented on the edge, as shown in:

    as shown above, there are a total of one activity (one edge),9 events (9 vertices). The whole project has only one start point and one completion point. That is, there is only one point with zero degrees (source) and only one point with zero out (meeting point).
    Critical Path: Is the length of the longest path from the start point to the completion point. The length of the path is the time spent on the edge. As shown,1 to 2 to 5 to 7 to 9 is the critical path (more than one critical path, please output the smallest dictionary order), the weight of the value of the.

Input

There are several sets of data, guaranteed to be no more than ten groups, to ensure that there is only one source point and meeting point. Enter a top point N (2<=n<=10000), number of sides m (1<=m <=50000), next m line, input start SV, end EV, the weight value w(1<=sv,ev<=n,sv! = ev,1<=w <=20). Data Assurance diagram connectivity.

Output

The weight of the critical path and the path on the critical path from the source point (if there are more than one, output the smallest dictionary order).

Sample Input

9 111 2 61 3 41 4 52 5 13 5 14 6 25 7 95 8 76 8 48 9 47 9 2

Sample Output

181 22 55) 77 9

Hint

is to ask for the longest road to impose a restrictive condition
Started with the Disktra changed the template found the wrong, the array can not be opened will not move
#include <stdio.h> #define Maxnum 1000#define maxint 999999int Dist[maxnum];   Represents the shortest path length of the current point to the source point int prev[maxnum];  Record the previous node of the current point int c[maxnum][maxnum];            The path length between two points of the record graph int n,line; The number of nodes and paths of the graph void Dij (int n,int v,int *dist, int *prev, int c[maxnum][maxnum])//v represents the source point?    {int S[maxnum] = {0};//Determines whether the point has been deposited into the S array of int i; for (i = n;i>=1;i--) {Dist[i] = c[v][i];//The initial distance from the source point to the I point is stored in the array dist s[i] = 0;//is initialized to all without using that point if (dist        [i] = = 0)//If the distance from the source point to the point is infinity, then there is no connection point in front of the point prev[i] = 0;    else prev[i] = v;    } Dist[v] = 0;    The distance from the source point to the source point is initialized to 0 s[v] = 1;//The source point into the array S//will not be placed in the collection S node, take dist "" minimum value of the node in//Once s contains all the V vertices, Dist records the shortest path from the source point to all other vertices       Note that the first node represents the source point for (i = 2;i<=n;i++) {int tmp = 0, starting from the second one;       int u = v;               for (int j = n;j>=1;j--)//Find Dist "J" Minimum Value {if ((!s[j]) && dist[j] > tmp) for the currently unused point J {               U = j; U stores the number of the point with the smallest distance in the current adjacency point tmp = Dist[j];           }} S[u] = 1; Once the For loop has been completed, the smallest point in all unused points is found and the point is stored in the set S//Update dist value for (int j = n;j>=1;j--) {if ((!s[j)) &am p;& c[u][j]>0)//determines that the point does not join the S collection, and that there is a distance, that is, after that point {int newdist = Dist[u] + c[u][j];//to determine the distance through that point to J                   And the size of the distance without passing that point to J if (Newdist > Dist[j]) {dist[j] = newdist;               PREV[J] = u;        }}}}}//finds the path from the source point V to the end of U, and outputs void Searc (int *prev,int v,int u) {int que[maxnum];    The array saves the path int tot = 1;       Que[tot] = u;    Starting from the end, find the source Point V and record the path tot++;    int tmp = Prev[u];        while (tmp! = v) {Que[tot] = tmp;        tot++;    TMP = prev[tmp];    } Que[tot] = V;        for (int i = tot;i>=1;i--)//output {if (i!=1) printf ("%d->", Que[i]);    else printf ("%d\n", Que[i]);    }//Note that if the topic requires output distance, then the direct output dist "U" can}int main () {scanf ("%d%d", &n,&line);  int P,q,len;  for (int i = 1;i<=n;i++) {for (int j = 1;j<=n;j++) c[i][j] = 0;    } for (int i = 1;i<=line;i++) {Dist[i] = 0;        } for (int i = 1;i<=line;i++) {scanf ("%d%d%d", &p,&q,&len);            if (Len>c[p][q]) {c[p][q] = len;        C[q][p] = len;    }} dij (N,1,dist,prev,c);    printf ("Shortest path length from source to endpoint is%d\n", dist[n]);    Searc (Prev,1,n); return 0;}
Here's the SPFA method.
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include < Queue> #define INF 0x3f3f3f3fusing namespace std;struct node{int u,v,w,next;} edge[500010];int cnt,n,m,head[10010],d    IS[10010],VIS[10010],PRE[10010], pre1[10005];int id[10005],cd[10005];void Add (int u,int v,int len) {edge[cnt].u=u;    Edge[cnt].v=v;    Edge[cnt].w=len;    Edge[cnt].next=head[u]; head[u]=cnt++;}    void SPFA (int u,int v,int n) {memset (vis,0,sizeof (VIS));    memset (pre,inf,sizeof (pre));    memset (dis,-inf,sizeof (dis));    queue<int>q;    Q.push (U);    dis[u]=0;    Vis[u]=1;        while (!q.empty ()) {int X=q.front ();        Q.pop ();        vis[x]=0; for (int p=head[x]; p!=-1; p=edge[p].next) {if (dis[edge[p].v]<dis[x]+edge[p].w| |            (DIS[EDGE[P].V]==DIS[X]+EDGE[P].W&AMP;&AMP;X&LT;PRE[EDGE[P].V]))                {DIS[EDGE[P].V]=DIS[X]+EDGE[P].W;                Pre[edge[p].v]=x; if (!VIS[EDGE[P].V])                {vis[edge[p].v]=1;                Q.push (EDGE[P].V);    }}}} int k=0;    printf ("%d\n", Dis[v]);    for (int i=v; i!=inf; i=pre[i]) pre1[k++]=i; for (int i=1; i<k; i++) printf ("%d%d\n", Pre1[i-1],pre1[i]);}        int main () {while (~SCANF ("%d%d", &n,&m)) {int u,v,w;        memset (head,-1,sizeof (head));        Memset (id,0,sizeof (id));        memset (cd,0,sizeof (CD));            while (m--) {scanf ("%d%d%d", &u,&v,&w);            Add (V,U,W);            id[u]++;        cd[v]++;            } for (int i=1; i<=n; i++) {if (id[i]==0) u=i;        if (cd[i]==0) v=i;    } SPFA (U,v,n); } return 0;}



Sdutoj 2498 AoE The shortest path on the network SPFA

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.