Pagini recente » Cod sursa (job #2575276) | Cod sursa (job #2773442) | Cod sursa (job #434296) | Cod sursa (job #26560) | Cod sursa (job #2608330)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <bitset>
using namespace std;
class InParser
{
#pragma warning(disable:4996)
private:
FILE* fin;
char* buff;
int sp;
char read()
{
++sp;
if (sp == 4096)
{
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume)
{
sp = 4095;
buff = new char[4096];
fin = fopen(nume, "r");
}
InParser& operator >> (int& n)
{
char c;
while (!isdigit(c = read()));
n = c - '0';
while (isdigit(c = read()))
n = n * 10 + c - '0';
return *this;
}
};
InParser fin("pitici.in");
ofstream fout("pitici.out");
const int NMAX = 1020;
int n, m, k, x, y, c, dp[NMAX][NMAX];
typedef pair <int, int> p;
vector <p> g[NMAX];
vector <int> pit;
queue <int> st_top;
bitset <NMAX> viz;
void DFS(int nod)
{
viz[nod] = 1;
for (auto i : g[nod])
if (viz[i.first] == 0)
DFS(i.first);
st_top.push(nod);
}
void solve(int x)
{
priority_queue <int, vector <int>, greater <int> > q;
for (auto i : g[x])
{
bool ok = false;
for (int j = 1; j <= k; ++j)
{
if (dp[i.first][j])
{
q.push(dp[i.first][j] + i.second);
ok = true;
}
}
if (ok == false)
q.push(i.second);
}
for (int i = 1; i <= k; ++i)
{
if (q.empty())
break;
dp[x][i] = q.top();
q.pop();
}
}
int main()
{
fin >> n >> m >> k;
for (int i = 1; i <= m; ++i)
{
fin >> x >> y >> c;
g[x].push_back({ y, c });
}
DFS(1);
while (!st_top.empty())
{
solve(st_top.front());
st_top.pop();
}
for (int i = 1; i <= k; ++i)
fout << dp[1][i] << " ";
fout.close();
return 0;
}