Algorithm 当我读到Djiktra';s algo对负边失败,但我实现了相同的概念,我的代码能正常工作吗?有虫子吗?

Algorithm 当我读到Djiktra';s algo对负边失败,但我实现了相同的概念,我的代码能正常工作吗?有虫子吗?,algorithm,graph-algorithm,shortest-path,Algorithm,Graph Algorithm,Shortest Path,代码也适用于负边缘,我使用了优先级队列 请检查它,让我知道它有什么问题,为什么这是工作,甚至负边缘。 约束:边的长度应小于10000 我做错什么了吗? 当我读到Djiktra的算法在负边缘时失败了,但我实现了相同的概念,我的代码正常工作了吗?有虫子吗 #include<iostream> #include<queue> using namespace std; struct reach { int n; i

代码也适用于负边缘,我使用了优先级队列
请检查它,让我知道它有什么问题,为什么这是工作,甚至负边缘。 约束:边的长度应小于10000
我做错什么了吗? 当我读到Djiktra的算法在负边缘时失败了,但我实现了相同的概念,我的代码正常工作了吗?有虫子吗

#include<iostream>
    #include<queue>
    using namespace std;

    struct reach
    {
        int n;
        int weight;
    };

    struct cmp
    {
        bool operator()(reach a, reach b)
        {
            return a.weight>b.weight;
        }
    };

    class sp
    {
        int *dist;
        int n;
        int **graph;
        int src;
        public:
            sp(int y)
            {
                n=y;
                src=1;
                dist=new int[n+1];
                graph=new int*[n+1];
                for(int i=0;i<=n;i++)
                {
                    graph[i]=new int[n+1];
                }
                for(int i=2;i<=n;i++)
                {
                    dist[i]=10000;
                }
            //  cout<<INT_MAX<<endl;
                dist[src]=0;
                for(int i=1;i<=n;i++)
                {
                    for(int j=1;j<=n;j++)
                    {
                        graph[i][j]=10000;
                    }
                }
                graph[1][1]=0;
            }
            void read()
            {
                int a;
                cout<<"enter number of edges"<<endl;
                cin>>a;
                cout<<"now enter the two vertices which has an edge followed by the weight of the edge"<<endl;
                while(a--)
                    {//cout<<"location: "<<i<<" : "<<j<<endl;
                    int as, ad,val;
                    cin>>as>>ad>>val;


                            graph[as][ad]=val;

                    }

            }
            void finder()
            {cout<<"enetered"<<endl;
                priority_queue<reach, vector<reach>, cmp> q;
                for(int i=1;i<=n;i++)
                {
                    if(dist[src]+graph[src][i]<dist[i])
                    {
                        reach temp;
                        temp.n=i;
                        cout<<i<<endl;
                        temp.weight=graph[src][i];
                        q.push(temp);
                        dist[i]=graph[src][i]+dist[src];
                    }
                }
                    while(q.empty()!=1)
                    {
                        reach now =q.top();
                        //cout<<" we have here: "<<now.n<<endl;
                        q.pop();
                        for(int i=1;i<=n;i++)
                        {
                            if((dist[now.n] + graph[now.n][i])<dist[i])
                            {
                                dist[i]=dist[now.n]+graph[now.n][i];
                                cout<<"it is: "<<i<<" : "<<dist[i]<<endl;

                                reach temp;
                                temp.n=i;
                                //cout<<temp.n<<endl;
                                temp.weight=graph[now.n][i];
                                q.push(temp);
                            }
                        }


                    }
                }

            void print()
            {
                for(int i=1;i<=n;i++)
                {
                    cout<<"we have: "<<dist[i]<<" at "<<i;
                    cout<<endl;
                }

                cout<<endl;
            }

        };


        int main()
        {cout<<"enter no. of vertices"<<endl;
            int n;
            cin>>n;
            sp sp(n);
            sp.read();
            sp.finder();
            sp.print();



    }
#包括
#包括
使用名称空间std;
结构延伸
{
int n;
整数权重;
};
结构cmp
{
bool运算符()(到达a,到达b)
{
返回a.weight>b.weight;
}
};
类sp
{
int*dist;
int n;
int**图;
int-src;
公众:
警司(国际)
{
n=y;
src=1;
dist=新整数[n+1];
图=新整数*[n+1];
对于(int i=0;i请考虑以下示例:

无向图(6v,8e)

边缘(v1-v2(重量)):

  • 1-2(2)
  • 2-3(1)
  • 1-3(-2)
  • 1-6(-2)
  • 3-6(-3)
  • 5-6(1)
  • 4-5(2)
  • 2-5(1)
现在,让源为
1
,目标为
4
。从源到源(1-3-6-1)有一个循环,它的权重为(-7)。因此,让我们列出几个从源到目标的路径,以及权重:

  • 1-6-5-4(1)
  • 1-3-6-5-4(-2)
  • 1-3-6-1-3-6-5-4(-9)
  • 1-3-6-1-3-6-1-3-6-5-4(-16)
等等。 那么,哪条路径最短?因为它是模糊的,所以算法不起作用。现在,你可以说,如果访问了一个节点,你不会更新它。在这种情况下,你不会得到正确的答案。可能有一些情况下,尽管有负边,Algoo给出了正确的结果,但Dijkstra不是这样工作的


理解Dijkstra的一个非常简单的方法是,它在图上从源到目的地执行BFS,并且在每一步它都更新访问的节点。因此,如果有一个节点
n
具有成本
c
,并且在BFS中有几个级别,它的成本就变成
k(Dijkstra算法在负权重下失败的最简单图具有邻接矩阵

0 1  2
1 0 -3
2 -3 0

并查找从顶点
0
到顶点
1
的路由。从优先级队列中出来的第一个顶点是距离
1
的顶点
1
,因此这就是返回的路由。但是,有一条总重量
-1
的路由通过仍然在优先级队列中的顶点,其重量
2
可能会发现它在某些情况下有效,但通常不起作用。