就有些怪,在acwing里一道类似的题是过的,但是在天梯赛里,最后一种情况就过不了,还有一点很怪的就是,我用这种字符串记录路径的方式在pta上会报g::_M_construct null not valid,查了一次也没看懂,最后直接把它跳过去输出后面的才过的样例
#include <bits/stdc++.h>
using namespace std;
int n, m, s, d;
int que[505], lujing[505];
bool jud[505];
int before[505];
vector<pair<int, int>> lu[505];
struct item
{
int bianhao;
int val;
int num;
int numpue;
string luu;
};
bool operator<(item a, item b)
{
if (a.val > b.val)
return true;
else if (a.val == b.val)
return a.numpue < b.numpue;
else
return false;
}
int main()
{
cin >> n >> m >> s >> d;
for (int i = 0; i < n; i++)
cin >> que[i];
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
lu[a].push_back({b, c});
lu[b].push_back({a, c});
}
item beginn = {s, 0, 0, que[s], "" + (char)('0' + s)};
before[s] = -1;
priority_queue<item> q;
q.push(beginn);
while (!q.empty())
{
item t = q.top();
if (t.bianhao == d)
break;
q.pop();
jud[t.bianhao] = 1;
for (int i = 0; i < lu[t.bianhao].size(); i++)
{
if (!jud[lu[t.bianhao][i].first])
{
q.push({lu[t.bianhao][i].first, t.val + lu[t.bianhao][i].second, t.num + 1, t.numpue + que[lu[t.bianhao][i].first], t.luu + (char)('0' + t.bianhao)});
}
}
}
item ans = q.top();
int num = 1;
q.pop();
while (!q.empty())
{
if (q.top().bianhao == d && q.top().val == ans.val)
num++;
else if (q.top().bianhao == d)
break;
q.pop();
}
cout << num << ' ' << ans.numpue << '\n';
for (int i = 30; i < ans.luu.size(); i++)
{
cout << ans.luu[i] << ' ';
}
cout << d;
}