Direct the examples:
HDU2544
Input includes a plurality of sets of input data. The first line of each two integers N, M (N <= 100 , M <= 10000), N represents a few street intersections Chengdu, reference numeral 1 is to store the location of the intersection, the intersection designated N is the location of the stadium, M indicates a couple of choices in Chengdu. N = M = 0 indicates the end of input. Next M rows, each row comprising three integers A, B, C (1 < = A, B <= N, 1 <= C <= 1000), indicates there is a path between the intersection A and the intersection B, we staff need to C-minute time traveled this road.
Input line to ensure the presence of at least one track to store.
Output For each input and output line, represents the track workers come from a store minimum time Sample Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
Sample Output
3 2
Dijkstra always forget! ! ! !
#include<iostream> #include<algorithm> #include<vector> #include<cstring> using namespace std; const int N=1E4+7; const int INF=1e5+7; vector<int >ve[N]; int arr[N][N]; int dis[N]; int mark[N]; int n,m; void djstrea(int x){ memset(mark,0,sizeof(mark)); //标记数组 for( Int I = . 1 ; I <= n-; I ++ ) { DIS [I] = ARR [X] [I]; } DIS [X] = 0 ; // to its distance 0 Mark [X] = . 1 ; for ( int I = . 1 ; I <= n; I ++) { // n points loop n times int ANS = INF; // always find the minimum point of dis array int POS; for ( int I = . 1 ; I <= n-; I ++ ) { IF (Mark [I] == 0 && ANS> DIS [I]) { ANS=dis[i]; pos=i; } } mark[pos]=1; for(int k=1;k<=n;k++){ if(!mark[k]) dis[k]=min(dis[k],ans+arr[pos][k]); } } } int main(){ while(cin>>n>>m){//n个点,m条边 int x,y,z; if(n==0&&m==0) BREAK ; // initializes the maximum value for ( int I = . 1 ; I <= n-; I ++ ) { for ( int J = . 1 ; J <= n-; J ++ ) ARR [I] [J] = INF; } // patterning assignment for ( int I = . 1 ; I <= m; I ++ ) { CIN >> >> X >> Y Z; ARR [X] [Y] = Z; ARR [Y] [X] = Z; } int XX = 1 ; // find the shortest distance to the point xx djstrea (xx); the printf ( " % D \ n- " , DIS [n-]); } return 0 ; }