POJ 2449 Remmarguts' Date ( Dijkstra + A* 求解第K短路 )

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

#define E 100005
#define V 1005
#define INF 1 << 30

int heads[V], r_heads[V];
int dists[V];
bool visits[V];

int nEdgeNum, nNodeNum, nEdgeCount;
int nEnd, nSrc, k;

struct Edge{

    int to_node;
    int next_edge;
    int edge_weight;

    int r_to_node;
    int r_next_edge;

    Edge(){}
    Edge( int from, int to, int weight ){
        to_node     = to;
        r_to_node   = from;
        edge_weight = weight;
     }

}edges[E];

struct Node{

    int v;
    int src_to_v_dist;

    Node(){
        this->v             = 0;
        this->src_to_v_dist = 0;
    }
    Node( int v, int d ){
        this->v             = v;
        this->src_to_v_dist = d;
    }
    bool operator < ( const Node& other ) const{
        return src_to_v_dist + dists[v] > dists[other.v] + other.src_to_v_dist;
    }

};

void addEdge( int from, int to, int dist ){

    edges[nEdgeCount]             = Edge( from, to, dist );
    edges[nEdgeCount].r_next_edge = r_heads[to];
    edges[nEdgeCount].next_edge   = heads[from];
    heads[from]                   = nEdgeCount;
    r_heads[to]                   = nEdgeCount;

    nEdgeCount++;

}

void dijkstra( int src ){

    priority_queue< Node > que;

    for( int i = 1; i <= nNodeNum; ++i )
        dists[i] = INF;

    dists[src] = 0;
    que.push( Node( src, 0 ) );

    while( !que.empty() ){

        Node cur = que.top();
        que.pop();

        if( visits[cur.v] )
            continue;

        visits[cur.v] = true;

        for( int i = r_heads[cur.v]; ~i; i = edges[i].r_next_edge ){
            if( dists[edges[i].r_to_node] > dists[cur.v] + edges[i].edge_weight ){
                dists[edges[i].r_to_node] = dists[cur.v] + edges[i].edge_weight;
                que.push( Node( edges[i].r_to_node, 0 ) );
            }
        }
    }
}

int AStar( int src ){

    priority_queue< Node > que;

    que.push( Node( src, 0 ) );

    while( !que.empty() ){

        Node cur = que.top();
        que.pop();

        if( cur.v == nEnd ){
            if( k > 1 )
                k--;
            else
                return cur.src_to_v_dist;
        }

        for( int i = heads[cur.v]; ~i; i = edges[i].next_edge )
            que.push( Node( edges[i].to_node, cur.src_to_v_dist + edges[i].edge_weight ) );

    }

    return -1;

}

void init(){
    memset( visits,  false, sizeof( visits ) );
    memset( heads,   -1,    sizeof( heads ) );
    memset( r_heads, -1,    sizeof( r_heads ) );
    nEdgeCount = 0;
}

int main(){

    //fstream fin( "t.txt" );

    while( cin >> nNodeNum >> nEdgeNum ){

        init();

        for( int i = 1; i <= nEdgeNum; ++i ){

            int from, to, dist;

            cin >> from >> to >> dist;

            addEdge( from, to, dist );

        }

        cin >> nSrc >> nEnd >> k;

        dijkstra( nEnd );

        if( dists[nSrc] == INF ){
            cout << "-1\n";
            continue;
        }

        if( nSrc == nEnd )
            k++;

        int ans = AStar( nSrc );

        cout << ans << endl;

    }

    return 0;
}

POJ 2449 Remmarguts' Date ( Dijkstra + A* 求解第K短路 )

时间: 2024-12-21 18:27:57

POJ 2449 Remmarguts' Date ( Dijkstra + A* 求解第K短路 )的相关文章

poj 2449 Remmarguts&#39; Date A*+spfa求第k短路

题意: 经典的第k短路,A*算法的经典应用之一. 分析: A*,已走的路程g+到终点的最短距离为启发函数,搜索过程中不判重,第k次到t节点时就求出了第k短路. 代码: //poj 2449 //sep9 #include <iostream> #include <queue> using namespace std; const int maxN=1024; const int maxM=100024; int n,m,s,t,k,e,ne; int head[maxN],nhea

POJ 2449 Remmarguts&#39; Date【SPFA】【A*】

Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 21978 Accepted: 5982 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he to

POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 244964-bit integer IO format: %lld      Java class name: Main "Good man never makes girls wait or breaks an appointment!" said the mandari

POJ 2449 Remmarguts&#39; Date ( 第 k 短路 &amp;&amp; A*算法 )

题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 1024; const int maxm = 100008; struct EDGE{ int v

图论(A*算法,K短路) :POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

poj 2449 Remmarguts&#39; Date k短路

/*poj 2449 k短路 A* 估价函数是 s到i的距离+i到t的距离 */ #include<cstdio> #include<queue> #include<vector> #define inf 1e7 #define maxn 100010 using namespace std; int n,m,S,T,K,num1,num2,head1[maxn],head2[maxn],dis[maxn]; int q[maxn],hea,tai,f[maxn],cn

POJ 2449 Remmarguts&#39; Date (第k短路 A*搜索算法模板)

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22412   Accepted: 6085 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

poj 2449 Remmarguts&#39; Date(K短路,A*算法)

http://poj.org/problem?id=2449 大致题意:给出一个有向图,求从起点到终点的第K短路. K短路与A*算法详解  学长的博客... 算法过程 #include <stdio.h> #include <iostream> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #in

poj 2449 Remmarguts&#39; Date (k短路模板)

Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30772   Accepted: 8397 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly tou