您的位置:首页 > 其它

POJ---2449 Remmarguts' Date[K短路入门题(Dijkstra()||Spfa()+A*)]

2012-08-29 01:26 369 查看
Remmarguts' Date

Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 15399Accepted: 4202
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

Source

POJ Monthly,Zeyuan Zhu

转自:http://hi.baidu.com/xjj59307/item/7e7ddb287447bdc7ef10f117
这道题的本质就是A*求K短路,题目的要求是求出s到t的第K短的路,可以用A*解 决,首先反向建图用Dijkstra求估价函数h[i],即i点到终点t的距离, 然后用基于BFS的优先队列A*来搜,当某个点第K次从队列中被pop出来的时候就找到答案了,严格的证明我也没想到,但可以给出简单直白的证明:
首 先有一个比较明显但很重要的事实,就是K短路中一定包含K条走向终点的路,而程序中的cnt[]数组所标记的正是最小的几条路线的条数,这个道理很明显。 当一个点被pop出来的时候,由于估价函数是这个点到终点的距离,所以一定是当前最优的走向终点的点,所以程序所走的每一步都是绝对最优的选择,这个有点 类似贪心,即用子问题的最优来推出最终的答案。另外一方面,任意一个点都不可能在这条K短路上出现K次以上,这个证明也比较直白,就是每次走到这个点时, 必然多出一条通向终点的不同路线,由于题目中说了路线不同算两条路,所以当第K次走到该点时,至少包含了K条走向终点的不同的路线,所以当搜到第K次走到 某个点时,已经没必要搜下去了,下个pop出来的点所包含的路线长度无论如何都不会比这条路线短,且这条路线已经被证明了包含至少K条不同的路线,故第K次走到当前点所花的代价再直接加上从该点到终点所花的代价即可

code:

/*
所谓K短路,就是从s到t的第K短的路,第1短就是最短路。
如何求第K短呢?有一种简单的方法是广度优先搜索,记录t出队列的次数,当t第k次出队列时,就是第k短路了。但点数过大时,入队列的节点过多,时间和空间复杂度都较高。
A*是在搜索中常用的优化,一种启发式搜索。简单的说,它可以用公式表示为f(n) = g(n) + f(n),其中,f(n)是从s经由节点n到t的估价函数,g(n)是在状态空间中从s到n的实际代价,
h(n)是从n到t的最佳路径估计代价。在设计中,要保证h(n)<= n到t的实际代价,这一点很重要,h(n)越接近真实值,速度越快。
由于启发函数的作用,使得计算机在进行状态转移时尽量避开不可能产生最优解的分支,而选择相对较接近最优解的路径进行搜索,降低了时间和空间复杂度。
算法过程:
1. 将图反向,用dijstra+heap求出t到所有点的最短距离,目的是求所有点到点t的最短路,用dis[i]表示i到t的最短路,其实这就是A*的启发函数,显然:h(n)<= n到t的实际代价。
2. 定义估价函数。我们定义g(n)为从s到n所花费的代价,h(n)为dis
,显然这符合A*算法的要求。
3. 初始化状态。状态中存放当前到达的点i,fi,gi。显然,fi=gi+dis[i]。初始状态为(S,dis[S],0),存入优先级队列中。
4. 状态转移。假设当前状态所在的点v相邻的点u,我们可以得到转换:(V,fv,gv)-->(U,fu+w[v][u],gv+w[v][u])。
5. 终止条件。每个节点最多入队列K次,当t出队列K次时,即找到解。
*/

#include<iostream>
#include<queue>
#include<vector>
using namespace std;

#define MAXN 1010
#define inf 0x3fffffff

int head[MAXN];                           //邻接矩阵
int vst[MAXN];                            //Dijkstra()中标记各点是否被访问
int dis[MAXN];                            //Dijkstra()中各点到终点的距离
int cnt[MAXN];                            //A*算法中记录出队列的次数

int n,m;
int st,et,k;
int edgenum;

typedef struct                           //边的结构体
{
int to;
int dis;
int next;
}Edge;
Edge e[100010];
vector<Edge>G[MAXN];

struct Node                            //节点的结构体
{
int f,g;         //评估函数:f=g+dis[v],f代表路径的长度,g代表距离起点的距离,dis[v]代表距离终点的距离
int v;           //当前到达的节点
Node(int a,int b,int c):f(a),g(b),v(c){}
bool operator < (const Node &a)const             //优先队列,f越小说明越接近
{
return a.f<f;
}
};

//邻接矩阵加边
void addEdge(int from,int to,int dis)
{
e[edgenum].to=to;
e[edgenum].dis=dis;
e[edgenum].next=head[from];
head[from]=edgenum++;
}

//Dijkstra来求反向图中各点距离终点的最短路径
void Dijkstra(int start)
{
int i;
for(i=1;i<=n;i++)
{
vst[i]=0;
dis[i]=inf;
}
dis[start]=0;
priority_queue<Node>Q;
Q.push(Node(0,0,start));          //起点的f和g的值都为0,当前点v为start
Node next(0,0,0);
while(!Q.empty())
{
Node now=Q.top();
Q.pop();
if(vst[now.v])                  //如果该点已被访问过,则continue
continue;
vst[now.v]=1;
for(int i=0;i<G[now.v].size();i++)
{
Edge edge=G[now.v][i];
if(!vst[edge.to]&&dis[now.v]+edge.dis<dis[edge.to])       //松弛
{
dis[edge.to]=dis[now.v]+edge.dis;
next.f=dis[edge.to];
next.v=edge.to;
Q.push(next);
}
}
}
}

//A*算法
int A_star()
{
memset(cnt,0,sizeof(cnt));                             //计数数组清零
int i;
priority_queue<Node>Q;                                 //构建优先队列
if(dis[st]==inf)                                        //如果起点到终点之间不存在通路(即起点无法到达终点)
return -1;
Q.push(Node(dis[st],0,st));                             //评估函数:f=g+dis[v],由于从起点出发,故g置为0
Node next(0,0,0);
while(!Q.empty())
{
Node now=Q.top();
Q.pop();
cnt[now.v]++;
if(cnt[et]==k)                                       //如果终点的出队次数等于k,则说明已经找到了k短路,故返回f的值
return now.f;
if(cnt[now.v]>k)
continue;
for(i=head[now.v];i!=-1;i=e[i].next)
{
next.v=e[i].to;                                  //下个节点
next.g=now.g+e[i].dis;
next.f=next.g+dis[e[i].to];
Q.push(next);
}
}
return -1;                                               //没有找到k短路,则输出-1
}

//主函数
int main()
{
int a,b,t;
edgenum=0;
memset(G,0,sizeof(G));
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
Edge edge;
scanf("%d%d%d",&a,&b,&t);
addEdge(a,b,t);
edge.to=a;
edge.dis=t;
G[b].push_back(edge);
}
scanf("%d%d%d",&st,&et,&k);
if(st==et)               //因为s既是源点又是汇点,所以其第一次入队的时候距离还为0
k++;
Dijkstra(et);
int ans=A_star();
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: