Pproblem: Link. I have already seen the
Solution
Maintaining two arrays of shortest distances from node 1 and n respectively using Dijkstra's Algorithm. And then go through all the edges, applying coupon on each one and saving the least cost.
and I did the required changes. But still, there are 3 test cases that are not passing.
Code
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const int sz = 1e5+1;
int n,m;
vector<bool> prd(sz);
void dijkstra(vector<vector<pair<int, int> > >& adj, vector<ll>& dist, int k)
{
for(int i=1; i<sz; ++i)
{
prd[i]=false;
}
priority_queue<pair<int, int> > q;
dist[k]=0;
q.push({0,k});
while(!q.empty())
{
int a = q.top().second;
q.pop();
if(prd[a])continue;
prd[a]=true;
for(auto u : adj[a])
{
int b = u.first, w = u.second;
if(dist[a]+w<dist[b])
{
dist[b]=w+dist[a];
q.push({-dist[b],b});
}
}
}
}
void solve()
{
cin>>n>>m;
vector<vector<pair<int, int> > > adj(sz), adj1(sz);
vector<tuple<int, int, int> > edges;
for(int i=0; i<m; ++i)
{
int a,b,w;
cin>>a>>b>>w;
adj[a].push_back({b,w});
adj1[b].push_back({a,w});
edges.push_back({a,b,w});
}
vector<ll> dist(sz, LINF), dist1(sz,LINF);
dijkstra(adj, dist,1);
dijkstra(adj1, dist1,n);
ll ans = LINF;
for(auto e : edges)
{
int a,b,w;
tie(a,b,w) = e;
ans = min(ans, dist[a]+dist1[b]+w/2);
}
cout<<ans<<endl;
}
int main()
{
solve();
}