• A*寻路算法


    //http://poj.org/problem?id=2449
    
    
    
    #include <iostream>
    #include <utility>
    #include <vector>
    #include <queue>
    using namespace std;
    
    typedef pair<int, int> pii;//距离,顶点
    struct Arc
    {
    	int vex;
    	int weight;
    };
    
    const int MAX_VEX_NUM = 1010;
    const int MAX = 1<<20;
    
    vector<Arc> Adjlist[MAX_VEX_NUM];
    vector<Arc> AdjlistRev[MAX_VEX_NUM];//反向图,求h(n)
    int h[MAX_VEX_NUM];
    
    int S,T,K;
    
    void Init()
    {
    	int N,M;
    	int A,B,T;
    	cin>>N>>M;
    	int i;
    	for(i = 0; i < N; i++)
    	{
    		Adjlist[i].clear();
    		AdjlistRev[i].clear();
    	}
    	Arc arc;
    	for(i = 0; i < M; i++)
    	{
    		cin>>A>>B>>T;
    		arc.vex = B;
    		arc.weight = T;
    		Adjlist[A].push_back(arc);
    		arc.vex = A;
    		AdjlistRev[B].push_back(arc);
    	}
    	cin>>S>>::T>>K;
    }
    
    //计算h[n]
    void Dijkstra(int u)
    {
    	priority_queue<pii, vector<pii>, greater<pii> > pq_dij;
    	bool traversed[MAX_VEX_NUM];
    	memset(traversed, false, MAX_VEX_NUM * sizeof(bool));
    	for(int i = 0; i < MAX_VEX_NUM; i++)
    	{
    		h[i] = MAX;
    	}
    	
    	h[u] = 0;
    	pq_dij.push(make_pair(h[u], u));
    	while(!pq_dij.empty())
    	{
    		pii pq_node = pq_dij.top();
    		pq_dij.pop();
    		int v = pq_node.second;
    		if(traversed[v])
    			continue;
    		traversed[v] = true;
    		for(vector<Arc>::iterator iter = AdjlistRev[v].begin(); iter != AdjlistRev[v].end(); iter++)
    		{
    			int w = iter->vex;
    			int weight = iter->weight;
    			if(h[w] > h[v] + weight)
    			{
    				h[w] = h[v] + weight;
    				pq_dij.push(make_pair(h[w], w));
    			}
    		}
    	}
    }
    
    int Astar(int s, int t, int k)
    {
    	priority_queue<pii, vector<pii>, greater<pii> > pq_astar;
    	int cnt[MAX_VEX_NUM];
    	memset(cnt, 0, MAX_VEX_NUM * sizeof(int));
    	pq_astar.push(make_pair(h[s], s));
    	
    	while(!pq_astar.empty())
    	{
    		pii node = pq_astar.top();
    		pq_astar.pop();
    		int u = node.second;
    		int cost = node.first;
    		cnt[u]++;
    		if(cnt[t] == k)
    			return cost;
    		if(cnt[u] > k)
    			continue;
    		for(vector<Arc>::iterator iter = Adjlist[u].begin(); iter != Adjlist[u].end(); iter++)
    		{
    			int v = iter->vex;
    			int weight = iter->weight;
    			if(h[v] != MAX)
    			{
    				pii adjArc = make_pair(cost - h[u] + weight + h[v], v);
    				pq_astar.push(adjArc);
    			}
    		}
    	}
    	return -1;
    }
    
    
    int main()
    {
    	//freopen("in.txt", "r", stdin);
    
    	Init();
    	Dijkstra(T);
    
    	if(S == T)
    		K++;
    	cout<<Astar(S, T, K)<<endl;
    	return 0;
    }
    
  • 相关阅读:
    js和java中使用join来进行数组元素的连接
    java的fail-fast 和 fail-safe机制
    chrome上表单的用户名密码autofill
    InnoDB索引底层是如何查询数据的?
    浏览器允许的并发请求资源数是什么意思?
    async/await的使用
    java获取HttpServletRequest 的客户端ip
    Free software
    Linux 配置 mysql 5.7.32 实操记录
    mysql 帮助手册翻译
  • 原文地址:https://www.cnblogs.com/steady/p/1935817.html
Copyright © 2020-2023  润新知