HDU 2544 Shortest Path (short circuit entry)

Source: Internet
Author: User

Link: click here

Test instructions

In the annual school game, all the students who enter the finals will get a beautiful T-shirt. But every time our staff put demonstrating clothes from the store back to the game, it was very tiring! So now they want to find the shortest route from the store to the arena, can you help them?

InputThe input includes multiple sets of data. The first row of each group of data is two integers n, m (n<=100,m<=10000), n indicates that there are several intersections on the streets of Chengdu, the intersection labeled 1 is the location of the store, the intersection labeled N is the location of the stadium, M said there are several roads in Chengdu. N=m=0 indicates the end of the input. The next m line, each line consists of 3 integers a,b,c (1<=a,b&lt;=n,1<=c<=1000), indicating that there is a road between junction A and intersection B, and our staff needs C minutes to walk this road.
Enter a guarantee that there are at least 1 shops to the track.
  OutputFor each set of inputs, the output line indicates the shortest time the worker has walked from the store to the arena
Ideas:Very bare very bare shortest problem, noteconsider repeating the path, that is, the input two points u,v the same, then stored if the smallest of the Quan (weights).
Reference code:
#include <string.h> #include <stdio.h> #include <iostream> #include <algorithm>using namespace  std;const int Inf=0x3f3f3f3f;const int maxn=10100;int COST[MAXN][MAXN];           Save diagram int D[MAXN];       The shortest path starting from S bool USED[MAXN];                 An already used figure int V;    Vertex number int city[maxn];void Dijkstra (int s) {for (int i=0; i<v; i++) d[i]=cost[s][i];    memset (used,false,sizeof (used));    d[s]=0;    Used[s]=true;        while (true) {int v=-1; for (int u=0; u<v; u++) {if (!used[u]&& (v==-1| |        D[U]&LT;D[V]) V=u;        } if (v==-1) break;        Used[v]=true;        for (int u=0; u<v; u++) {d[u]=min (d[u],d[v]+cost[v][u]); }}//return d[v];}    int main () {int m,i,j;        while (~SCANF ("%d%d", &v,&m), v+m) {memset (cost,inf,sizeof (cost));        int U,v,quan;            for (i=0; i<m; i++) {scanf ("%d%d%d", &u,&v,&quan);if (Cost[u][v]>quan) Cost[u][v]=cost[v][u]=quan;        } Dijkstra (V);    printf ("%d\n", d[1]);   } return 0;}

There are three other ways to read from other blogs, which are comprehensive and are for informational purposes only:
/* Link: http://gzhu-101majia.iteye.com/blog/1150170*/#include <string.h> #include <stdio.h> #include < Iostream> #include <algorithm>using namespace std;const int N = 105;const int INF = 99999999;int Map[n][n], dist[n    ];bool visit[n];int N, m;void init ()//initialization {int I, J;            for (i = 1, i < n; i++) {for (j = 1; j < N; J + +) {if (i = = j) Map[i][j] = 0;        else map[i][j] = map[j][i] = INF;    }}}void input ()//input function {int VI, VJ, cost;        while (m--) {scanf ("%d%d%d", &vi, &AMP;VJ, &cost);    if (Cost < MAP[VI][VJ]) MAP[VI][VJ] = Map[vj][vi] = Cost;    }}void Floyd ()//floyd algorithm {int I, j, K;                for (k = 1; k <= N; k++)//k is the middle point for (i = 1; I <= n; i++) for (j = 1; J <= N; j + +) if (Map[i][k] + map[k][j] < Map[i][j]) map[i][j] = Map[i][k] + map[k][j];}    void Dijkstra ()//dijkstra algorithm {int i, J, next, MIN; MeMset (visit, false, sizeof (visit));    for (i = 1; I <= n; i++) dist[i] = INF;    DIST[1] = 0;        for (i = 1; I <= n; i++) {MIN = INF;        for (j = 1; J <= N; j + +) if (!visit[j] && dist[j] <= min) min = dist[next=j];        if (MIN = = INF) break;        Visit[next] = true; for (j = 1; J <= N; j + +) if (!visit[j] && dist[j] > Dist[next] + map[next][j]) dist[    J] = Dist[next] + map[next][j];    }}void SPFA ()//SPFA algorithm {int i, now;    Memset (visit, false, sizeof (visit));    for (i = 1; I <= n; i++) dist[i] = INF;    DIST[1] = 0;    Queue<int> Q;    Q.push (1);    Visit[1] = true; while (!        Q.empty ()) {now = Q.front ();        Q.pop ();        Visit[now] = false; for (i = 1; I <= n; i++) {if (Dist[i] > Dist[now] + map[now][i]) {dist[i                ] = Dist[now] + map[now][i];       if (visit[i] = = 0) {             Q.push (i);                Visit[i] = true;        }}}}}int main () {while (scanf ("%d%d", &n, &m)) {if (!n | |!m) break;        Init ();        Input ();        Floyd ();        Dijkstra ();        SPFA ();        printf ("%d\n", Map[1][n]);    printf ("%d\n", Dist[n]); } return 0;} Test data:/*2 11 2 33 31 2 52 3 53 1 20 0*/



HDU 2544 Shortest Path (short circuit entry)

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.