如何使用一组重叠最小的范围覆盖一个范围? [英] How to cover a range using a set of ranges with minimal overlap?

查看:20
本文介绍了如何使用一组重叠最小的范围覆盖一个范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有 n 个任务和一组 m 个人,每个人可以完成一系列任务(Ti 到 Tj).完成每项任务的成本是 k* no.完成该任务的人.如果可能,至少完成一次所有任务的最低成本是多少.我觉得这是一个动态规划问题,但我无法达到最佳方程.有人能帮我找到正确的方程式或上面的代码块吗?为了更好地理解,我附上了几个例子.

Assume that there are n tasks and a group of m people which can each do a range of tasks (Ti to Tj). The cost of completing each task is k* no. of people who have completed that task. What will be minimum cost to complete all the tasks atleast once, if possible. I feel that is a Dynamic Programming problem but I am unable to reach to the optimal equation. Can someone help me reach the correct equation or the code block for above. I have attached a couple of examples for better understanding.

n:4
m:3
Range of tasks for m people: {(3,4);(1,2);(2,3)}
Answer: m1 & m2 can complete all 4 tasks so cost is 4.

Ex2:
n:4
m:2
Range of tasks for m people: {(1,3);(2,4)}
Answer: m1 & m2 are both required to complete all 4 tasks so cost is 6.

推荐答案

这里是贪心算法 - 总是最好的起点.

Here is the greedy algorithm - always the best place to start.

allocate all teams
IF not all sections covered
    output -1
    stop
mark all teams non-critical
flag_improved = true
WHILE( flag_improved == true )
   flag_improved = false
   find most expensive section 
   find most expensive non-critical team on most expensive section 
   IF team found that can be removed without leaving a section uncovered
       remove team
       flag_improved = true
   ELSE
       mark team critical
output cost - sum coverage of remaining teams

这是实现该算法的 C++ 应用程序的主要功能

Here is the main function of a C++ application implementing this algorithm

main()
{
    std::vector<cTeam> teams;
    int bridge_section_count;
    Input( bridge_section_count, teams);

    // allocate all teams
    for (int s = 0; s < bridge_section_count; s++)
        Bridge.insert(std::pair(s, std::vector<cTeam>(0)));
    for (auto &t : teams)
        for (int s = t.myRange.first; s <= t.myRange.second; s++)
        {
            Bridge[s].push_back(t);
        }

    // check every section has at least one team allocated
    if (!IsCovered())
    {
        std::cout << "-1\n";
        exit(1);
    }

    // loop while improvements are being found
    bool flag_improved = true;
    while (flag_improved)
    {
        flag_improved = false;

        auto most_expensive_section = find_most_expensive_section();

        while (1)
        {
            // loop over teams allocated to most expensive section
            std::vector<cTeam>::iterator most_expensive_team;
            if (!find_most_expensive_non_critical_team(
                    most_expensive_team,
                    most_expensive_section))
            {
                break;
            }

            // check can team be removed without leaving section uncovered
            if (testRemoval(*most_expensive_team))
            {
                // remove team
                doRemoval(*most_expensive_team);
                flag_improved = true;
                break;
            }
            else
            {
                // this team is critical, it cannot be removed
                most_expensive_team->myCritical = true;
            }
        }
    }
    printResult();
}

完整的应用程序代码位于 https://gist.github.com/JamesBremner/ada6210a8517671a8517671abd45e882c97d526d"一个>

The complete application code is at https://gist.github.com/JamesBremner/ada6210a8517671abd45e882c97d526d

这篇关于如何使用一组重叠最小的范围覆盖一个范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆