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
找第k短路。可以用宽搜,但是宽搜空间复杂度特别高,所以要A*算法优化,f(x) = g(x)+h(x),f(x)为每个状态的优先程度,越小越优先,g(x)是搜索所需要耗费的,h(x)是当前状态到目标状态的距离。用这个广搜,很多不必要的状态就不会加入队列,所以空间复杂度减小
/***********************************************\
|Author: YMC
|Created Time: 2014/4/18 8:32:15
|File Name: poj2449.cpp
|Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
using namespace std;
#define N 1005
#define M 200100
#define INF 1<<29
int head[N],tail[N],dis[N];
int num[N];
int n,m;
int s,t,k;
bool vis[N];
struct node{
int v,c,nxt;
}edge[M];
struct seg{
int h,g,d;
bool operator < (const seg a)const{
return a.g + a.h < h+g;
}
};
void add_edge(int u,int v,int c,int e){
edge[e<<1].v = v;
edge[e<<1].c = c;
edge[e<<1].nxt = head[u];
head[u] = e<<1;
edge[e<<1|1].v = u;
edge[e<<1|1].c = c;
edge[e<<1|1].nxt = tail[v];
tail[v] = e<<1|1;
}
void dij(){
memset(vis,false,sizeof(vis));
memset(dis,0x7F,sizeof(dis));
dis[t] = 0;
//vis[t] = true;
for(int i=1;i<=n;++i){
int mm = 0x7FFF;
int pos = -1;
for(int j=1;j<=n;++j){
if(!vis[j] && mm > dis[j]){
mm = dis[j];
pos = j;
}
}
if(pos == -1) break;
vis[pos] = true;
for(int j=tail[pos];j!=-1;j=edge[j].nxt){
int v = edge[j].v;
if(dis[v] > edge[j].c + dis[pos]) dis[v] = edge[j].c + dis[pos];
}
}
}
int astar(){
memset(num,0,sizeof(num));
seg tp,tp1,tp2;
priority_queue<seg> pq;
tp1.h = dis[s];
tp1.g = 0;
tp1.d = s;
pq.push(tp1);
while(!pq.empty()){
tp = pq.top();
pq.pop();
num[tp.d] ++;
if(num[tp.d] > k) continue;
if(num[t] == k) return tp.g;
for(int i=head[tp.d];i!=-1;i = edge[i].nxt){
tp2.d = edge[i].v;
tp2.g = tp.g + edge[i].c;
tp2.h = dis[tp2.d];
pq.push(tp2);
}
}
return -1;
}
int main() {
//freopen("input.txt","r",stdin);
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF){
memset( head,0xFF,sizeof(head) );
memset( tail,0xFF,sizeof(tail) );
for(int i=0;i<m;++i){
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c,i);
}
scanf("%d%d%d",&s,&t,&k);
if(s == t) k++;
dij();
//for(int i=1;i<=n;++i) cout<<dis[i]<<" ";cout<<endl;
printf("%d\n",astar());
}
return 0;
}
本文介绍了一种使用A*算法优化寻找从起点到终点的第K条最短路径的方法,适用于包含多个站点的大规模网络环境。该算法通过合理评估状态的重要性来减少不必要的搜索空间。
258

被折叠的 条评论
为什么被折叠?



