算法设计 第二次上机 All discs considered

本文介绍了一种算法,用于计算安装由两个DVD组成的操作系统时所需的最小DVD更换次数。该算法考虑了软件包间的依赖关系,并假设只有一个DVD光驱可用。

总时间限制: 
10000ms 
内存限制: 
65536kB
描述
Operating systems are large software artefacts composed of many packages, usually distributed on several media, e.g., discs. You probably remember the time when your favourite operating system was delivered on 21 floppy discs, or, a few years later, on 6 CDs. Nowadays, it will be shipped on several DVDs, each containing tens of thousands of packages. 

The installation of certain packages may require that other packages have been installed previously. Therefore, if the packages are distributed on the media in an unsuitable way, the installation of the complete system requires you to perform many media changes, provided that there is only one reading device available, e.g., one DVD-ROM drive. Since you have to start the installation somehow, there will of course be one or more packages that can be installed independently of all other packages. 

Given a distribution of packages on media and a list of dependences between packages, you have to calculate the minimal number of media changes required to install all packages. For your convenience, you may assume that the operating system comes on exactly 2 DVDs. 
输入
The input contains several test cases. Every test case starts with three integers N1, N2, D. You may assume that 1<=N1,N2<=50000 and 0<=D<=100000. The first DVD contains N1 packages, identified by the numbers 1, 2, ..., N1. The second DVD contains N2 packages, identified by the numbers N1+1, N1+2, ..., N1+N2. Then follow D dependence specifications, each consisting of two integers xi, yi. You may assume that 1<=xi,yi<=N1+N2 for 1<=i<=D. The dependence specification means that the installation of package xi requires the previous installation of package yi. You may assume that there are no circular dependences. The last test case is followed by three zeros.
输出
For each test case output on a line the minimal number of DVD changes required to install all packages. By convention, the DVD drive is empty before the installation and the initial insertion of a disc counts as one change. Likewise, the final removal of a disc counts as one change, leaving the DVD drive empty after the installation.
样例输入
3 2 1
1 2
2 2 2
1 3
4 2
2 1 1
1 3
0 0 0
样例输出
3
4
3
来源
Ulm Local 2004


拓扑排序,需要维护两个队列用作拓扑。


#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <string.h>

using namespace std;

vector<int> graph[100005];
int indegree[100005];
int n1, n2;

int topSort(int n)
{
    queue<int> dis[2];
    int in[100005];
    int cost = 1;
    memcpy(in, indegree, sizeof(indegree));
    for(int i = 1; i <= n1; i++)
        if(in[i] == 0)
            dis[0].push(i);
    for(int i = n1 + 1; i <= n1 + n2; i++)
        if(in[i] == 0)
            dis[1].push(i);
    while(!dis[0].empty() || !dis[1].empty())
    {
        while(!dis[n].empty())
        {
            int now = dis[n].front();
            dis[n].pop();
            for(int i = 0; i < graph[now].size(); i++)
            {
                in[graph[now][i]]--;
                if(in[graph[now][i]] == 0)
                {
                    if(graph[now][i] > n1)
                        dis[1].push(graph[now][i]);
                    else
                        dis[0].push(graph[now][i]);
                }
            }
        }
        if(n == 0)
            n = 1;
        else
            n = 0;
        cost++;
    }
    return cost;
}

int main()
{
    ios::sync_with_stdio(false);
    //freopen("test.txt", "r", stdin);
    int d;
    while(cin >> n1 >> n2 >> d)
    {
        if(n1 == 0 && n2 == 0 && d == 0)
            break;
        for(int i = 0; i < 100005; i++)
            graph[i].clear();
        memset(indegree, 0, sizeof(indegree));
        int x, y;
        for(int i = 0; i < d; i++)
        {
            cin >> x >> y;
            graph[y].push_back(x);
            indegree[x]++;
        }
        cout << min(topSort(0), topSort(1)) << endl;
    }
    return 0;
}



【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值