poj2449(第k短路模板,A*算法+spfa)

题目链接:http://poj.org/problem?id=2449

Remmarguts' Date
Time Limit: 4000MS

Memory Limit: 65536K
Total Submissions: 39386

Accepted: 10775
Description
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. 

"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission." 

"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)" 

Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help! 

DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate. 
Input
The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T. 

The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
Output
A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.
Sample Input
2 2
1 2 5
2 1 4
1 2 2
Sample Output
14

输入:第一行两个数n,m 表示点的个数和边的条数,接下来m行每行三个数a,b,c表示从a到b有一条长度为c的单向边,最后一行三个数u,v,k

输出:u到v的第k短路

A*算法:在bfs的基础上加一个启发函数用来限制bfs的优先级(我的理解)想具体了解的话去看大牛的博客

启发函数:F=f+g;f表示v到当前节点的代价,g表示当前节点到v的期望值,根据题意对F进行具体优先级选择

思路:f为bfs求出的从v到当前节点的距离,g为当前节点到u的最短距离(图中的边为单向边,这里反向建图,用spfa求u到各个节点的最短距离),然后从v开始bfs,将每一次bfs得到的F存入优先队列,取队头继续bfs,每一次遍历得到的结果的终点为u时,计数一次,到第k次时得到的距离就是v到u的第k短路

AC代码:

#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
const int MOD=998244353;
const int inf=0x3f3f3f3f;
const LL MAX_N=30005;
const LL MAX_M=100005;
#define MEF(x) memset(x,-1,sizeof(x))
#define ME0(x) memset(x,0,sizeof(x))
#define MEI(x) memset(x,inf,sizeof(x))
struct LX
{
    int v,next,di;
}lxz[100005],lxf[100005];
struct H
{
    int u,f,g;
    bool operator < (const H &aa) const
    {
        return f+g>aa.f+aa.g;
    }
};
int firstz[1005],firstf[1005];
int vis[1005];
int dis[1005];
queue q;
priority_queue pq;
void add(int u,int v,int di,int cnt,int *first,LX *a)
{
    a[cnt].v=v;
    a[cnt].di=di;
    a[cnt].next=first[u];
    first[u]=cnt;
}
void spfa(int x,int *first,LX *a)
{
    dis[x]=0;
    q.push(x);
    vis[x]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        vis[x]=0;
        for(int i=first[x];i!=-1;i=a[i].next)
        {
            int y=a[i].v;
            if(dis[y]>dis[x]+a[i].di)
            {
                dis[y]=dis[x]+a[i].di;
                if(!vis[y])
                {
                    q.push(y);
                    vis[y]=1;
                }
            }
        }
    }
}
int Astar(int x,int y,int z)
{
    int t,time[1005];
    ME0(time);
    H ha,hb;
    ha.u=x;ha.f=0;ha.g=0;
    pq.push(ha);
    while(!pq.empty())
    {
        ha=pq.top();
        pq.pop();
        time[ha.u]++;
        if(time[ha.u]==z&&ha.u==y)
        {
            return ha.f+ha.g;
        }
        if(time[ha.u]>z)
        {
            continue;
        }
        for(int i=firstz[ha.u];i!=-1;i=lxz[i].next)
        {
            hb.f=ha.f+lxz[i].di;
            hb.g=dis[lxz[i].v];
            hb.u=lxz[i].v;
            pq.push(hb);
        }
    }
    return -1;
}
int main()
{
    int n,m,a,b,c;
    scanf("%d%d",&n,&m);
    MEF(firstz);
    MEF(firstf);
    ME0(vis);
    MEI(dis);
    for(int m1=1;m1<=m;m1++)
    {
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c,m1,firstz,lxz);
        add(b,a,c,m1,firstf,lxf);
    }
    scanf("%d%d%d",&a,&b,&c);
    spfa(b,firstf,lxf);
    if(a==b)
    {
        c++;
    }
    printf("%d\n",Astar(a,b,c));
}

 

 

 

你可能感兴趣的:(bfs)