有 4 名同学到一家公司参加三个阶段的面试,公司要求每个同学都必须首先找公司秘书初 试,然后到部门主管处复试,最后到经理处参加面试,并且不允许插队(前在任何一个阶段 4 名同学的顺序是一样的),由于 4 名同学的专业背景不同,所以每人在三个阶段的而试时 间也不同,如下表所示 (单位:min):

秘书初试 主管复试 经理面试
同学甲 13 15 20
同学乙 10 20 18
同学丙 20 16 10
同学丁 8 10 15

最短时间与面试顺序有关,枚举顺序,求得最优解。

#include<bits/stdc++.h>

#define INF 0x3f3f3f3f
using namespace std;
const int N = 7;
int tim[N][N] = {
        {0, 0,  0,  0},
        {0, 13, 15, 20},
        {0, 10, 20, 18},
        {0, 20, 16, 10},
        {0, 8,  10, 15}
};
int seq[N] = {0, 1, 2, 3, 4};
int now[N][N], sch[N * N][N], tot;

inline void out(int x) {
    switch (x) {
        case 1:
            printf("甲");
            break;
        case 2:
            printf("乙");
            break;
        case 3:
            printf("丙");
            break;
        default:
            printf("丁");
    }
}

int main() {
    int ans = INF;
    do {
        memset(now, 0, sizeof(now));
        for (int i = 1; i <= 4; ++i)
            for (int j = 1; j <= 3; ++j)
                now[seq[i]][j] = max(now[seq[i]][j - 1], now[seq[i - 1]][j]) + tim[seq[i]][j];
        if (now[seq[4]][3] == ans)tot++, memcpy(sch[tot], seq, sizeof(seq));
        if (now[seq[4]][3] < ans) tot = 1, ans = now[seq[4]][3], memcpy(sch[tot], seq, sizeof(seq));
        ans = min(ans, now[seq[4]][3]);
    } while (next_permutation(seq + 1, seq + 5));
    printf("%d\n", ans);
    for (int i = 1; i <= tot; ++i)
        for (int j = 1; j <= 4; ++j)
            out(sch[i][j]), printf("%c", j == 4 ? '\n' : ' ');
    return 0;
}

运行结果:

84
丁 甲 乙 丙

因此按丁、甲、乙、丙顺序耗时最少,可以在 9:24 离开。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐