总结:
该代码有两个测试点没过,因为没有考虑0->2>3>1(2,3,1权值分别为:54,0,78),最大容量为100,这种情况需要send46辆,剩18,而不是send18,剩0
1.这道题考察了dfs,最短路径问题,加上了送出的车最少和拿回的车最少,主要是自己之前理解出错,导致第一次没通过,第二次的代码是分开做的便于理解
代码1;
#include<iostream>
#include<vector>
using namespace std;
int G[600][600]; //边权
int weight[600]; //点权
int vi[600];
int minlen = 999999;
int totalen = 0;
int cmax, n, sp, m;
int totalcollect = 0;
int mincollect = 9999999;
vector<vector<int> > minroad;
int que = 0; int sheng = 0; int qu = 0; int sg = 0;
void dfs(int index,vector<int> road)
{
if (vi[index])return;
if (index == sp)
{
int s1 = totalcollect;
if (s1<0){ s1 = -s1; sheng = 0; }
else if (s1 == 0){s1 = 0; sheng = 0; }
else if (s1>0){
sheng = s1; s1 = 0;
}
if (totalen < minlen){ minlen = totalen; mincollect = s1; sg = sheng; minroad.push_back(road); }
else if (totalen == minlen&&sheng<sg&&s1==mincollect){ mincollect = s1; sg = sheng; minroad.push_back(road); }
else if (totalen == minlen&&s1<mincollect){ mincollect = s1; sg = sheng; minroad.push_back(road); }
return;
}
vi[index] = 1;
for (int i = 1; i <= n; i++)
{
if ((!vi[i]) && (G[index][i]))
{
totalen += G[index][i];
totalcollect += weight[i] – cmax / 2;
road.push_back(i);
dfs(i,road);
totalen -= G[index][i];
totalcollect -= weight[i] – cmax / 2;
road.pop_back();vi[i] = 0;
}
}
}
int main()
{
cin >> cmax >> n >> sp >> m;
for (int i = 1; i <= n; i++)
{
int wt; scanf(“%d”,&wt);
weight[i] = wt;
}
for (int i = 0; i < m; i++)
{
int a, b, ti;
scanf(“%d%d%d”,&a,&b,&ti);
G[a][b] = ti; G[b][a] = ti;
}
vector<int> road;
road.push_back(0);
dfs(0,road);
cout << mincollect<<” “;
int phg = minroad.size() – 1;
for (int i = 0; i < minroad[phg].size(); i++)
{
printf(“%d”, minroad[phg][i]);
if (i != minroad[phg].size() – 1)printf(“->”);
}
cout << ” “<<sg;
return 0;
}
AC代码:先找出最短路径集合,然后求最小send和back
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int G[600][600]; //边权
int weight[600]; //点权
int vi[600];
int minlen = 999999;
int totalen = 0;
int cmax, n, sp, m;
int totalneed = 0;
int totalsheng = 0;
int total = 0; int sum = 0;
int maxcost = 99999;
vector<vector<int> > minroad;
int sg = 9999;
int g = 0;
map<int, int> sps;
void dfs(int index, vector<int> road)
{
if (vi[index])return;
if (index == sp)
{
if (totalen <= minlen){ minlen = totalen; sps[g++] = minlen; minroad.push_back(road); }
return;
}
vi[index] = 1;
for (int i = 1; i <= n; i++)
{
if ((!vi[i]) && (G[index][i]))
{
totalen += G[index][i];
road.push_back(i);
dfs(i, road);
totalen -= G[index][i];
road.pop_back(); vi[i] = 0;
}
}
}
int main()
{
cin >> cmax >> n >> sp >> m;
for (int i = 1; i <= n; i++)
{
int wt; scanf(“%d”, &wt);
weight[i] = wt;
}
for (int i = 0; i < m; i++)
{
int a, b, ti;
scanf(“%d%d%d”, &a, &b, &ti);
G[a][b] = ti; G[b][a] = ti;
}
vector<int> road;
road.push_back(0);
dfs(0, road);
int index = 0;
for (int i = 0; i < minroad.size(); i++)
{
if (sps[i] != minlen)continue;
totalneed = 0;
totalsheng = 0; total = 0; sum = 0;
for (int j = 1; j < minroad[i].size(); j++)
{
int x = minroad[i][j];
if (weight[minroad[i][j]] + total – cmax / 2>0)
{
totalsheng = weight[minroad[i][j]] + total – cmax / 2; totalneed = 0;
total = totalsheng;
}
else{ totalneed = -(weight[minroad[i][j]] + totalsheng – cmax / 2); sum += totalneed; total = 0; totalsheng = 0; }
}
if (sum<maxcost){ maxcost = sum; sg = totalsheng; index = i; }
else if (sum == maxcost&&totalsheng < sg){ maxcost = sum; sg = totalsheng; index = i; }
}
cout << maxcost << ” “;
for (int i = 0; i < minroad[index].size(); i++)
{
printf(“%d”, minroad[index][i]);
if (i != minroad[index].size() – 1)printf(“->”);
}
cout << ” ” << sg;
return 0;
}