游览车问题
#include
#include
using namespace std;
#define N 10000
vector node[N];//存储结点连通间的联系
vector value[N];//结点与结点之间的连通成本
vector time[N];//结点到结点的游览时间
int vist[N] = { 0 };
int pre[N] = { 0 }, pos[N];
int sum_cost = 0, sum_time = 0;
int final_cost = 0, max_time = -1;
int n, num = 0;
int maxpes = 0;
void dfs(int s)
{
if (s == n)
{
if (sum_time > max_time)//存储当前最长观光时间
{
max_time = sum_time;
final_cost = sum_cost;
for (int j = 0; j < num; j++)
{
pre[j] = pos[j];
}
maxpes = num;
}
return;
}
for (int i = 0; i < node[s].size(); i++)//遍历各个结点,0结束输入
{
if (vist[node[s][i]] != 1)
{
sum_cost += value[s][i];
sum_time += time[s][i];
vist[node[s][i]] = 1;
pos[num] = node[s][i];
num++;
dfs(node[s][i]);
num--;
pos[num] = 0;
sum_cost -= value[s][i];
sum_time -= time[s][i];
vist[node[s][i]] = 0;
}
}
}
int main()
{
cin >> n;
int u, v, val, tim;
cin >> u >> v >> val >> tim;
while (u != 0)//建图
{
node[u].push_back(v);
node[v].push_back(u);
value[u].push_back(val);
value[v].push_back(val);
time[u].push_back(tim);
time[v].push_back(tim);
cin >> u >> v >> val >> tim;
}
vist[1] = 1;
dfs(1);
cout << max_time << " " << final_cost << endl;
cout << " 1 ";
for (int j = 0; j < maxpes; j++)
cout << pre[j] << " ";
cout << endl;
return 0;
}