• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            gzwzm06

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              1 隨筆 :: 52 文章 :: 17 評論 :: 0 Trackbacks
              1#include <cstdio>
              2#include <cstring>
              3
              4const int MAXN = 10100000 ;
              5const __int64 INT_MAX = 2000000000 ;
              6
              7struct NODE
              8{
              9    int v ;
             10    __int64 w ;
             11    NODE *next ;
             12}
            edge[MAXN], redge[MAXN], g_Temp[MAXN * 2] ;
             13
             14int g_Pos = 0 ;
             15
             16__int64 ecost[MAXN] ;
             17int N , M , W , Q[MAXN];
             18
             19bool visited[MAXN] ;
             20
             21bool SPFA(bool rg = false)
             22{
             23    int  h = 0 , t = 1 , u ;
             24    NODE *ptr ;
             25
             26    memset(visited, 0sizeof(visited)) ;
             27
             28    for ( int i = 0 ; i <= N ; ++i )
             29        ecost[i] = INT_MAX ;
             30    Q[0= 1 ;
             31
             32    ecost[1= 0 ;
             33
             34    while ( h != t )
             35    {
             36        u = Q[h] ;
             37        h++ ;
             38
             39        visited[u] = false ;
             40
             41        if ( !rg )
             42            ptr = edge[u].next ;
             43        else
             44            ptr = redge[u].next ;
             45        
             46        while ( ptr ){
             47            if ( ecost[ptr->v] > ecost[u] + ptr->w )    //松弛操作
             48            {
             49                ecost[ptr->v] = ecost[u] + ptr->w ;    
             50                if ( !visited[ptr->v] )
             51                {
             52                    Q[t] = ptr->v ;
             53                    t++ ;                    
             54                    visited[ptr->v] = true ;                    
             55                }

             56            }

             57            ptr = ptr->next ;
             58        }

             59    }

             60
             61    return ( ecost[N] != INT_MAX ) ;
             62}

             63
             64void Insert( const int& x, const int& y, const __int64& w )
             65{
             66    NODE *ptr = &g_Temp[g_Pos++] ;
             67    ptr->= y ;
             68    ptr->= w ;
             69    ptr->next = edge[x].next ;
             70    edge[x].next = ptr ;
             71
             72    ptr = &g_Temp[g_Pos++] ;
             73    ptr->= x ;
             74    ptr->= w ;
             75    ptr->next = redge[y].next ;
             76    redge[y].next = ptr ;
             77}

             78
             79void Init()
             80{
             81    g_Pos = 0 ;
             82
             83    for ( int i = 0 ; i <= N ; ++i )
             84    {
             85        edge[i].next = NULL ;
             86        redge[i].next = NULL ;
             87    }

             88}

             89
             90int main()
             91{
             92    int t , i , x , y ;
             93    __int64 w ;
             94    __int64 ans ;
             95
             96    scanf("%d"&t) ;
             97
             98    while ( t-- )
             99    {
            100        scanf("%d %d"&N, &M) ;
            101
            102        Init() ;
            103        for ( i = 0 ; i < M ; ++i )
            104        {
            105            scanf("%d %d %I64d"&x, &y, &w) ;
            106            Insert( x, y, w ) ;
            107        }

            108
            109        ans = 0 ;
            110
            111        //照樣對正反圖求最短路
            112        SPFA() ;
            113
            114        for ( i = 2 ; i <= N ; ++i )
            115        {
            116            ans += ecost[i] ;
            117        }

            118
            119        SPFA( true ) ;
            120
            121        for ( i = 2 ; i <= N ; ++i )
            122            ans += ecost[i] ;
            123
            124        printf("%I64d\n", ans) ;
            125    }

            126
            127    return 0 ;
            128}
            posted on 2008-11-19 23:46 閱讀(291) 評論(0)  編輯 收藏 引用 所屬分類: 圖論
            国产视频久久| 中文字幕久久精品| 国内精品人妻无码久久久影院| 久久精品人人做人人爽97| 久久久国产精品网站| 久久人妻少妇嫩草AV蜜桃| 国产麻豆精品久久一二三| 亚洲欧美另类日本久久国产真实乱对白| 亚洲精品国产第一综合99久久| 精品熟女少妇a∨免费久久| 久久亚洲天堂| 蜜桃麻豆www久久| 久久婷婷五月综合色奶水99啪| 国产精品99久久精品爆乳| 亚洲精品国精品久久99热一| 久久久受www免费人成| 精品久久久久久无码专区| 欧美久久久久久| 国产成人99久久亚洲综合精品 | 亚洲午夜久久久精品影院| 一级做a爰片久久毛片免费陪 | 亚洲国产精品狼友中文久久久| 久久99精品久久只有精品| 久久受www免费人成_看片中文| 国产精品久久久久久久久久免费| 国产亚洲精品久久久久秋霞 | 伊人热人久久中文字幕| 欧美亚洲色综久久精品国产| 怡红院日本一道日本久久 | 久久成人精品| 很黄很污的网站久久mimi色| 欧美亚洲另类久久综合| 国产午夜精品理论片久久影视| 久久精品无码专区免费东京热| 亚洲∧v久久久无码精品| 久久久国产打桩机| 伊人久久大香线蕉综合Av| 婷婷五月深深久久精品| 色偷偷久久一区二区三区| 国产精品久久久久影院嫩草| 色综合久久天天综合|