leetcode不知道多少题

一堆程序,有互斥的,只要不互斥就可以同时运行,互斥的只能单独运行。求最短的运行时间,时间=可以一起运行程序中的最大时间+n个单独运行程序时间

思路

  1. 01背包,搜就行了
#include<stdio.h>
#include<string.h>
int min(int a, int b){
    if(a<b)
        return a ;
    else return b;
}
int max(int a,int b){
    if(a>b)
        return a;
    else
        return b;
}
int canput(int *stark,int start,int *top,int **choice,int end){
    int j;
    for(j=0;j<*top;j++){
        if(*((int*)choice+end*stark[j]+start)==1)
            return 0;
    }
    return 1;

}
int time1(int *cast,int *stark,int end,int *top){
    int j ,i,r=0,maxcast=0;
    for(j=0;j<*top;j++){
        maxcast = max(cast[stark[j]],maxcast);//cast的标号写错
    }
    i=0;j=0;
    int flag[end];
    memset(flag,0,sizeof(int)*end);
    for(j=0;j<*top;j++){
        flag[stark[j]]=1;
    }
    for(j=0;j<end;j++){//找不能一起运行的程序
        if(flag[j]==0){
            r = r+cast[j];
        }
    }
    r = r + maxcast;
    return r;//忘记return了·····
}
void tree8(int *stark,int start,int end,int *cast,int **choice,int *result,int *top){
    if(start==end){
        int r1 = time1(cast,stark,end,top);
        *result = min(r1,*result);
        return;
    }
    else {  //搜就完事了
        if(canput(stark,start,top,choice,end)){
            stark[(*top)++]=start;
            tree8(stark,start+1,end,cast,choice,result,top);
            (*top)--;
        }
        tree8(stark,start+1,end,cast,choice,result,top);
    }
}

void calautor(int n,int *cast,int **choice,int *result){
    int stark[n];
    memset(stark,-1,sizeof(int)*n);
    *result = 9999;
    int top =0;
    tree8(stark,0,n,cast,choice,result,&top);
}

int main(){
    int n = 4;
    int choice[4][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,1},{0,0,1,0}};
    int result = 0;
    int cast[4] = {1,8,7,4};
    calautor(n,cast,(int**)choice,&result);
    printf("reslut = %d\n",result);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值