Poj2449-BFS + astar

Source: Internet
Author: User

Find the nth short circuit. When I saw this question, I quickly thought of it, but I just couldn't implement it. After reading discuss, you can use Dijkstra to find the shortest path as the evaluation function and use astar to find the shortest path. The most common practice of astar is to use the priority queue to store nodes.

Use tpoint to save the nodes in astar, where to is the point, DIS represents the distance between the current node, and all is the total distance to the target.

#include<queue>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define maxn 1005#define inf 1<<30int sum1[maxn],sum2[maxn];int dis[maxn],cnt[maxn];bool vis[maxn];int tot1,tot2,n,m,t;int st,en,kth;struct TNode{int to,next,cost;bool operator<(const TNode &temp) const{return cost>temp.cost;}};struct TPoint{int to,dis,all;bool operator<(const TPoint &temp) const{return all>temp.all;}};vector<TNode>ans1,ans2;priority_queue<TNode>qu;priority_queue<TPoint>ans;void addedge(int u,int v,int w,bool flag){TNode tmp;tmp.to=u,tmp.cost=w;if(flag){tmp.next=sum1[v],sum1[v]=tot1++;ans1.push_back(tmp);}else{tmp.next=sum2[v],sum2[v]=tot2++;ans2.push_back(tmp);}}void dijkstra(){int k;fill(dis,dis+n+1,inf);memset(vis,0,sizeof(vis));while(!qu.empty()) qu.pop();TNode tmp1,tmp2;tmp1.to=en,tmp1.cost=0;qu.push(tmp1);dis[en]=0;while(!qu.empty()){tmp1=qu.top(),qu.pop();if(vis[tmp1.to]) continue;vis[tmp1.to]=1;for(k=sum1[tmp1.to];k!=-1;k=ans1[k].next){    if(dis[ans1[k].to]>dis[tmp1.to]+ans1[k].cost)    {        dis[ans1[k].to]=dis[tmp1.to]+ans1[k].cost;        tmp2.to=ans1[k].to;        tmp2.cost=dis[tmp2.to];        qu.push(tmp2);    }}}}int astar(){    int i,j,k;    TPoint tmp1,tmp2;    while(!ans.empty()) ans.pop();    memset(cnt,0,sizeof(cnt));    tmp1.to=st,tmp1.dis=0,tmp1.all=dis[st];    if(tmp1.all==inf) return -1;    ans.push(tmp1);    while(!ans.empty())    {        tmp1=ans.top(),ans.pop();        cnt[tmp1.to]++;        if(cnt[en]==kth) return tmp1.all;        if(cnt[tmp1.to]>kth) continue;        for(k=sum2[tmp1.to];k!=-1;k=ans2[k].next)        {            tmp2.to=ans2[k].to;            tmp2.dis=tmp1.dis+ans2[k].cost;            tmp2.all=tmp2.dis+dis[tmp2.to];            ans.push(tmp2);        }    }    return -1;}int main(){int i,u,v,w;while(scanf("%d%d",&n,&m)!=EOF){tot1=tot2=0;memset(sum1,-1,sizeof(sum1));memset(sum2,-1,sizeof(sum2));ans1.clear();ans2.clear();for(i=0;i<m;i++){scanf("%d%d%d",&u,&v,&w);addedge(u,v,w,1);addedge(v,u,w,0);}scanf("%d%d%d",&st,&en,&kth);dijkstra();if(st==en) kth++;printf("%d\n",astar());}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.