A Miser Boss

 
A Miser Boss

Time Limit: 2 Seconds Memory Limit: 65536 KB

There are three different lathes in a factory namely A,B,C. They are all able to work on all kinds of workpieces. And there aren workpieces to be processed, denoted by 1,2,..,n.

Due to different internal implemetations, for a specific workpiece i, it costs Aa[i] seconds to finish the job on it while B takes b[i] and C takesc[i] seconds. Each lathe can work on at most one workpiece a time.

Additionally, the Boss is so stingy that he didn't want his lathes to be disengaged at any time before the end of the work. That is:
1. there should not be a time when one of the lathes is in leisure.
2. all lathes should stop simultaneously,and start from time 0.

Your task is to find if there exists an arrangement meeting with the constraints above. if there is,output the minimal time needed to process all the workpieces. Output a lineNO else.

Input

There are multiple test cases. The first line of each case is an integer N(0 < N ≤ 40 ), followed by N lines. In the following N lines, line i contains three integer,a[i],b[i],c[i] (0 < a[i],b[i],c[i] ≤ 120 && 0 < sum(a[i]),sum(b[i]),sum(c[i]) ≤ 120 ) indicating the seconds A,B,C takes to process workpiecei.

Output

Output one line the minimal seconds it takes to process all workpieces within the constraints if there is an arrangement. PrintNO if not.

Sample Input
3
7 1 2
1 7 1
1 3 7
2
1 2 3
3 2 1
Sample Output
1
NO
 
代码转自一个叫 路竹 的人,该作者的网易博客名字叫 留得累人身外物,半肩行李半肩书
连接为http://pictureyong.blog.163.com/blog/static/851870682011102102720520/
 

 

感觉穷举的话需要有3的40次方的复杂度。 文章中说摒弃不必要的枚举,这一句不必要,我费死劲了才看懂。 应该是完成了第k个任务,a,b,c机器用的时间分别是an,bn,cn,那么不管任务是怎么分配的,效果是一样的。所以入队列时,这样的重复就都摒弃了。那么队列最大时也超不过120的3次方,时间复杂度同样不会超过这个。这个复杂度分析的好像没有多大意义。算了,就这样吧。

 

枚举用的是bfs,vis三位数组应该是dp,不过感觉不到dp的存在。


 
 
//思路:BFS枚举所有可能的生产方案,vis[][][]做优化摒弃不必要的枚举。
//代码:

#include<cstdio>
#include<cstring>
#include<queue>

#define INF_MAX 0xfffff
#define SIZE_N 42
#define MAX_N 122

using namespace std;

typedef struct{
    int an,bn,cn;//an:A机器的耗时,bn:B机器的耗时,cn:C机器的耗时
    int tn;//完成前 tn 个
}SNode;

int vis[MAX_N][MAX_N][MAX_N];//vis[i][j][k] = n;完成第 n 个零件后,A耗时 i,B耗时 j,C耗时 k 
int as[SIZE_N],bs[SIZE_N],cs[SIZE_N];

int BFS_DP(int n)
{
    int an,bn,cn,nn,ans;
    queue<SNode>que;
    SNode cur,next;

    memset(vis,0,sizeof(vis));
    cur.an = cur.bn = cur.cn = cur.tn = 0;
    que.push(cur);
    ans = INF_MAX;
    while(!que.empty()) {
        cur = que.front();
        que.pop();
        an = cur.an;
        bn = cur.bn;
        cn = cur.cn;
        nn = cur.tn + 1;
        if(nn == n + 1) {
            if(an == bn && bn == cn && an < ans){
                ans = an;
            }
            continue;
        }
        if(vis[an+as[nn]][bn][cn] != nn) {
            vis[an+as[nn]][bn][cn] = nn;
            next.an = an + as[nn];
            next.bn = bn;
            next.cn = cn;
            next.tn = nn;
            que.push(next);
        }
        if(vis[an][bn+bs[nn]][cn] != nn) {
            vis[an][bn+bs[nn]][cn] = nn;
            next.an = an;
            next.bn = bn + bs[nn];
            next.cn = cn;
            next.tn = nn;
            que.push(next);
        }
        if(vis[an][bn][cn+cs[nn]] != nn) {
            vis[an][bn][cn+cs[nn]] = nn;
            next.an = an;
            next.bn = bn;
            next.cn = cn + cs[nn];
            next.tn = nn;
            que.push(next);
        }
    }
    return ans;
}

int main()
{
    int n,i,ans;

    while(scanf("%d",&n) != EOF) {
        for(i = 1;i <= n;i ++) {
            scanf("%d%d%d",&as[i],&bs[i],&cs[i]);
        }

        if(n < 3) {puts("NO");continue;}
        ans = BFS_DP(n);
        if(ans == INF_MAX)
            puts("NO");
        else printf("%d\n",ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/ConfuciusPei/archive/2011/11/15/5118422.html

基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值