Pagini recente » Cod sursa (job #252509) | Cod sursa (job #2488075) | Cod sursa (job #738871) | Cod sursa (job #1654490) | Cod sursa (job #2771503)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#define maxi 1050
#define INF 0x3f3f3f3f
using namespace std;
ifstream f("pitici.in",ios::in);
ofstream g("pitici.out",ios::out);
vector<pair<short,short>> G[maxi],GT[maxi];
vector<short> F;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> heap;
map<short,short> T;
unordered_map<short,set<int>> M;
unordered_map<short,multiset<int>> M2;
unordered_map<short,multiset<int>> sol;
int D[maxi],n,m,k,cnt;
bool viz[maxi];
void READ()
{
int a,b,c;
f>>n>>m>>k;
for(int i=1;i<=m;i++)
{
f>>a>>b>>c;
G[a].push_back(make_pair(b,c));
GT[b].push_back(make_pair(a,c));
}
f.close();
return;
}
void INIT()
{
for(int i=1;i<=n;i++)
viz[i]=false,D[i]=INF;
return;
}
void DFS(int x)
{
viz[x]=true;
for(auto a:G[x])
{
if(!viz[a.first])
DFS(a.first);
}
F.push_back(x);
}
void DIJKSTRA(int x)
{
INIT();
D[x]=0;
heap.push(make_pair(D[x],x));
while(!heap.empty())
{
int sursa=heap.top().second;
viz[sursa]=true;
heap.pop();
for(auto a:G[sursa])
{
int vecin=a.first;
int cost=a.second;
if(!viz[vecin])
{
if(D[sursa]+cost<D[vecin])
{
D[vecin]=D[sursa]+cost;
heap.push(make_pair(D[vecin],vecin));
}
}
}
}
return;
}
int main()
{
READ();
for(int i=1;i<=n;i++)
if(!viz[i])
DFS(i);
for(int i=F.size()-1;i>=0;i--)
{
cnt++;
T.insert(pair<short,short>(cnt,F[i]));
}
DIJKSTRA(1);
for(int i=1;i<=n;i++)
M[i].insert(D[i]);
for(map<short,short>::iterator I=T.begin();I!=T.end();I++)
{
int vertex=I->second;
for(auto a:GT[vertex])
{
int vecin=a.first;
int cost=a.second;
for(auto b:M[vecin])
{
int nou=b+cost;
if(M[vertex].count(nou)==0)
M[vertex].insert(nou);
else M2[vertex].insert(nou);
}
/*for(auto b:M2[vecin])
{
int nou=b+cost;
if(M2[vertex].count(nou)==0)
M[vertex].insert(nou);
else M2[vertex].insert(nou);
}*/
}
}
int cnt=0;
for(int i=1;i<=n;i++)
{
for(auto a:M[i])
sol[i].insert(a);
}
for(int i=1;i<=n;i++)
{
cnt=0;
for(auto a:M2[i])
{
cnt++;
if(cnt>1)
sol[i].insert(a);
}
}
int cntt=0;
for(auto a:sol[n])
{
cntt++;
if(cntt<=k)
g<<a<<" ";
}
return 0;
}