#include
#include <cstdio>
using namespace std;
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MAXSIZE 10
#define INF 999999999
struct Job_Info
{
int t1, t2;
}job[MAXSIZE];
int n;
//作业数量
int solu[MAXSIZE];
//调度次序
int optimalValue;
//最优值
int optimalSolu[MAXSIZE];
//最优解
void Swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
//time1,time2分别为机器1和机器2的完成处理时间
//totalTime为已完成作业的时间和
void Backtrack(int k, int time1, int time2, int totalTime)
{
int i;
if (k >= n)
{
for (i = 0; i < n; i++)
{
optimalSolu[i] = solu[i];
}
optimalValue = totalTime;
}
else
{
for (i = k; i < n; i++)
{
//计算完成第i个任务后,两机器的时间
int nextTime1 = time1 + job[solu[i]].t1;
int nextTime2 = MAX(nextTime1, time2) + job[solu[i]].t2;
//已完成作业的时间和是否小于最优值
if (totalTime + nextTime2 < optimalValue)
{
Swap(solu[k], solu[i]);
Backtrack(k+1, nextTime1, nextTime2, totalTime+nextTime2);
Swap(solu[k], solu[i]);
}
}
}
}
int main(void)
{
int i;
for (i = 0; i < MAXSIZE; i++)
{
solu[i] = i;
}
while (scanf(“%d”, &n) != EOF)
{
for (i = 0; i < n; i++)
{
scanf(“%d%d”, &job[i].t1, &job[i].t2);
}
optimalValue = INF;
Backtrack(0, 0, 0, 0);
printf(“optimal value: %d\n”, optimalValue);
printf(“optimal solution: “);
for (i = 0; i < n-1; i++)
{
printf(“%d “, optimalSolu[i]+1);
}
printf(“%d\n”, optimalSolu[i]+1);
}
return 0;
}