• Dijkstra----------


     

    hnldyhy(303882171) 11:00:25
    // Dijkstra 邻接矩阵的下标从1开始

    #include <stdio.h>
    #define N 201
    #define INF 1000000000

    int g[N][N],bz[N],dist[N],s,t,n;

    void Dijkstra(int s)
    { int i,j,min,k,x;
    for (i=1;i<=n;i++)
    { dist[i]=g[s][i]; bz[i]=0; }
    bz[s]=1;
    for (i=1;i<=n;i++)
    { printf("dist[] ");
    for (x=1;x<=n;x++)
    printf("%d ",dist[x]); printf(" ");
    printf("bz[] ");
    for (x=1;x<=n;x++)
    printf("%d ",bz[x]); printf(" ");
    min=INF;
    for (k=1;k<=n;k++)
    if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
    bz[j]=1;
    for (k=1;k<=n;k++)
    if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
    }
    }
    int main()
    { int i,j;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    for (j=1;j<=n;j++)
    scanf("%d",&g[i][j]);
    scanf("%d",&s);
    Dijkstra(s);
    for (i=1; i<=n; i++) printf("%d ",dist[i]);
    printf(" ");
    }


    /*
    5
    0 10 200 11 200
    10 0 13 18 200
    200 13 0 12 15
    11 18 12 0 16
    200 200 15 16 0
    1
    */


    // Dijkstra 邻接矩阵的下标从1开始

    #include <stdio.h>
    #define N 201
    #define INF 1000000000

    int g[N][N],bz[N],dist[N],s,t,n;

    void Dijkstra(int s)
    { int i,j,min,k,x;
    for (i=1;i<=n;i++)
    { dist[i]=g[s][i]; bz[i]=0; }
    bz[s]=1;
    for (i=1;i<=n;i++)
    { printf("dist[] ");
    for (x=1;x<=n;x++)
    printf("%d ",dist[x]); printf(" ");
    printf("bz[] ");
    for (x=1;x<=n;x++)
    printf("%d ",bz[x]); printf(" ");
    min=INF;
    for (k=1;k<=n;k++)
    if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
    bz[j]=1;
    for (k=1;k<=n;k++)
    if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
    }
    }
    int main()
    { int i,j;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    for (j=1;j<=n;j++)
    scanf("%d",&g[i][j]);
    scanf("%d",&s);
    Dijkstra(s);
    for (i=1; i<=n; i++) printf("%d ",dist[i]);
    printf(" ");
    }


    /*
    5
    0 10 200 11 200
    10 0 13 18 200
    200 13 0 12 15
    11 18 12 0 16
    200 200 15 16 0
    1
    */

    // Dijkstra 邻接矩阵的下标从1开始

    #include <stdio.h>
    #define N 201
    #define INF 1000000000

    int g[N][N],bz[N],dist[N],s,t,n;

    void Dijkstra(int s)
    { int i,j,min,k,x;
    for (i=1;i<=n;i++)
    { dist[i]=g[s][i]; bz[i]=0; }
    bz[s]=1;
    for (i=1;i<=n;i++)
    { printf("dist[] ");
    for (x=1;x<=n;x++)
    printf("%d ",dist[x]); printf(" ");
    printf("bz[] ");
    for (x=1;x<=n;x++)
    printf("%d ",bz[x]); printf(" ");
    min=INF;
    for (k=1;k<=n;k++)
    if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
    bz[j]=1;
    for (k=1;k<=n;k++)
    if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
    }
    }
    int main()
    { int i,j;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    for (j=1;j<=n;j++)
    scanf("%d",&g[i][j]);
    scanf("%d",&s);
    Dijkstra(s);
    for (i=1; i<=n; i++) printf("%d ",dist[i]);
    printf(" ");
    }


    /*
    5
    0 10 200 11 200
    10 0 13 18 200
    200 13 0 12 15
    11 18 12 0 16
    200 200 15 16 0
    1
    */

    // Dijkstra 邻接矩阵的下标从1开始

    #include <stdio.h>
    #define N 201
    #define INF 1000000000

    int g[N][N],bz[N],dist[N],s,t,n;

    void Dijkstra(int s)
    { int i,j,min,k,x;
    for (i=1;i<=n;i++)
    { dist[i]=g[s][i]; bz[i]=0; }
    bz[s]=1;
    for (i=1;i<=n;i++)
    { printf("dist[] ");
    for (x=1;x<=n;x++)
    printf("%d ",dist[x]); printf(" ");
    printf("bz[] ");
    for (x=1;x<=n;x++)
    printf("%d ",bz[x]); printf(" ");
    min=INF;
    for (k=1;k<=n;k++)
    if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
    bz[j]=1;
    for (k=1;k<=n;k++)
    if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
    }
    }
    int main()
    { int i,j;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    for (j=1;j<=n;j++)
    scanf("%d",&g[i][j]);
    scanf("%d",&s);
    Dijkstra(s);
    for (i=1; i<=n; i++) printf("%d ",dist[i]);
    printf(" ");
    }


    /*
    5
    0 10 200 11 200
    10 0 13 18 200
    200 13 0 12 15
    11 18 12 0 16
    200 200 15 16 0
    1
    */


    // Dijkstra 邻接矩阵的下标从1开始

    #include <stdio.h>
    #define N 201
    #define INF 1000000000

    int g[N][N],bz[N],dist[N],s,t,n;

    void Dijkstra(int s)
    { int i,j,min,k,x;
    for (i=1;i<=n;i++)
    { dist[i]=g[s][i]; bz[i]=0; }
    bz[s]=1;
    for (i=1;i<=n;i++)
    { printf("dist[] ");
    for (x=1;x<=n;x++)
    printf("%d ",dist[x]); printf(" ");
    printf("bz[] ");
    for (x=1;x<=n;x++)
    printf("%d ",bz[x]); printf(" ");
    min=INF;
    for (k=1;k<=n;k++)
    if ( dist[k]<min && bz[k]==0 ) { min=dist[k]; j=k; }
    bz[j]=1;
    for (k=1;k<=n;k++)
    if ( min+g[j][k]<dist[k] && bz[k]==0) dist[k]=min+g[j][k];
    }
    }
    int main()
    { int i,j;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    for (j=1;j<=n;j++)
    scanf("%d",&g[i][j]);
    scanf("%d",&s);
    Dijkstra(s);
    for (i=1; i<=n; i++) printf("%d ",dist[i]);
    printf(" ");
    }


    /*
    5
    0 10 200 11 200
    10 0 13 18 200
    200 13 0 12 15
    11 18 12 0 16
    200 200 15 16 0
    1
    */

    hnldyhy(303882171) 11:02:32
    /*最小生成树,用Prim算法,邻接矩阵的下标从1开始*/

    #include<stdio.h>
    #define INF 200
    int g[110][110],bz[110],low[110];
    int min,ans,Q,n,a,b;

    void prim()
    { int i,j,k,x;
    for (i=1;i<=n;i++)
    { low[i]=g[1][i]; bz[i]=0; }
    ans=0; bz[1]=1;
    for (i=1;i<n;i++)
    { printf("low[] ");
    for (x=1;x<=n;x++)
    printf("%d ",low[x]); printf(" ");
    printf("bz[] ");
    for (x=1;x<=n;x++)
    printf("%d ",bz[x]); printf(" ");
    min=INF;
    for (k=1;k<=n;k++)
    if ( (low[k]<min)&& ( bz[k]==0) ) { min=low[k]; j=k; }
    ans+=min;
    printf("min=%d ans=%d ",min, ans);
    bz[j]=1;
    for (k=1;k<=n;k++)
    if ( (g[j][k]<low[k])&&(bz[k]==0)) low[k]=g[j][k];
    }
    }
    int main()
    { int i,j;
    while (scanf("%d",&n)!=EOF)
    {
    for (i=1;i<=n;i++)
    for (j=1;j<=n;j++)
    scanf("%d",&g[i][j]);
    prim();
    printf("ans=%d ",ans);
    }
    }

    /*
    5
    200 10 200 11 200
    10 200 13 18 200
    200 13 200 12 15
    11 18 12 200 16
    200 200 15 16 200
    */

  • 相关阅读:
    【C++】未定义的标识符“_TCHAR*”
    【C++】编写动态库DLL
    【C++】使用SQLite步骤及示例
    【VS】如何查询该项目是什么样工程类型
    新概念英语第一册单词
    地道情景表达一
    日常学习经典口语一
    全国省市区sql脚本
    关于Object.defineProperty
    响应式原理
  • 原文地址:https://www.cnblogs.com/2014acm/p/3905620.html
Copyright © 2020-2023  润新知