• 最短路径


    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int N, M, map[1010][1010], dis[1010], hash[1010], cnt, dp[1010];
    const int inf = 0x7fffffff;
    void Dijsktra () {
         dis[2] = 0;
         for ( int i = 1; i <= N; i++ ) {
              int pos, t = inf;
              for ( int j = 1; j <= N; j++ ) {
                     if ( hash[j] == 0 ) {
                         if ( dis[j] < t ) {
                             t = dis[j]; pos = j;    
                         }    
                     }   
              }   
              hash[pos] = 1;
              for ( int j = 1; j <= N; j++ ) {
                     if ( hash[j] == 0 ) {
                         if ( map[pos][j] != inf && dis[j] > dis[pos] + map[pos][j] ) {
                               dis[j] = dis[pos] + map[pos][j];    
                         }    
                     }   
              }
         }
    }
    int DFS ( int x ) {
         if ( x == 2  ) {
              cnt++; return 1;    
         }        
         if ( dp[x]  ) {
              cnt++; return dp[x];    
         }
        
         for ( int i = 1; i <= N; i++ ) {
              if ( hash[i] == 0  ) {
                  if ( map[x][i] != inf &&dis[i] < dis[x] )  {
                          hash[i] = 1;
                          dp[x] += DFS ( i );
                          hash[i] = 0;   
                  }   
              }   
         }
         return dp[x];
    }
    int main () {
         while ( scanf ( "%d", &N ), N ) {
               scanf ( "%d", &M );
               int x, y, val;
               for ( int i = 1; i <= N; i++ ) {
                    dis[i] = inf ; hash[i] = 0;
                    for ( int j = 1; j <= N; j++ )
                         map[i][j] = inf;  
               } 
               for ( int i = 1; i <= M; i++ ) {
                    scanf ( "%d%d%d", &x, &y, &val );
                    if ( map[x][y] > val )  {
                        map[x][y] = map[y][x] = val;    
                    }
               }
               Dijsktra ();
               cnt = 0;
               memset ( hash, 0, sizeof (hash) );
               memset ( dp, 0, sizeof (dp) );
               DFS (1)
               printf ( "%d\n",  cnt );
         }
         return 0;  
    }
     
    ////////////////////////
     
    #include<stdio.h>
    #include<string.h>
    int map[110][110],dis[110], hash[110], N, M, x, y, v;
    const int inf = 0x7fffffff;
    int iii = 0x7f7f7f7f;
    int Dijkstra () {
        for ( int i = 1; i <= N; ++ i ) {
            dis[i] = inf;
            hash[i] = 0;
        }
        dis[1] = 0;
        for ( int i = 1; i <= N; ++ i ) {
            int t = inf, pos = 0;
            for ( int j = 1; j <= N; ++ j ) {
                if ( !hash[j] ) {
                    if ( dis[j] < t ) {
                         t = dis[j];
                         pos = j;   
                    }    
                }    
            }  
            hash[pos] = 1;
            //if ( pos == N ) break;
            for ( int j = 1; j <= N; ++ j ) {
                if ( !hash[j] && map[pos][j] != inf ) {
                   if ( dis[j] > dis[pos] + map[pos][j] ) {
                        dis[j] = dis[pos] + map[pos][j];   
                   }     
                }    
            }  
        }    
        return dis[N];
    }
    int main( )
    {
        while ( scanf ( "%d%d", &N, &M ) , N || M ) {
              for ( int i= 1; i <= N; ++ i ) {
                  for ( int j = 1; j <= N; ++ j ) map[i][j] = inf;    
              }      
             // memset ( map, 0x7f, sizeof ( map ) );
             for ( int i = 1; i <= M ; ++ i ) {
                 scanf ( "%d%d%d", &x, &y, &v );
                 map[x][y] = map[y][x] = v;    
             }   
             printf ( "%d\n", Dijkstra () );
        }
        return 0;
    }
     
  • 相关阅读:
    C#中enum的总结(转载)
    jQuery之获取select选中的值
    event.preventDefault方法的使用
    C++概述
    C++ 名字空间
    C++初步学习
    C++回调机制实现 signal-slot
    转: 关于UI开发(总结一些技术和一些论坛牛人讨论)
    Duilib 入门教程: 怎么创建一个自定义的窗口
    SQL Server 数据库设计规范
  • 原文地址:https://www.cnblogs.com/QQbai/p/2135278.html
Copyright © 2020-2023  润新知