hdu2544 (Shortest way)

Source: Internet
Author: User

HDU 2544

Dijkstra

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring>using namespace std; #define MAXN 110#define inf 0x3f3f3f3f3fstruct edge{int from,to,dist;}    Map[11000];int a[maxn][maxn];int d[maxn];int visit[maxn];int n,m;void init () {memset (visit,0,sizeof (visit));          for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {a[i][j]=inf;      if (i==j) a[i][j]=0;    } for (int i=2;i<=maxn;i++) D[i]=inf; d[1]=0;} void Dijkstra () {for (int k=1;k<=n;k++)//loop n times because each time a point closest to the source point is found, n times the nearest point of all the source points is found, can be seen as two sets, and a collection represents the point that has been found closest to the source point,      One is not yet found the point {int m=inf;      int x;                 for (int i=1;i<=n;i++)//Violent enumeration points {if (!visit[i] && d[i]<m) that do not find the minimum distance connected to the source point in the point of the minimum distance {                X=i;         M=d[i];      }} visit[x]=1;      for (int i=1;i<=n;i++)//update {if (A[x][i]!=inf) d[i]=min (D[i],d[x]+a[x][i]); }}//for (int i=1;i<=n;i++)//printf ("%d", d[i]);     cout<<endl; printf ("%d\n", D[n]);}        int main () {while (~scanf ("%d%d", &n,&m) && n!=0 && m!=0) {init ();            for (int i=1;i<=m;i++) {scanf ("%d%d%d", &map[i].from,&map[i].to,&map[i].dist);            A[map[i].from][map[i].to]=map[i].dist;        A[map[i].to][map[i].from]=map[i].dist;    } Dijkstra ();    }//System ("pause"); return 0;}

Dijkstra (Priority queue optimization is optimized to select points closest to the source point each time, bare is a brute force enumeration)

Dijkstra uses a priority queue, although the same point can be queued multiple times (because each node can be refreshed multiple times by other nodes), but the done array guarantees a point when the real pop comes out and refreshes the other points only once

When the first time out of the team to update others, done[i]=true, then judge on it.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue >using namespace std; #define MAXN 110#define inf 0x3f3f3f3f3f3fstruct edge{int from,to,dist;};  Vector <int> G[maxn*4]; Number of stored diagrams vector <Edge> edges;int d[maxn];int VISIT[MAXN];    Whether the tag is in the queue int n,m;priority_queue < int > q;void init () {for (int i=0;i<=n;i++) g[i].clear ();    Edges.clear ();    memset (visit,0,sizeof (visit));    for (int i=1;i<=n;i++) D[i]=inf; d[1]=0;}    void Addedge (int from,int to,int dist) {Edge edge; Edge.from=from; Edge.to=to;    Edge.dist=dist;    Edges.push_back (Edge);    int size=edges.size (); G[from].push_back (SIZE-1);} void Dijkstra ()//due to the minimum value each time the team is out, ensure that each node only enters the team once.    {//Plus done array tag, if there are multiple identical nodes in the queue, only let the first one update others.   Dijkstra uses a priority queue, although the same point can be queued multiple times, but the done array guarantees a point when the real pop comes out and refreshes the other points only once. Pop out on the show is the shortest, so only need to update the other one at a time, and naked is the same.

Q.push (1); while (!q.empty ()) {int x=q.top (); Q.pop (); for (int i=0;i<g[x].size (); i++) {Edge e=edges[g[x][i]]; if (d[e.to]>d[e.from]+e.dist) {d[e.to]=d[e.from]+e.dist; Q.push (e.to); }}}//for (int i=1;i<=n;i++)//printf ("%d", d[i]); cout<<endl; printf ("%d\n", D[n]);} int main () {while (~scanf ("%d%d", &n,&m) && n!=0 && m!=0) {init (); for (int i=1;i<=m;i++) {int from,to,dist; scanf ("%d%d%d", &from,&to,&dist); Addedge (from,to,dist); Addedge (to,from,dist); } Dijkstra (); }//System ("pause"); return 0;}

Bellman_ford (Diagram shortest path http://www.java3z.com/cwbwebhome/article/article1/1359.html?id=4720)

  d[k][map[i].to]=d[k-1][map[i].from]+map[i].dist;

#include <iostream> #include <cstdio> #include <cstring >using namespace std; #define MAXN 110#define INF 0x3f3f3f3f3fstruct edge{int from,to,dist;}        Map[200010];int d[maxn][maxn];int n,m;void Init () {for (Int. k=0;k<maxn;k++) for (int i=1;i<maxn;i++)    D[k][i]=inf; for (int k=0;k<maxn;k++) d[k][1]=0;}        void bellman_foyed () {//n slack operation per edge//cout<< "FSDFSD"; for (int k=1;k<=n;k++) {for (int i=1;i<=m;i++) {if (D[k-1][map[i].fro M]<inf) if (d[k-1][map[i].from]+map[i].dist<d[k][map[i].to]) {d[k                 ][map[i].to]=d[k-1][map[i].from]+map[i].dist; }}}/*for (int k=1;k<=n;k++) {for (int i=1;i<=n;i++) PRI             NTF ("%d", d[k][i]);         printf ("\ n"); }*/printf ("%d\n", D[n][n]);} int main () {while (~scanf ("%d%d", &n,&m) && n!=0 && m!=0) {init ();            for (int i=1;i<=m;i++) {scanf ("%d%d%d", &map[i].from,&map[i].to,&map[i].dist);         cin>>map[i].from>>map[i].to>>map[i].dist;             } for (int i=m+1;i<=2*m;i++) {map[i].to=map[i-m].from;             map[i].from=map[i-m].to;         Map[i].dist=map[i-m].dist;         } m=2*m;    Bellman_foyed ();    }//getchar (); return 0;}

 

Bellman_ford adjacency Table

D[k][map[i].to]=d[k-1][map[i].from]+map[i].dist (omitting K, which can be understood as a node flushed n times by other nodes)

#include <iostream> #include <cstdio> #include <cstring >using namespace std; #define MAXN 110#define INF 0x3f3f3f3f3fstruct edge{int from,to,dist;}        Map[200010];int d[maxn];int n,m;void Init () {for (int i=1;i<maxn;i++) D[i]=inf; d[1]=0;}        void bellman_foyed () {//n slack operation per edge//cout<< "FSDFSD"; for (int k=1;k<=n;k++) {for (int i=1;i<=m;i++) {if (d[map[i].from]&lt ; inf) if (D[map[i].from]+map[i].dist<d[map[i].to]) {d[map[i].to]=d[m                 Ap[i].from]+map[i].dist; }}}/*for (int k=1;k<=n;k++) {for (int i=1;i<=n;i++) PRI             NTF ("%d", d[k][i]);         printf ("\ n"); }*/printf ("%d\n", D[n]);}         int main () {while (~scanf ("%d%d", &n,&m) && n!=0 && m!=0) {init (); for (int i=1;i<=m;i++) {            scanf ("%d%d%d", &map[i].from,&map[i].to,&map[i].dist);         cin>>map[i].from>>map[i].to>>map[i].dist;             } for (int i=m+1;i<=2*m;i++) {map[i].to=map[i-m].from;             map[i].from=map[i-m].to;         Map[i].dist=map[i-m].dist;         } m=2*m;    Bellman_foyed ();    }//getchar (); return 0;}

Bellman_ford adjacency Matrix (one node is flushed n times)

D[k][i] represents the shortest distance from the source point through a K-point, or a K-bar.

D[k][i]=min (D[k][i],d[k-1][j]+a[j][i]);

#include <iostream> #include <cstdio> #include <cstring >using namespace std; #define MAXN 110#define    INF 0x3f3f3f3f3fint A[maxn][maxn];int D[MAXN][MAXN][MAXN];             int n,m;void init () {for (int k=0;k<maxn;k++) for (int. i=0;i<maxn;i++) for (int j=0;j<maxn;j++) {            A[i][j]=inf;            D[k][i][j]=inf;       if (i==j) d[k][i][j]=0; }}void bellman_foyed () {//2*m relaxation operation for each edge for (int k=1;k<=n;k++) {for (int i=1;i<=n;i + +) {for (int j=1;j<=n;j++) {if (I!=j && a[j][i]!=inf                        ) {if (D[k-1][1][j]+a[j][i]<d[k][1][i]) {                      D[k][1][i]=d[k-1][1][j]+a[j][i]; }}}}}/* for (int. k=1;k<=n;k++) {for ( int i=1;i<=n;i++) printf ("%d ", D[k][1][i]);         printf ("\ n"); }*/printf ("%d\n", D[n][1][n]);}         int main () {while (~scanf ("%d%d", &n,&m) && n!=0 && m!=0) {init ();          int from,to,dist;             for (int i=1;i<=m;i++) {scanf ("%d%d%d", &from,&to,&dist);            A[from][to]=dist;             D[from][to]=dist;            A[to][from]=dist;         D[to][from]=dist;    } bellman_foyed (); } return 0;}

Spfa

The visit array checks the updated nodes to ensure that there are no identical nodes in the queue, but one node can be queued multiple times

With SPFA, the same node may be queued multiple times, and then several times to refresh the other nodes, which will result in the shortest possible number of short-circuit bar repeated calculation (so you can judge the negative ring)

When each node only enters the queue once, degenerate into a wide search.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue >using namespace std; #define MAXN 110#define inf 0x3f3f3f3f3f3fstruct edge{int from,to,dist;};  Vector <int> G[maxn*4]; Number of stored diagrams vector <Edge> edges;int d[maxn];int VISIT[MAXN];    Whether the tag is in the queue int n,m;queue < int > q;void init () {for (int i=0;i<=n;i++) g[i].clear ();    Edges.clear ();    memset (visit,0,sizeof (visit));    for (int i=1;i<=n;i++) D[i]=inf; d[1]=0;}    void Addedge (int from,int to,int dist) {Edge edge; Edge.from=from; Edge.to=to;    Edge.dist=dist;    Edges.push_back (Edge);    int size=edges.size (); G[from].push_back (SIZE-1);}   void Spfa () {Q.push (1);   Visit[1]=1;       while (!q.empty ()) {//cout<< "FSDF" <<endl;       int X=q.front ();       Q.pop ();       visit[x]=0;  for (int i=0;i<g[x].size (); i++) {int next_=g[x][i]; if (d[edges[next_].to]>d[edges[next_].from]+eDges[next_].dist && d[edges[next_].from]<inf) {D[edges[next_].to]=d[edges[next_].from]+edge            S[next_].dist;               if (visit[edges[next_].to]==0) {visit[edges[next_].to]=1;            Q.push (edges[next_].to);     }}}}//for (int i=1;i<=n;i++)//printf ("%d", d[i]);    cout<<endl; printf ("%d\n", D[n]);}        int main () {while (~scanf ("%d%d", &n,&m) && n!=0 && m!=0) {init ();           for (int i=1;i<=m;i++) {int from,to,dist;           scanf ("%d%d%d", &from,&to,&dist);          Addedge (from,to,dist);        Addedge (to,from,dist);    } SPFA ();    }//System ("pause"); return 0;}

Floyd

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineMAXN 110#defineINF 0x3f3f3f3f3f3fintn,m;intMAP[MAXN][MAXN];intD[MAXN][MAXN][MAXN];voidinit () { for(intI=0; i<maxn;i++)         for(intj=0; j<maxn;j++) {Map[i][j]=inf; if(i==j) Map[i][j]=0; }}voidFloyd () { for(intI=0; i<maxn;i++)         for(intj=0; j<maxn;j++) d[0][i][j]=Map[i][j];  for(intk=1; k<=n;k++)         for(intI=1; i<=n;i++)          for(intj=1; j<=n;j++) D[k][i][j]=min (d[k-1][i][j],d[k-1][i][k]+d[k-1][k][j]); /*for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) printf ("%d", d[i][j]);         printf ("\ n"); }*/printf ("%d\n", d[n][1][n]);}intMain () { while(SCANF ("%d%d", &n,&m) && n!=0&& m!=0) {init (); int  from, To,dist;  for(intI=1; i<=m;i++) {scanf ("%d%d%d",& from,&to,&Dist); map[ from][to]=Dist; map[to][ from]=Dist;    } Floyd (); }    return 0;}

  

hdu2544 (Shortest way)

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.