跳到正文

科技·工程

short-path.h (未完成)

Fyq_20约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年6月4日
收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟

版本

  1. v1当前
C++
#include<queue>
#include"vector.h"
namespace std
{
    class path_with_value
    {
    public:
        int to,val;
        friend bool operator<(path_with_value t,path_with_value other)
        {
            return t.val>other.val;
        }
    };
    class short_path
    {
    private:
        vector<long long> dist;
        bool double_side,negative_value;
        int last_start;
        int n,m;
        path_with_value path(int to,int val)
        {
            return {to,val};
        }
    public:
        unordered_map<int,vector<path_with_value> > paths;
        short_path(bool is_double_side,bool have_negative_value)
        {
            last_start=-1;
            double_side=is_double_side;
            negative_value=have_negative_value;
        }
        void set_size(int num_point,int num_path)
        {
            n=num_point;
            m=num_path;
        }
        void insert(int from,int to,int val)
        {
            paths[from].push_back(path(to,val));
            if(double_side)paths[to].push_back(path(from,val));
        }
        int shortest_path(int from,int to,bool (*cmp)(int,int))
        {
            if(from==last_start)return dist[to];
            if(to==last_start)return dist[from];
            dist.set_value(0x3f3f3f3f,1,n+5);
            dist[from]=0;
            if(m*log2(m)<n*n)
            {
                priority_queue<path_with_value> q;
                q.push(path(from,0));
                while(!q.empty())
                {
                    int cur=q.top().to;
                    q.pop();
                    for(int i=0;i<paths[cur].size();i++)
                    {
                        int to=paths[cur][i].to;
                        if(dist[cur]+paths[cur][i].val<dist[to])
                        {
                            dist[to]=dist[cur]+paths[cur][i].val;
                            q.push(path(to,dist[to]));
                        }
                    }
                }
                return dist[to];
            }
            else
            {

            }
        }
        int shortest_path(int from,int to)
        {

        }
    };
}
原文