HDU 2544
Find the shortest 1 point n
Sample Input
2 1//Node number edge
1 2 3//u v W
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2
1# include <iostream>2# include <cstdio>3# include <cstring>4# include <algorithm>5# include <cmath>6# define LLLong Long7 using namespacestd;8 9 Const intmaxn= $;Ten Const intinf=0x3f3f3f3f; One intN; A BOOLVIS[MAXN]; - intCOST[MAXN][MAXN]; - intLOWCOST[MAXN]; the intPRE[MAXN]; - voidDijkstra (intBeg) - { - for(intI=0; i<n;i++) + { -lowcost[i]=inf;vis[i]=false;p re[i]=-1; + } Alowcost[beg]=0; at for(intj=0; j<n;j++) - { - intk=-1; - intmin=INF; - for(intI=0; i<n;i++) - if(!vis[i]&&lowcost[i]<Min) in { -min=Lowcost[i]; tok=i; + } - if(k==-1) Break; thevis[k]=true; * for(intI=0; i<n;i++) $ if(!vis[i]&&lowcost[k]+cost[k][i]<Lowcost[i])Panax Notoginseng { -lowcost[i]=lowcost[k]+Cost[k][i]; thepre[i]=K; + } A } the } + - intMain () $ { $ //freopen ("In.txt", "R", stdin); - intm; - while(SCANF ("%d%d", &n, &m)! =EOF) the { - if(n==0&& m==0)Wuyi Break ; the intu, V, W; - intI, J; Wu for(i =0; I < n; i++) - for(j =0; J < N; J + +) AboutCOST[I][J] =INF; $ while(m--) - { -scanf"%d%d%d", &u, &v, &W); - if(W < Cost[u][v])//prevent heavy edges A { +COST[U][V] =W; theCost[v][u] =W; - } $ } theDijkstra (0) ; theprintf"%d\n", lowcost[n-1]) ; the } the - return 0 ; in}
View Code
HDU 1874
The shortest distance to walk is the output. If there is no route from S to T, then output-1.
Sample Input
3 3//Node number edge
0 1 1//u v W
0 2 3
1 2 1
0 2//Start point
3 1
0 1 1
1 2
Sample Output
2
-1
1# include <iostream>2# include <cstdio>3# include <cstring>4# include <algorithm>5# include <cmath>6# define LLLong Long7 using namespacestd;8 9 Const intmaxn= -;Ten Const intinf=0x3f3f3f3f; One intN; A BOOLVIS[MAXN]; - intCOST[MAXN][MAXN]; - intLOWCOST[MAXN]; the intPRE[MAXN]; - voidDijkstra (intBeg) - { - for(intI=0; i<n;i++) + { -lowcost[i]=inf;vis[i]=false;p re[i]=-1; + } Alowcost[beg]=0; at for(intj=0; j<n;j++) - { - intk=-1; - intmin=INF; - for(intI=0; i<n;i++) - if(!vis[i]&&lowcost[i]<Min) in { -min=Lowcost[i]; tok=i; + } - if(k==-1) the Break ; *vis[k]=true; $ for(intI=0; i<n;i++)Panax Notoginseng if(!vis[i]&&lowcost[k]+cost[k][i]<Lowcost[i]) - { thelowcost[i]=lowcost[k]+Cost[k][i]; +pre[i]=K; A } the } + - } $ $ intMain () - { - //freopen ("In.txt", "R", stdin); the intm; - while(SCANF ("%d%d", &n, &m)! =EOF)Wuyi { the - intu, V, W; Wu intI, J; - for(i =0; I < n; i++) About for(j =0; J < N; J + +) $COST[I][J] =INF; - while(m--) - { -scanf"%d%d%d", &u, &v, &W); A if(W < Cost[u][v])//prevent heavy edges + { theCOST[U][V] =W; -Cost[v][u] =W; $ } the } thescanf"%d%d", &u, &v); the Dijkstra (u); the if(Lowcost[v]! =INF) -printf"%d\n", Lowcost[v]); in Else theprintf"-1\n") ; the } About the return 0 ; the}
View Code
Hdu 2544 hdu 1874 Dijkstra Template questions