0

Here is the code.

#include<bits/stdc++.h>
using namespace std;

#define vi vector<int>
#define vii vector<pair<int, int>>
#define ii pair<int, int>
#define INF 1e6

int main()
{
    int n,m,s,t;
    cin>>n>>m>>s>>t;
    vector<vii> AL(n, vii());
    int u,v,w;

    for(int i=0;i<n;i++)
    {
        cin>>u>>v>>w;
        AL[u].emplace_back(u, w);
        AL[v].emplace_back(v, w);        
    }

    vi dist(n, INF);
    dist[s] = 0;
    set<ii> pq;

    for(int i=0;i<n;i++)
    {
        pq.insert({dist[u], u});
    }

    while(!pq.empty()) {
    
        auto [d, u] = *pq.begin();
        pq.erase(pq.begin());

        for(auto &[v, w]:AL[u]) {

            if(dist[u] + w > dist[v])
                continue;

            pq.erase(pq.find({dist[v], v}));
            dist[v] = dist[u] + w;
            pq.insert({dist[v], v});
        }
    }
    if(dist[t]!=INF)
        cout<<dist[t];
    else
        cout<<"Unreachable";
}

These are the errors:

sol.cpp: In function 'int main()':

sol.cpp:34:14: error: expected unqualified-id before '[' token

auto [d, u] = *pq.begin(); ^ sol.cpp:37:19: error: expected unqualified-id before '[' token

for(auto &[v, w]:AL[u]) { ^ sol.cpp:37:19: error: expected ';' before '[' token

sol.cpp: In lambda function:

sol.cpp:37:25: error: expected '{' before ':' token for(auto &[v, w]:AL[u]) { ^ sol.cpp: In function 'int main()':

sol.cpp:37:25: error: could not convert 'main()::<lambda()>{v, w}' from 'main()::<lambda()>' to 'bool' sol.cpp:37:25: error: expected ';' before ':' token

sol.cpp:37:25: error: expected primary-expression before ':' token

sol.cpp:37:25: error: expected ')' before ':' token

sol.cpp:37:25: error: expected primary-expression before ':' token

How do I fix these errors I get while solving the Dijkstra algorithm in c++?

cigien
  • 57,834
  • 11
  • 73
  • 112
  • 2
    You didn't write this code, did you? `auto [d, u]` is a *structured binding*, those were added in C++17. Your compiler is either outdated and doesn't support C++17, or not configured to use it. – HolyBlackCat Sep 11 '21 at 09:26
  • 2
    Recommended reading: [Why should I not #include ?](https://stackoverflow.com/q/31816095/2752075), [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/2752075). Also the first three macros should be replaced with `using`s (if not removed altogether), and the last one should be a constant. – HolyBlackCat Sep 11 '21 at 09:27
  • Also `#define vi vector`**is a very bad practice**. You should never use preprocessor for things that are better done otherwise with alias: `using vi = std::vector;`. Also, it is a very, very bad practice to use such short meaningless name as it make code hard to read and understand. Good practice is to always write complete words. Another very bas practice is to define constants using the preprocessor. There is absolutely no reason to use a macro for `INF`. **You should read a few C++ books!** – Phil1970 Sep 11 '21 at 19:55
  • Yes, I have not written this code myself. I am just using the NPTEL course programs. I appreciate your help. Thanks for your guidance. – Sambit Khandai Sep 12 '21 at 10:18

0 Answers0