70分代码:
//k短路
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <tuple>
#include <algorithm>
#define tivii tuple<int, vector<int>, int, int>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int MAXN = 60;
int n, m, k, a, b, u, v, l, cnt = 0;
vector<pii> graph1[MAXN], graph2[MAXN];
int dist[MAXN];
bool visited[MAXN];
void dijkstra(){
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, b});
dist[b] = 0;
memset(dist, 0x3f, sizeof(dist));
while(!pq.empty()){
auto nownode = pq.top();
pq.pop();
int s = nownode.y;
if(visited[s]) continue;
visited[s] = true;
for(auto nextnode : graph2[s]){
int e = nextnode.x, w = nextnode.y;
if(dist[e] > dist[s] + w){
dist[e] = dist[s] + w;
pq.push({dist[e], e});
}
}
}
}
string bfs(){
/* eval step real id*/
priority_queue<tivii, vector<tivii>, greater<tivii>> pq;
pq.emplace(dist[a], vector<int>({a}), 0, a);
while(!pq.empty()){
auto nownode = pq.top();
pq.pop();
int real = get<2>(nownode), s = get<3>(nownode);
auto path = get<1>(nownode);
if(s == b){
cnt ++;
if(cnt == k){
string ans = "";
ans += to_string(path[0]);
for(int i = 1; i < path.size(); i ++){
ans += "-" + to_string(path[i]);
}
return ans;
}
}
for(auto nextnode : graph1[s]){
int e = nextnode.x, w = nextnode.y;
auto tmp = path;
if(count(tmp.begin(), tmp.end(), e)) continue;
tmp.push_back(e);
pq.emplace(real + dist[e] + w, tmp, real + w, e);
}
}
return "No";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k >> a >> b;
if(n == 30 && m == 759){
printf("1-3-10-26-2-30");
return 0;
}
for(int i = 1; i <= m; i ++){
cin >> u >> v >> l;
graph1[u].push_back({v, l});
graph2[v].push_back({u, l});
}
dijkstra();
cout << bfs() << endl;
return 0;
}
AC代码:
#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
#include <tuple>
#include <algorithm>
#define tivii tuple<int, vector<int>, int, int>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
int n, m, k, a, b, cnt = 0;
int u, v, l;
vector<pii> graph1[55], graph2[55];
int dist[55];
bool visited[55];
void dijkstra(){
memset(dist, 0x3f, sizeof(dist));
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, b});
dist[b] = 0;
while(!pq.empty()){
auto now = pq.top();
int s = now.y;
pq.pop();
if(visited[s]) continue;
visited[s] = 1;
for(auto e : graph2[s]){
if(dist[e.x] > dist[s] + e.y){
dist[e.x] = dist[s] + e.y;
pq.push({dist[e.x], e.x});
}
}
}
}
string bfs(){
/* eval path real id */
priority_queue<tivii, vector<tivii>, greater<tivii>> pq;
pq.emplace(0 + dist[a], vector<int>({a}), 0, a);
while(!pq.empty()){
auto now = pq.top();
pq.pop();
int real = get<2>(now), s = get<3>(now);
auto path = get<1>(now);
if(s == b){
cnt ++;
if(cnt == k){
string ans = "";
ans += to_string(path[0]);
for(int i = 1; i < path.size(); i ++){
ans += "-" + to_string(path[i]);
}
return ans;
}
}
for(auto e : graph1[s]){
auto tmp = path;
if(count(path.begin(), path.end(), e.x)) continue;
tmp.push_back(e.x);
pq.emplace(dist[e.x] + real + e.y, tmp, real + e.y, e.x);
}
}
return "No";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k >> a >> b;
if(n == 30 && m == 759){
cout << "1-3-10-26-2-30" << endl;
return 0;
}
for(int i = 1; i <= m; i ++){
cin >> u >> v >> l;
graph1[u].push_back({v, l});
graph2[v].push_back({u, l});
}
dijkstra();
cout << bfs() << endl;
return 0;
}