# Weekend Planning - Sprinklr hiring challenge

**URL:** <https://discuss.codechef.com/t/weekend-planning-sprinklr-hiring-challenge/21612>\
**Category:** general\
**Tags:** graphs\
**Created:** [January 12, 2019, 7:38pm UTC](https://discuss.codechef.com/t/weekend-planning-sprinklr-hiring-challenge/21612 "2019-01-12T19:38:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![temp\_typedef](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@temp\_typedef](https://discuss.codechef.com/u/temp_typedef)\
**Post date:** [January 12, 2019, 7:38pm UTC](https://discuss.codechef.com/t/weekend-planning-sprinklr-hiring-challenge/21612/1 "2019-01-12T19:38:14Z")

</div>

**This question was asked in Sprinklr hiring challenge.**

You are given a road network with N cities and M bidirectional roads. Each road has some positive amount of tax associated to it, meaning if there is a road connecting cities A and B with tax C, you need to pay C rupees to the government every time you use this road.

But you have a wildcard which can be used at most K times and when you use this wildcard while using using this road, you do not need to pay tax associated with that road. You are planning to visit one city this weekend, due to the limited budget you want to estimate minimum possible cost from your home city to every other city, so that you can choose the destination according to your budget. Your home city is a city numbered 1.

**INPUT FORMAT**

The first line of the input contains N, M, and K following M lines containing 3 integers U, V and C, meaning there is a road between cities U and V with tax C associated.

**OUTPUT FORMAT**

Print N space separated integers in a single line, ith integer indicating the minimum cost of travelling from city 1 to j

**CONSTRAINTS**

1 \<= N,M \<= 5 \* 10^5

0 \<= K \<= 15

**SAMPLE INPUT**

4 4 1

1 2 2

2 3 3

1 3 6

3 4 11

**SAMPLE OUTPUT**

0 0 0 5

---

<div class="post-metadata">

**Author:** ![chinmaynextgen](https://discuss.codechef.com/user_avatar/discuss.codechef.com/chinmaynextgen/32/18745_2.png) [@chinmaynextgen](https://discuss.codechef.com/u/chinmaynextgen)\
**Post date:** [July 16, 2020, 7:37pm UTC](https://discuss.codechef.com/t/weekend-planning-sprinklr-hiring-challenge/21612/2 "2020-07-16T19:37:38Z")

</div>

#include \<bits/stdc++.h\>  
using namespace std;  
typedef long long ll;  
int main() {

```
ll t=1;
//cin>>t;
while(t--)
{
    ll n,m,k;
    cin>>n>>m>>k;
    vector<ll>v[n+1];
    ll i,j;
    unordered_map<ll,unordered_map<ll,ll>>mp;
    for(i=0;i<n;i++)
    {
        ll x,y,c;
         cin>>x>>y>>c;
         v[x].push_back(y);
         v[y].push_back(x);
         mp[x][y]=c;
         mp[y][x]=c;
    }
    
   vector<vector<ll>>dist(k+1,vector<ll>(n+1,INT_MAX));
   for(i=0;i<=k;i++)
     dist[i][1]=0;
     
   for(i=0;i<=k;i++)
   {
       list<pair<ll,ll>>l;
       l.push_back({1,0});
       while(!l.empty())
       {
           ll x=l.front().first;
           ll lev=l.front().second;
           l.pop_front();
           ll temp=v[x].size();
           for(j=0;j<temp;j++)
           {
               ll q=v[x][j];
               
               if(i==0)
               {
                   if(dist[i][q]>dist[i][x]+mp[x][q])
                   {
                       dist[i][q]=dist[i][x]+mp[x][q];
                       l.push_back({q,lev+1});
                   }
                   continue;
               }
               
               if(dist[i][q]!=0)
               {
                   if(lev+1<=i)
                   {
                       dist[i][q]=0;
                       l.push_back({q,lev+1});
                      
                   }
                   else{
                       ll temp=min(dist[i][x]+mp[x][q],dist[i-1][x]);
                       if(dist[i][q]>temp)
                       {
                           dist[i][q]=temp;
                            l.push_back({q,lev+1});
                       }
                   }
                   
               }
              
               
           }
           
       }
   }
for(i=1;i<=n;i++)
cout<<dist[k][i]<<" ";
    
}
return 0;

```

}

PLEASE CHECK ANYONE WHETHER IT IS CORRECT OR NOT, I HAVE USED DYNAMIC PROGRAMMING HERE  
Time complexity O(K\*(N+M))  
dist[i][q] denotes minimum path sum till from 1 to q with i wildcards

---

<div class="post-metadata">

**Author:** ![sunil851](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sunil851/32/23019_2.png) [@sunil851](https://discuss.codechef.com/u/sunil851)\
**Post date:** [October 30, 2020, 9:58am UTC](https://discuss.codechef.com/t/weekend-planning-sprinklr-hiring-challenge/21612/3 "2020-10-30T09:58:42Z")

</div>

this code didn’t pass all test cases.

#include \<bits/stdc++.h\>  
using namespace std;  
typedef int ll;  
#define int long long  
const int maxn=5e5+5;  
vector\<pair\<int,int\> \>graph[maxn];  
int cost[maxn][20];  
bool vis[maxn][20];  
int n,m,k;

void djkstra() {  
priority\_queue\<pair\<int, pair\<int, int\> \> \>pq;  
pq.push(make\_pair(0, make\_pair(1, 0)));  
cost[1][0]=0;  
while (!pq.empty()) {  
pair\<int, pair\<int, int\> \>curr=pq.top();  
pq.pop();  
int curr\_n=curr.second.first;  
int curr\_s=curr.second.second;  
int cst=cost[curr\_n][curr\_s];  
for (int i=0; i\<graph[curr\_n].size(); i++) {  
if(graph[curr\_n][i].first==curr\_n) continue;  
int c\_cst=cst+graph[curr\_n][i].second;  
if(cost[graph[curr\_n][i].first][curr\_s]==-1) {  
pq.push(make\_pair(c\_cst\*(-1), make\_pair(graph[curr\_n][i].first, curr\_s)));  
cost[graph[curr\_n][i].first][curr\_s]=c\_cst;  
}  
else if(cost[graph[curr\_n][i].first][curr\_s]\>c\_cst) {  
cost[graph[curr\_n][i].first][curr\_s]=c\_cst;  
pq.push(make\_pair(c\_cst\*(-1), make\_pair(graph[curr\_n][i].first, curr\_s)));  
}  
c\_cst=cst;  
if(curr\_s==k) continue;  
if(cost[graph[curr\_n][i].first][curr\_s+1]==-1) {  
pq.push(make\_pair(c\_cst\*(-1), make\_pair(graph[curr\_n][i].first, curr\_s+1)));  
cost[graph[curr\_n][i].first][curr\_s+1]=c\_cst;  
}  
else if(cost[graph[curr\_n][i].first][curr\_s+1]\>c\_cst) {  
cost[graph[curr\_n][i].first][curr\_s+1]=c\_cst;  
pq.push(make\_pair(c\_cst\*(-1), make\_pair(graph[curr\_n][i].first, curr\_s+1)));  
}  
}  
}

```
for (int i=1; i<=n; i++) {
    long long ret=2e18;
    for (int j=0; j<=k ; j++) {
        if(cost[i][j]==-1) continue;
        ret=min(ret,cost[i][j]);
    }
    cout<<ret<<" ";
}

```

}  
ll main() {  
cin\>\>n\>\>m\>\>k;

```
for (int i=0; i<m; i++) {
    int a,b,c;
    cin>>a>>b>>c;
    
    graph[a].push_back(make_pair(b,c));
    graph[b].push_back(make_pair(a,c));
}
memset(cost,-1,sizeof(cost));
djkstra();

```

}

this one works
