|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <bits/stdc++.h>
using namespace std;
int a[5][6] = {{0, 0, 0, 0, 0, 0},
{0, 9, 2, 4, 15, 9},
{0, 6, 5, 12, 4, 2},
{0, 11, 7, 13, 4, 17},
{0, 19, 11, 15, 8, 9}};
bool v[10];
map<int, string> mp;
struct sta {
int sum, two;
pair<int, int> c;
vector<pair<int, int> > one;
};
vector<sta> sch;
void dfs(int u, sta now) {
if (u == 5) {
if (!sch.empty() && sch.back().sum > now.sum)sch.clear();
if (sch.empty() || sch.back().sum == now.sum)
sch.push_back(now);
return;
}
if (!sch.empty() && sch.back().sum < now.sum)return;
vector<int> c;
for (int i = 1; i <= 5; i++)if (!v[i])c.push_back(i);
if (u == 4 && c.size() == 2) {
v[c[0]] = true, v[c[1]] = true;
sta nex = now;
nex.two = u;
nex.sum += a[u][c[0]] + a[u][c[1]];
nex.c = make_pair(c[0], c[1]);
dfs(u + 1, nex);
v[c[0]] = false, v[c[1]] = false;
return;
}
for (int i = 0; i < c.size(); i++) {
sta nex = now;
nex.sum += a[u][c[i]];
nex.one.emplace_back(u, c[i]);
v[c[i]] = true;
dfs(u + 1, nex);
v[c[i]] = false;
}
for (int i = 0; i < c.size(); i++)
for (int j = i + 1; j < c.size(); j++) {
v[c[i]] = true, v[c[j]] = true;
sta nex = now;
nex.two = u;
nex.sum += a[u][c[i]] + a[u][c[j]];
nex.c = make_pair(c[i], c[j]);
dfs(u + 1, nex);
v[c[i]] = false, v[c[j]] = false;
}
}
int main() {
sta now = {0, 0, {0, 0}, {}};
dfs(1, now);
mp[1] = "甲", mp[2] = "乙", mp[3] = "丙", mp[4] = "丁";
printf("总最小花费时间为:%d\n", sch.back().sum);
for (int i = 0; i < sch.size(); i++) {
cout << "方案" << i + 1 << ":" << endl;
cout << mp[sch[i].two] << "完成任务:" << (char) ('A' + sch[i].c.first - 1) << "和"
<< (char) ('A' + sch[i].c.second - 1) << endl;
for (int j = 0; j < sch[i].one.size(); j++) {
cout << mp[sch[i].one[j].first] << "完成任务:" << (char) ('A' + sch[i].one[j].second - 1) << endl;
}
puts("");
}
return 0;
}
|
-
|