1 / 3
Nov 2021

I solved this problem using Dijkstra and adjacency list to store graph… It is simple and straight forward however, I always get WA. Please Help.

#include <bits/stdc++.h>`

using namespace std;

#define fatak                         \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

#define newl cout << "\n"
#define prDouble(x) cout << fixed << setprecision(10) << x
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second

// Shortcuts for "common" data types
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using psi = pair<string, int>;

using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
using vpsi = vector<psi>;

using si = set<int>;
using msi = map<string, int>;

//memset(dist, MEMSET_INF, sizeof dist); // useful to initialize shortest path distances
//memset(dp_memo, -1, sizeof dp_memo); // useful to initialize DP memoization table
//memset(arr, 0, sizeof arr); // useful to clear array of integers
//priority_queue<int> q;
// priority_queue<int,vector<int>,greater<int>> q;
const int mod = 1'000'000'007;

int a[N];
ll binexp(ll a, ll b);//binary exponentiation
bool comp(pii &a, pii &b); //comparator
void ipgraph(int n, int m);// input graph edgelist
void dfs(int u);

/* _______________________________________________________________________________ */

void gg() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}
const int N = 101;
vector<vpii> g(N); bool visited[N];
int n,e,T,m;
int depth[101];
void solve(int u)
{
    memset(depth,INT_MAX,sizeof depth);
    memset(visited,0,sizeof visited);
    depth[u]=0;
    priority_queue<pair<int,int>> q;
    q.push({0,u});
    while(!q.empty()){
        pii ut = q.top();
        q.pop();
        int u = ut.s, d = -ut.f;
        if (visited[u]) continue;
        visited[u] = true;
        for(auto vt: g[u]){
            int v=vt.f;
            int t = vt.s;
            if(depth[u]+t<depth[v]){
                depth[v]=depth[u]+t;
                q.push({-depth[v],v});
            }
        }
    }
    return;
}

int main()
{
    fatak;
    int t;
    // cin >> t;
    t=1;
    while (t--)
    {
        
        cin >>n>>e>>T>>m;
        // cout << n<<e<<T<<m<<"\n";
        for(int i=0;i<m;i++){
            int x,y,w;
            cin >>x>>y>>w;
            g[y].push_back({x,w});
        }
        int ans{};
        solve(e);
        for(int i=1;(i<=n);i++){
            if(i==e) continue;
            if(depth[i]<=T) ans++;
        }
        cout << ans << "\n";
    }
    return 0;
}
  • created

    Nov '21
  • last reply

    Nov '21
  • 2

    replies

  • 554

    views

  • 2

    users

I see you’ve not got accepted, so assume you found what the problem was?

yes I got the issue, using memset, array can be initialized by only a 1 byte value, like -1,0,true,false etc… So, I changed it to following and it worked.

 for(int i=0;i<=n;i++){
     depth[i]=INT_MAX;
 }

Also if(i==e) continue; is not needed, that was my error in understanding the output.