Acm:hdu 2544 Shortest Path-dijkstra algorithm

Source: Internet
Author: User

HDU 2544 Shortest circuitTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d & %i64u

Description

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?

Input

The 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<=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.

Output

For each set of inputs, the output line indicates the shortest time the worker has walked from the store to the arena

Sample Input

2 11 2 33 31 2 52 3 53 1 20 0

Sample Output

32

The template problem of Dijkstra algorithm
And I wrote before the smooth works are basically the same as the decision is different.

AC Code:

#include "algorithm" #include "iostream" #include "CString" #include "Cstdlib" #include "string" #include "Cstdio" # Include "vector" #include "cmath" #include "queue" using namespace std;typedef long long LL; #define MEMSET (x, y) memset (x, Y, sizeof (x)) #define MX 401const int dij_v=1005;const int dij_edge=10005 #define MEMCPY (x, y) memcpy (x,y,sizeof); Template <class t>struct Dijkstra {struct Edge {int v,nxt; T W;} E[dij_edge<<1];int head[dij_v],erear; T p[dij_v],inf;typedef pair< t, int > Pii;void edge_init () {erear=0;memset (head,-1);} void Edge_add (int u,int v,t W) {e[erear].v=v; E[erear].w=w; E[erear].nxt=head[u];; head[u]=erear++;} void run (int u) {memset (p,0x3f); inf=p[0];p riority_queue<pii, Vector<pii >,greater<pii > >q;while (! Q.empty ()) {Q.pop ();} Q.push (PII (0,u));p [U]=0;while (! Q.empty ()) {PII a=q.top (); Q.pop (); int u=a.second;if (A.first!=p[u]) continue;for (int i=head[u]; ~i; i=e[i].nxt) {int v=e[i].v; T w=e[i].w;if (P[u] + W <p[v]) {p[v]=w+p[u]; Q.push (PII (P[v],v));}}}};D IjkStra<int > Dij;int Main () {int n,m;while (~scanf ("%d%d", &n,&m)) {if (!n&&!m) Break;dij.edge_init ( ); for (int i=1;i<=m;i++) {int u,v,w;scanf ("%d%d%d", &u,&v,&w);d Ij.edge_add (u,v,w);d Ij.edge_add (v,u,w );} Dij.run (1);p rintf ("%d\n", Dij.p[n]);} return 0;}

  

Acm:hdu 2544 Shortest Path-dijkstra algorithm

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.