【算法】程序填空

在这里插入图片描述

(1)d[v]>d[u]+w(u,v)
(2)Init-single-source(G,s)
(3)Φ
(4)Relax(u,v,w)

在这里插入图片描述

在这里插入图片描述

(5)m[n][j]=0;
(6)m[i+1][j]
(7)m[i+1][j],m[i+1][j-w[i]]+v[i]
(8)c>=w[1]

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

给定n种物品和一个背包,物品i的重量是w[i], 其价值是p[i], 背包的容量为c。设物品已按单位重量价值递减的次序排序。每种物品不可以装入背包多次,但可以装入部分的物品i。求解背包问题的贪心算法如下:

float Knapsack (float x[ ], float w[ ], float p[ ],float c, int n)  

{ float maxsum=     1      ;   // maxsum表示装进包的物品价值总和               

for ( int i=1;i<=n; i++)  x[i]=0 // x[i]=0表示第i个物品未装进包         

for (            2             )                        

         if(        3        )                                

{ x[i] =1;                                 

             maxsum+=       4      ;                           

             c- = w[i];                                

       }

         else break;                               

if (c>0) {x[i]=c/w[i];         5          ;}      

return maxsum;                             

}

int i=1;i<=n;i++

w[i]<=C

p[i]

maxsum+ = p[i] * c / w[i]


三、算法填空

1.背包问题的贪心算法

void Knapsack(int n,float M,float v[],float w[],float x[])

{

       Sort(n,v,w);

       int i;

       for (i=1;i<=n;i++) x[i]=0;

       float c=M;

       for (i=1;i<=n;i++) {

          if (w[i]>c) break;

          x[i]=1;

          **c - =w[i];**

          }

       if (i<=n) **x[i]=c/w[i];**

}

2.最大子段和: 动态规划算法

int MaxSum(int n, int a[])

{

    int sum=0, b=0//sum存储当前最大的b[j], b存储b[j]

    for(int j=1; j<=n; j++)  { 

        if (b>0)  b+= a[j]**else  b=a[i];**//一旦某个区段和为负,则从下一个位置累和

 if(b>sum) **sum=b;**

 

     }

     return sum;

 }        

3.贪心算法求装载问题

template<class Type>

void Loading(int x[],  Type w[], Type c, int n)

{

        int *t = new int [n+1];

                             **加粗样式**;

        for (int i = 1; i <= n; i++) x[i] = 0;

        for (int i = 1; i <= n && w[t[i]] <= c; i++)

 {x[t[i]] = 1;

          **加粗样式** ;}

}

4.贪心算法求活动安排问题

template<class Type>

void GreedySelector(int n, Type s[], Type f[], bool A[])

{

       **A[1]=true;**

       int j=1;

       for (int i=2;i<=n;i++) {

          if (s[i]>=f[j])

		{  
			**A[i]=true;**

			**j=i;**

 }

          else A[i]=false;

          }

}

 

5.快速排序

template<class Type>

void QuickSort (Type a[], int p, int r)

{

      if (p<r) {

        int q=Partition(a,p,r);

        QuickSort (a,p,q-1); //对左半段排序

        QuickSort (a,q+1,r); //对右半段排序

        }

}

 

6.排列问题

Template <class Type>

void perm(Type list[],  int k, int m )

{ //产生[list[k:m]的所有排列

    **if(k==m)**

     {  //只剩下一个元素

         for (int i=0;i<=m;i++)  cout<<list[i];

         cout<<endl;

    }

    else  //还有多个元素待排列,递归产生排列

       **for (int i=k; i<=m; i++)**

        {

           swap(list[k],list[i]);

           **perm(list,k+1;m);**

           swap(list[k],list[i]);        

     }

  }
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是PTA最短路径迪杰斯特拉算法程序填空代码: ```python #include <stdio.h> #include <stdlib.h> #define MaxVertexNum 501 #define INFINITY 65535 typedef int Vertex; typedef int WeightType; typedef char DataType; typedef struct GNode *PtrToGNode; struct GNode{ int Nv; int Ne; WeightType G[MaxVertexNum][MaxVertexNum]; DataType Data[MaxVertexNum]; }; typedef PtrToGNode MGraph; typedef struct ENode *PtrToENode; struct ENode{ Vertex V1, V2; WeightType Weight; }; typedef PtrToENode Edge; MGraph CreateGraph( int VertexNum ) { Vertex V, W; MGraph Graph; Graph = (MGraph)malloc(sizeof(struct GNode)); Graph->Nv = VertexNum; Graph->Ne = 0; for (V=0; V<Graph->Nv; V++) for (W=0; W<Graph->Nv; W++) Graph->G[V][W] = INFINITY; return Graph; } void InsertEdge( MGraph Graph, Edge E ) { Graph->G[E->V1][E->V2] = E->Weight; Graph->G[E->V2][E->V1] = E->Weight; } MGraph BuildGraph() { MGraph Graph; Edge E; int Nv, i; scanf("%d", &Nv); Graph = CreateGraph(Nv); scanf("%d", &(Graph->Ne)); if ( Graph->Ne != 0 ) { E = (Edge)malloc(sizeof(struct ENode)); for (i=0; i<Graph->Ne; i++) { scanf("%d %d %d", &E->V1, &E->V2, &E->Weight); E->V1--; E->V2--; InsertEdge( Graph, E ); } } return Graph; } void PrintDist( WeightType dist[], int N ) { int i; for ( i=0; i<N; i++ ) printf("%d ", dist[i]); } void PrintPath( int path[], int V ) { if ( path[V] != -1 ) { PrintPath( path, path[V] ); printf(" %d", V); } else printf("%d", V); } void Dijkstra( MGraph Graph, WeightType dist[], int path[], Vertex S ) { int collected[MaxVertexNum]; Vertex V, W, MinV; WeightType MinDist; for ( V=0; V<Graph->Nv; V++ ) { dist[V] = Graph->G[S][V]; if ( dist[V]<INFINITY ) path[V] = S; else path[V] = -1; collected[V] = 0; } dist[S] = 0; collected[S] = 1; while (1) { MinDist = INFINITY; MinV = -1; for ( V=0; V<Graph->Nv; V++ ) { if ( collected[V]==0 && dist[V]<MinDist ) { MinDist = dist[V]; MinV = V; } } if (MinV==-1) break; collected[MinV] = 1; for( W=0; W<Graph->Nv; W++ ) { if ( collected[W]==0 && Graph->G[MinV][W]<INFINITY ) { if ( Graph->G[MinV][W]<0 ) { printf("Error: Negative Dist!\n"); return; } if ( dist[MinV]+Graph->G[MinV][W] < dist[W] ) { dist[W] = dist[MinV]+Graph->G[MinV][W]; path[W] = MinV; } } } } } int main() { int i; WeightType dist[MaxVertexNum]; int path[MaxVertexNum]; MGraph G = BuildGraph(); Vertex S = 0; Dijkstra( G, dist, path, S ); for ( i=1; i<G->Nv; i++ ) { printf("%d\n", dist[i]); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值