• <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   管理


            av无码久久久久久不卡网站| 99久久人妻无码精品系列蜜桃| 亚洲乱码日产精品a级毛片久久 | 久久人人爽人人澡人人高潮AV | 精品综合久久久久久97| 欧美精品久久久久久久自慰| 97久久久精品综合88久久| 国内精品久久久久久久影视麻豆| 老男人久久青草av高清| 亚洲一本综合久久| 亚洲欧美日韩中文久久| 久久久久亚洲精品天堂久久久久久| 久久精品国产久精国产一老狼| 亚洲国产天堂久久综合网站| 伊人久久大香线蕉综合Av| 久久黄视频| 93精91精品国产综合久久香蕉| 久久亚洲AV成人出白浆无码国产| 久久亚洲精品无码播放| 久久国产成人精品麻豆| 丰满少妇高潮惨叫久久久| 伊人久久大香线蕉av不变影院| 欧美粉嫩小泬久久久久久久| 久久九九亚洲精品| 狠狠色婷婷久久一区二区三区| 少妇高潮惨叫久久久久久| 亚洲欧美久久久久9999| 久久久久久亚洲精品不卡 | 亚洲人AV永久一区二区三区久久 | 国产精品免费福利久久| 丁香色欲久久久久久综合网| 久久这里的只有是精品23| 久久激情五月丁香伊人| 久久99精品久久久久久秒播| 996久久国产精品线观看| 欧美熟妇另类久久久久久不卡| 7777久久久国产精品消防器材| 亚洲国产日韩欧美综合久久| 色99久久久久高潮综合影院 | 狠狠久久亚洲欧美专区| 久久精品国产亚洲AV无码娇色|