此题有坑,不能单纯的把数量加起来
测试点9段错误 把q[500]改为500即可
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int station[505] = {0};
int Cmax = 0;
struct Route
{
vector<int> q[500];
int count;
Route()
{
count = 0;
}
};
void CountSetBack(vector<int>a,int&set,int&back)
{
set = 0, back = 0;
vector<int>q = a;
for (int i = 0; i < q.size();i++)
{
int sta = station[q[i]];
int tmp = sta + back - Cmax / 2;
if (tmp > 0)
{
back = tmp;
}
else
{
set -= tmp;
back = 0;
}
}
}
int main()
{
int N=0, Sp=0, M=0;
cin >> Cmax >> N >> Sp >> M;
Route *route = new Route[N+1];
int **rode = new int*[N + 1];
station[0] = 0;
rode[0] = new int[N + 1];
for (int i = 0; i <= N; i++)
rode[0][i] = 99999;
for (int i = 1; i <= N; i++)
{
rode[i] = new int[N+1];
for (int j = 0; j <= N; j++)
rode[i][j] = 99999;
cin >> station[i];
}
for (int i = 0; i < M; i++)
{
int a = 0, b = 0, c = 0;
cin >> a >> b >> c;
rode[a][b] = c;
rode[b][a] = c;
}
for (int i = 1; i <= N; i++)
{
if (rode[0][i] <99999)
{
route[i].count = 1;
route[i].q[0].push_back(i);
}
}
vector<int>Unsolve;
for (int i = 1; i <= N; i++)
Unsolve.push_back(i);
while (Unsolve.size()>0)
{
vector<int>::iterator min= Unsolve.begin();
int imin = rode[0][*min];
for (vector<int>::iterator it = Unsolve.begin(); it != Unsolve.end(); it++)
{
if ( rode[0][*it] < imin)
{
imin = rode[0][*it];
min = it;
}
}
if (*min == Sp)
break;
for (vector<int>::iterator it = Unsolve.begin(); it != Unsolve.end(); it++)
{
if (*it == *min)
continue;
int tmp = rode[0][*min] + rode[*min][*it];
if (rode[0][*it] > tmp)
{
rode[0][*it] = tmp;
route[*it].count = route[*min].count;
for (int i = 0; i < route[*min].count; i++)
{
route[*it].q[i] = route[*min].q[i];
route[*it].q[i].push_back(*it);
}
}
else if (rode[0][*it] == tmp)
{
for (int i = 0; i < route[*min].count; i++)
{
route[*it].q[i + route[*it].count] = route[*min].q[i];
route[*it].q[i + route[*it].count].push_back(*it);
}
route[*it].count += route[*min].count;
}
}
Unsolve.erase(min);
}
int set = 0, back = 0;
int mark = 0;
CountSetBack(route[Sp].q[0], set, back);
for (int i = 1; i <route[Sp].count; i++)
{
int s = 0, b = 0;
CountSetBack(route[Sp].q[i], s, b);
if (s < set || (s == set&&b < back))
{
set = s; back = b; mark = i;
}
}
cout << set << " 0";
for (int i = 0; i < route[Sp].q[mark].size(); i++)
{
cout << "->" << route[Sp].q[mark][i];
}
cout << " " << back;
return 0;
}