• 多段图动态规划算法的实现(DP)


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <limits.h>
    
    #define MAX_VERTEX_NUM 20
    #define MAX_VALUE_TYPE INT_MAX
    
    typedef int VertexType;
    
    
    typedef struct node{
        VertexType adjvex;
        int weight; //权值
        struct node *next;
    }EdgeNode;
    
    typedef struct vnode{
        VertexType vertex;  //顶点域
        EdgeNode *firstedge;
    }VertexNode;
    
    typedef VertexNode AdjList[MAX_VERTEX_NUM];
    
    typedef struct{
        AdjList adjlist;
        int n,e;    //顶点数和边数
    }ALGraph;
    /*
    int Locate(ALGraph *G, int n){
        int i;
        for(i=0; i<G->n; i++){
            if(G->adjlist[i].vertex == n) return i;
        }
        return -1;
    }*/
    
    void CreateALGraph(ALGraph *G){//建立有向图的邻接表
        int i,j;
        int k;
        EdgeNode *s;
        scanf("%d%d", &G->n, &G->e);
        for(i=0; i<G->n; i++){
            scanf("%d", &G->adjlist[i].vertex);//G->adjlist[i].vertex = getch();
            G->adjlist[i].firstedge = NULL; //边表设置成空表
        }
        for(k=0; k<G->e; k++){
            scanf("%d%d", &i, &j);
        //    i = Locate(G, i); j = Locate(G, j); //查找结点序号
            s = (EdgeNode *)malloc(sizeof(EdgeNode));
            scanf("%d", &s->weight);
            s->adjvex = j;  //邻接点序号为j
            s->next = G->adjlist[i].firstedge;
            G->adjlist[i].firstedge = s;
    
            //无向图时加上
         //   s = (EdgeNode *)malloc(sizeof(EdgeNode));
         //   s->adjvex = i;
         //   s->next = G->adjlist[j].firstedge;
         //   G->adjlist[j].firstedge = s;
        }
    }
    
    VertexType fgraph(ALGraph *G, int route[], int n){
        int i;
        EdgeNode *pnode;
    
        int *path = (int *)malloc(n*sizeof(int));
        VertexType min_cost, *cost = (VertexType*)malloc(n*sizeof(VertexType));
        for(i=0; i<n; i++){
            cost[i] = MAX_VALUE_TYPE; path[i] = -1; route[i] = 0;
        }
        cost[n-1] = 0;
        for(i=n-1; i>=0; i--){
            pnode = G->adjlist[i].firstedge;
            while(pnode != NULL){
                if(pnode->weight + cost[pnode->adjvex] < cost[i]){
                    cost[i] = pnode->weight + cost[pnode->adjvex];
                    path[i] = pnode->adjvex;
                }
                pnode = pnode->next;
            }
        }
        i = 0;
        while((route[i] != n-1) && (path[i] != -1)){
            i++;
            route[i] = path[route[i-1]];
        }
        min_cost = cost[0];
        free(path); free(cost);
        return min_cost;
    }
    
    
    void print(ALGraph *G){
        EdgeNode *p;
        int i;
        for(i=0; i<G->n; i++){
            printf("index %d VERTEX %d", i, G->adjlist[i].vertex);
            for(p = G->adjlist[i].firstedge; p; p = p->next){
                printf("->\tVERTEX %d weight %d", p->adjvex, p->weight);
            }
            putchar('\n');
        }
    }
    
    int main(){
        freopen("d:\\my.txt", "r", stdin);
        int route[MAX_VERTEX_NUM];
        ALGraph G;
        CreateALGraph(&G);
        print(&G);
        printf("%d", fgraph(&G, route, G.n));
        int i;
        for(i=0; i<G.n; i++){
            printf("\t%d", route[i]);
        }
        return 0;
    }
    my.txt
    
    10 19
    0 1 2 3 4 5 6 7 8 9
    0 1 4
    0 2 1
    0 3 3
    2 3 1
    1 4 9
    1 5 8
    2 4 6
    2 5 7
    2 6 8
    3 5 4
    3 6 7
    4 7 5
    4 8 6
    5 7 8
    5 8 6
    6 7 6
    6 8 5
    7 9 7
    8 9 3



  • 相关阅读:
    Fiddler抓包2-只抓APP的请求【转载】
    Fiddler抓包1-抓firefox上https请求【转载】
    python笔记-用python解决小学生数学题【转载】
    python笔记-冒泡排序【转载】
    python爬虫搜片利器fmovice【转载】
    httpd: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
    linux_硬件信息
    Django rest framework:__str__ returned non-string (type NoneType) 真正原因
    centos如何安装python库?
    linux如何自动获取ip地址
  • 原文地址:https://www.cnblogs.com/tanhehe/p/2883509.html
Copyright © 2020-2023  润新知