为啥我的代码在acwing有三个测试点错误?
..
//这里填你的代码^^
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 510;
int g[N][N];
int bk[N], visit[N], d[N];
int bring = 0x3f3f3f, send = 0x3f3f3f;
vector<int> ans, path;
int m, n , c, S;
void dijkstrea()
{
// 0 是起点
memset(d , 0x3f, sizeof d);
d[0] = 0;
int s;
for(int i = 0; i <= m; i++)
{
int MIN = 0x3f;
for (int j = 0; j <= m; j++)
{
if (visit[j] == false && d[j] < MIN)
{
MIN = d[j];
s = j;
}
}
visit[s] = true;
for (int j = 0; j <= m; j++)
{
if (d[j] > d[s] + g[s][j]) d[j] = d[s] + g[s][j];
}
}
}
// 直接减,就不需要额外的if else判断了
// 通过mins存储路径前往的过程, 有最大mins 需要take数量
void dfs(int u, int have, int mins)
{
if (u)
{
// 求have
have -= c / 2 - bk[u];
mins = min (have, mins);
}
if (u == S)
{
int take = abs(mins);
int back = take + have;
if (take < send) ans = path , send = take , bring = back;
else if (take == send && back < bring) ans = path, bring = back;
return;
}
for(int i = 1; i <= m; i++)
{
if (d[i] == d[u] + g[u][i])
{
path.push_back(i);
dfs(i, have, mins);
path.pop_back();
}
}
}
int main()
{
cin >> c >> m >> S >> n;
memset(g , 0x3f, sizeof g);
for (int i = 1; i <= m; i++) cin >> bk[i];
for(int i = 0; i < n ; i++)
{
int a, b, dist;
cin >> a >> b >> dist;
g[a][b] = g[b][a] = dist;
}
dijkstrea();
//cout << d[S] << endl;
dfs(0, 0, 0);
cout << send << " 0";
for(int i = 0; i < ans.size(); i++) cout << "->" << ans[i];
cout << " " << bring;
}
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
MIN = 0x3f不能这样直接赋值, MIN会变成63