Pagini recente » Cod sursa (job #2502905) | Cod sursa (job #975556) | Cod sursa (job #1732405) | Cod sursa (job #2708218) | Cod sursa (job #2211363)
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
using namespace std;
ifstream f ("pitici.in");
ofstream g ("pitici.out");
typedef pair <int, int> pii;
const int NMAX = 1024;
const int INF = 1 << 30;
int n, m, k, u, v, cost;
int dp[NMAX][NMAX], numPath[NMAX];
bool vis[NMAX];
vector <pii> adj[NMAX];
void dfs (int node) {
vis[node] = true;
for (int i = 0; i < k; ++i) {
dp[node][i] = -INF;
}
if (node == n - 1) {
dp[node][0] = 0;
return;
}
priority_queue < pair <int , pair <int, int> > > q;
while (!q.empty())
q.pop();
for (auto &i : adj[node]) {
if (!vis[i.first]) {
dfs(i.first);
}
numPath[i.first] = 0;
q.push({dp[i.first][0] - i.second, {i.first, i.second}});
}
for (int i = 0; i < k && !q.empty(); ++i) {
int dist = q.top().first;
int adjNode = q.top().second.first;
int cost = q.top().second.second;
q.pop();
dp[node][i] = dist;
++numPath[adjNode];
q.push({dp[adjNode][numPath[adjNode]] - cost, {adjNode, cost}});
}
}
int main() {
f >> n >> m >> k;
for (int i = 0; i < m; ++i) {
f >> v >> u >> cost;
--v; --u;
adj[v].pb({u, cost});
}
dfs(0);
for (int i = 0; i < k; ++i) {
g << -dp[0][i] << ' ';
}
f.close();
g.close();
return 0;
}