K短路

本文介绍了如何利用A*启发式搜索算法解决图论中的K短路问题,通过实例详细解析了A*算法在poj2449问题中的具体应用和实现过程。

A*启发式搜索


(poj2449)

int n,k,Start,End;
int dis[maxn];         // 存放每个点到终点的最短距离
bool vis[maxn];

struct heap{
    int p,d;   
    heap(int _p,int _d):p(_p),d(_d){}       
    bool operator < (const heap &x) const{
        return d+dis[p] > x.d+dis[x.p];   // d在反向dij里没有用,在A*函数里为该点到起点的距离(不一定是最短距离!!!)
    }
};

///////      ↓构图↓
int head[maxn],rhead[maxn];   // 构反向图是为了求出每个点到终点的最短距离
struct Edge{
    int from,to,cost;
    int next,rnext;
}e[100005];
int edgenum=0;
void addEdge(int from,int to,int cost){
    e[edgenum].from=from;
    e[edgenum].to=to;
    e[edgenum].cost=cost;
    e[edgenum].next=head[from];
    head[from]=edgenum;
    e[edgenum].rnext=rhead[to];
    rhead[to]=edgenum;
    edgenum++;
}
///////

void dij(int s){
    priority_queue<heap> q;
    memset(dis,INF,sizeof(dis));
    dis[s]=0;
    q.push(heap(s,0));
    while(!q.empty()){
        int p=q.top().p;
        q.pop();
        if(vis[p])continue;
        vis[p]=true;
        for(int i=rhead[p];i!=-1;i=e[i].rnext){
            int v=e[i].from;
            if(dis[p]+e[i].cost<dis[v]){
                dis[v]=dis[p]+e[i].cost;
                q.push(heap(v,0));
            }
        }
    }
}

int A_start(int s){
    priority_queue<heap> q;
    q.push(heap(s,0));
    while(!q.empty()){
        heap h=q.top();
        q.pop();
        if(h.p==End){
            if(k>1)
                k--;
            else
                return h.d;
        }
        for(int i=head[h.p];i!=-1;i=e[i].next)
            q.push( heap( e[i].to , h.d+e[i].cost ) );  //  核心
    }
    return -1;
}

int main() {
    freopen("input.txt","r",stdin);
    int i,j,a,b,t;
    memset(head,-1,sizeof(head));
    memset(rhead,-1,sizeof(rhead));
    memset(vis,false,sizeof(vis));
    scanf("%d%d",&n,&t);
    while(t--){
        scanf("%d%d%d",&i,&j,&a);
        addEdge(i,j,a);
    }
    scanf("%d%d%d",&Start,&End,&k);
    dij(End);
    if(dis[Start]==INF){
        puts("-1\n");
        return 0;
    }
    if(Start==End)k++;     // 题目要求
    printf("%d\n",A_start(Start));
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值