题面
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
题目链接
参考链接
总结A*,Dijkstra,广度优先搜索,深度优先搜索的复杂度比较
题意简述
求S到T的第k长的路径长度。
初学A*算法。
用堆优化的dijkstra计算各点到终点的最短距离,计算估价函数。
当S==T时,路径长度为0的路不算,k++。
两个堆都是小顶堆。
程序
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 1005
struct edge
{
int v;/**有向边指向的点**/;
int w;/**权值**/
int next;/**上一条加进图的边**/
edge(){}
edge(int v,int w,int next):v(v),w(w),next(next){}
}graph[100005],graph2[100005];
int tot,pre[maxn],tot2,pre2[maxn];
void add_edge(int u,int v,int w)
{
graph[tot]=edge(v,w,pre[u]);
pre[u]=tot++;
graph2[tot2]=edge(u,w,pre2[v]);
pre2[v]=tot2++;
}
struct Node
{
int v,d;
Node(int v,int d):v(v),d(d){}
friend bool operator<(Node a,Node b)
{
if(a.d!=b.d)
return a.d>b.d;
else
return a.v>b.v;
}
};
struct point
{
int v,h,g;
point(int v, int h, int g) : v(v), h(h), g(g) {}
friend bool operator<(point a,point b)
{
return a.h+a.g>b.h+b.g;
}
};
int n,m,k,dist[maxn];/**各个点到终点的最短距离**/
bool visit[maxn];/**是否当前为最小的距离**/
void dijkstra(int s)
{
memset(dist,INF,sizeof(dist));
memset(visit,0,sizeof(visit));
dist[s]=0;
priority_queue<Node>q;
visit[s]=true;
q.push(Node(s,0));
while(!q.empty())
{
Node now=q.top();
q.pop();
visit[now.v]=true;
for(int i=pre2[now.v];i!=-1;i=graph2[i].next)
{
if(!visit[graph2[i].v]&&now.d+graph2[i].w<dist[graph2[i].v])
{
dist[graph2[i].v]=now.d+graph2[i].w;
q.push(Node(graph2[i].v,dist[graph2[i].v]));
}
}
}
}
int times[maxn];
int Astar(int s,int e)
{
if(dist[s]==INF)
return -1;
memset(times,0,sizeof(times));
priority_queue<point>Q;
Q.push(point(s,0,0));
while(!Q.empty())
{
point now=Q.top();
Q.pop();
times[now.v]++;
if(times[now.v]==k&&now.v==e)
return now.h+now.g;
if(times[now.v]>k)/**已经存在k条到now.v最短的路了**/
continue;
for(int i=pre[now.v];i!=-1;i=graph[i].next)
{
Q.push(point(graph[i].v,now.h+graph[i].w,dist[graph[i].v]));
}
}
return -1;
}
int main()
{
int u,v,w,Start,End;
scanf("%d%d",&n,&m);
tot2=tot=0;
memset(pre2,-1,sizeof(pre2));
memset(pre,-1,sizeof(pre));
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
}
scanf("%d%d%d",&Start,&End,&k);
if(Start==End)
k++;
dijkstra(End);
printf("%d\n",Astar(Start,End));
return 0;
}
PS:
HDU 1560 DNA sequence (IDA*) MLE ---->学习IDA*
学习IDA*---->学习A*
学习A*---->学写例题POJ 2449
学写例题POJ2449---->重学SPFA、重学Dijkstra
学写例题POJ2449---->重学反码、补码、~0==-1 ~-1==0的原因
递归回去解决HDU1560的MLE问题……
0 的原码:00000,反码(不包含符号位按位取反):00000, 补码:00000,~0(对补码,包含符号位按位取反):11111
-1的原码:10001,反码:11110,补码:11111,~0:00000,
~0==-1的补码,~0==-1
~1==0的补码,~-1==0
公式:~x==-(x+1)