Address: (http://acm.hdu.edu.cn/showproblem.php?pid=2544)
The topic uses the Dijkstra algorithm, the time complexity of the algorithm is O (v*v), the use of this premise--the graph does not have negative weight edge, otherwise this algorithm will not solve the problem, but also to use the Bellman-ford algorithm (the premise of using this algorithm is that there is no negative ring in the diagram : A loop with a negative total weight from the source point)
The AC code is as follows:
#include <cstdio>#include <cstring>#include <iostream>using namespace STD;Const intmaxn=111;Const intinf=201314;intN,M,COST[MAXN][MAXN],VIS[MAXN],DIS[MAXN];voidInit () {memset(Cost,0,sizeof(cost)); for(intI=1; i<=n; i++) for(intj=1; j<=n; J + +)if(I!=J) Cost[i][j]=inf;}voidDijkstra () {memset(Vis,false,sizeof(VIS)); for(intI=1; i<=n; i++) dis[i]=cost[1][i]; vis[1]=true; for(intI=1; i<=n; i++) {intmins=inf,pos=1; for(intj=1; j<=n; J + +)if(!vis[j]&&dis[j]<mins) mins=dis[pos=j]; vis[pos]=true; for(intk=1; k<=n; k++)if(!vis[k]&&dis[k]>dis[pos]+cost[pos][k]) dis[k]=dis[pos]+cost[pos][k]; }}intMain () { while(Cin>>n>>m&& (m+n)) {init (); for(intI=0; i<m; i++) {intFrom,to,co;Cin>>from>>to>>co;if(Co<cost[from][to]) Cost[from][to]=cost[to][from]=co; } Dijkstra ();cout<<dis[n]<<endl; }return 0;}
hdu2544-Shortest circuit