https://cses.fi/problemset/task/1195
Here is my code
// Online C++ compiler to run C++ program online
#include <climits>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
/*
10 12
1 2 4
5 7 1
4 6 4
7 9 3
9 10 1
7 8 5
1 3 1
6 7 4
4 5 1
3 4 2
8 10 5
2 4 3
*/
void print(vector<long long>&arr){
cout<<"array size is " << arr.size();
for(int i=0;i<arr.size();i++){
cout<<i<<" value is "<<arr[i]<<" ";
}
return;
}
void djikstra(vector<vector<pair<int,int>>>&graph,vector<long long>&dist,int src){
int n=graph.size()-1;
using T =pair<long long,int>;
priority_queue<T , vector<T> ,greater<T>>pq;
vector<bool>vis(n+1,false);
pq.push({0ll+0,src});
vis[src]=true;
dist[src]=0ll+0;
while(pq.size()>0){
pair<long long,int>front=pq.top();
pq.pop();
int node=front.second;
// cout<<"Node comes "<<node<<" ";
long long currDis=front.first;
for(pair<int,int>nbr:graph[node]){
if(vis[nbr.second]==false){
// cout<<" nbr node "<<nbr.second <<" is unvisited updating ist distance";
vis[nbr.second]=true;
dist[nbr.second]=0ll+currDis+nbr.first;
pq.push({currDis+nbr.first,nbr.second});
// cout<<pq.size();
}else{
if(dist[nbr.second]>currDis+nbr.first){
// cout<<" minimising the distance of "<<nbr.second ;
dist[nbr.second]=currDis+nbr.first;
}
}
}
// cout<<
}
return;
}
void findOptimalCost(vector<vector<pair<int,int>>>&graph,vector<long long>&dist1,vector<long long>&dist2){
int n = graph.size()-1;
long long minCost=LLONG_MAX;
pair<int,int>path;
for(int a=1;a<=n;a++){
// if not reachable continue to next vertex
if(dist1[a]==LLONG_MAX)continue;
//reachable try to half each edge of v
for(pair<int,int>nbr:graph[a]){
int b=nbr.second,cost=nbr.first;
if(dist2[b]==LLONG_MAX)continue;
//now 1-->A A-->B B-->N all reachabel now half the cost and update the minCost
long long d1=dist1[a],d2=dist2[b],halved=cost/2;
if( d1+halved+ d2 <minCost ){
// cout<<"Chnaging on a= "<<a<<" - b ="<<b<<" "<<endl;
minCost=d1+halved+d2;
path={a,b};
}
}
}
cout<<minCost;
//<<path.first<<" "<<path.second;
}
void solve(){
// int n,m;cin>>n>>m;
// cout<<"s1"; int n=3,m=4;
int n,m;cin>>n>>m;
vector<long long>dist1(n+1,LLONG_MAX),dist2(n+1,LLONG_MAX);
// cout<<"n m done";
vector<vector<pair<int,int>>>graph(n+1,vector<pair<int,int>>()),rev_graph(n+1,vector<pair<int,int>>());
for(int i=0;i<m;i++){
int u,v,cost;
cin>>u>>v>>cost;
// cout<<"taking input";
graph[u].push_back({cost,v});
rev_graph[v].push_back({cost,u});
}
// cout<<"done input";
djikstra(graph,dist1,1);
// print(dist1);
// cout<<endl;
djikstra(rev_graph,dist2,n);
// print(dist2);
// cout<<endl;
findOptimalCost(graph,dist1,dist2);
}
int main() {
// Write C++ code here
solve();
return 0;
}
Not all testcases getting passed where i am behin din this code ??