• <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>
            posts - 100,  comments - 15,  trackbacks - 0
             1#include <limits.h>
             2#include <stdio.h>
             3#include <stdlib.h>
             4
             5/* Let INFINITY be an integer value not likely to be
             6   confused with a real weight, even a negative one. */

             7#define INFINITY ((1 << 14)-1)
             8
             9typedef struct {
            10    int source;
            11    int dest;
            12    int weight;
            13}
             Edge;
            14
            15void BellmanFord(Edge edges[], int edgecount, int nodecount, int source)
            16{
            17    int *distance = (int*) malloc (nodecount * sizeof (*distance));    //int
            18    int i, j;
            19
            20    for (i=0; i < nodecount; ++i)
            21      distance[i] = INFINITY;
            22    distance[source] = 0;
            23
            24    for (i=0; i < nodecount; ++i) 
            25    {
            26       int somethingchanged = 0
            27       for (j=0; j < edgecount; ++j) 
            28       {
            29            if (distance[edges[j].source] != INFINITY) 
            30            {
            31                int new_distance = distance[edges[j].source] + edges[j].weight;
            32                if (new_distance < distance[edges[j].dest])
            33                {
            34                  distance[edges[j].dest] = new_distance;
            35                  somethingchanged = 1;
            36                }
             
            37            }

            38        }

            39        /* if one iteration had no effect, further iterations will have no effect either */
            40        if (!somethingchanged) break;
            41    }

            42
            43    for (i=0; i < edgecount; ++i) 
            44    {
            45        if (distance[edges[i].dest] > distance[edges[i].source] + edges[i].weight) 
            46        {
            47            puts("Negative edge weight cycles detected!");
            48            free(distance);
            49            return;
            50        }

            51    }

            52
            53    for (i=0; i < nodecount; ++i) {
            54        printf("The shortest distance between nodes %d and %d is %d\n",
            55            source, i, distance[i]);
            56    }

            57
            58    free(distance);
            59    return;
            60}

            61
            62int main(void)
            63{
            64    /* This test case should produce the distances 2, 4, 7, -2, and 0. */
            65    Edge edges[10= {{0,15}{0,28}{0,3-4}{1,0-2},
            66                      {2,1-3}{2,39}{3,17}{3,42},
            67                      {4,06}{4,27}}
            ;
            68    BellmanFord(edges, 1054);
            69    return 0;
            70}

            71
            posted on 2009-04-03 22:08 wyiu 閱讀(194) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            热re99久久精品国99热| 久久久网中文字幕| 青青热久久国产久精品 | 欧美黑人激情性久久| 久久国产视屏| 亚洲国产成人精品久久久国产成人一区二区三区综 | 日韩美女18网站久久精品| 天天久久狠狠色综合| 久久这里只精品国产99热| 狠狠色丁香久久婷婷综| 久久精品国产免费| 狠狠色综合久久久久尤物 | 日韩欧美亚洲综合久久影院Ds| 欧美成a人片免费看久久| 久久久午夜精品福利内容| 无码人妻久久一区二区三区蜜桃 | 国产精品美女久久久m| 国产精品免费福利久久| 亚洲一本综合久久| 国产精品美女久久久久av爽| 国产精品免费久久久久久久久 | 久久精品国产欧美日韩| 婷婷久久综合九色综合绿巨人| 中文无码久久精品| 国产精品久久久久aaaa| 精品久久国产一区二区三区香蕉| 久久毛片一区二区| 久久99免费视频| 久久国产欧美日韩精品| 国产一区二区三区久久精品| 色婷婷久久久SWAG精品| 久久精品国产99久久无毒不卡| 成人精品一区二区久久久| 精品久久久久久国产| 亚洲一区中文字幕久久| 久久久久久精品免费看SSS| 色综合久久天天综合| 国产69精品久久久久久人妻精品| 亚洲国产成人久久综合一| 久久综合亚洲色HEZYO社区| 久久精品国产精品亚洲精品|