Skip to content

Instantly share code, notes, and snippets.

@tamimcsedu19
Created August 6, 2014 17:25
Show Gist options
  • Save tamimcsedu19/a719c3a5e34246abc9bc to your computer and use it in GitHub Desktop.
Save tamimcsedu19/a719c3a5e34246abc9bc to your computer and use it in GitHub Desktop.
#include <iostream>
#include <bits/stdc++.h>
#define maxn 1005
#define inf 1000009
using namespace std;
typedef pair<int,int> ii;
typedef pair<long long int,int> li;
vector<ii> G[maxn];
vector<int> d[maxn];
int n,k;
int viscnt[maxn];
vector<int> res;
int dijkstra(int s,int d)
{
res.clear();
priority_queue<li,vector<li>,greater<li> > pq;
pq.push({0,s});
while(!pq.empty())
{
int u = pq.top().second,scoreu = pq.top().first;
pq.pop();
if(viscnt[u]>=k+1)
continue;
++viscnt[u];
if(u == d)
res.push_back(scoreu);
for(int i=0;i<G[u].size();++i)
{
int scorenow = scoreu+G[u][i].second,v=G[u][i].first;
pq.push({scorenow,v});
}
}
sort(res.begin(),res.end());
if(res.size() < k)
return -1;
return res[k-1];
}
int main()
{
//freopen("in.txt","r",stdin);
int m;
while(cin>>n>>m && (n||m))
{
for(int i=1;i<=n;++i)
{
G[i].clear();
viscnt[i] =0;
}
int s,d;
cin>>s>>d>>k;
while(m--)
{
int u,v,w;
cin>>u>>v>>w;
G[u].push_back({v,w});
}
cout<<dijkstra(s,d)<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment