HDU 1874unblocked Works continuedTime
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d & %i64u
Description
A province has been building a lot of roads since the implementation of many years of smooth engineering projects. But the road is not good, every time from one town to another town, there are many ways to choose, and some programmes are more than others to walk the distance is much shorter. This makes pedestrians very troubled.
Now that you know the starting and ending points, you can figure out how much distance you need to walk from the start to the end.
Input
This topic contains multiple sets of data, please handle to the end of the file.
The first row of each group of data contains two positive integers N and M (0<n<200,0<m<1000), representing the number of existing towns and the number of roads that have been built. The towns were numbered 0~n-1.
Next is the M-Line road information. Each line has three integers a,b,x (0<=a,b<n,a!=b,0<x<10000), indicating that there is a two-way road with X length between town A and town B.
The next line has two integer s,t (0<=s,t<n), representing the starting and ending points, respectively.
Output
For each set of data, output the shortest distance to walk in a row. If there is no route from S to T, then output-1.
Sample Input
3 30 1 10 2 31 2 10 23 10 1 11 2
Sample Output
2-1
/* /dijkstra[abbreviation DIJ] algorithm is similar to BFS, or can be said, BFS is a special dij. The core of DIJ is only three lines. The priority queue is then used. The following also describes how to use the pair object; 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 401/*/********************************************** #define MEMCPY (x, y) memcpy (x,y,sizeof) /*///typedef pair< T, int> PII; Pair<int,int>/*/*************************************************************pair default to First ascending, When the first phase is ascending to second; class templates: Template <class T1, class t2> struct pair parameter: T1 is the data type of the number one, and T2 is the data type of the second value. Function: Pair combines a pair of values into one value, which can have different data types (T1 and T2) two values can be accessed using the two public functions first and second of the pair respectively. /*/const int dij_v=1e3;const int dij_edge=1e4; Template<class t>//template class. struct Dijkstra {typedef pair< T, int> PII; pair<int,int>struct Edge {int v,nxt; T W;} E[dij_edge<<1];int head[dij_v],erear; T dis[dij_v],inf;void Edge_init () {memset (HEAD,-1); erear=0;} 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 (dis,0x3f); Inf=dis[0];p riority_queue<pii, Vector<pii>, greater<pii> >Q; Priority Queue while (! Q.empty ()) Q.pop (); Q.push (PII (0,u));d is[u]=0;while (! Q.empty ()) {PII a=q.top (); Q.pop (); int a=a.second;if (a.first!=dis[a]) continue; If it is not on a minimum spanning tree, skip. for (int i=head[a]; ~i; i=e[i].nxt) {int v=e[i].v; T W=e[i].w;/*/********************dijkstra Algorithm Core *************************/*/if (W+dis[a]<dis[v]) {// Determine whether it is the smallest way to reach pity Dorado Dis[v]=dis[a]+w; If not, overwrite pity Dorado Q.push (PII (dis[v],v)); Log into the queue. }/*/*************************************************************/*/}}};D ijkstra<int > Dij;int main () {int n , M;while (~scanf ("%d%d", &n,&m)) {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);} int st,ed;scanf ("%d%d", &st,&ed);d Ij.run (ST), if (Dij.dis[ed]==dij. INF) puts ("1"); else PRintf ("%d\n", Dij.dis[ed]);} return 0;}
Continuous-dijkstra algorithm for ACM:HDU 1874 unblocked project