您的位置:首页 > 其它

POJ 2449 Remmarguts' Date(K短路)

2017-08-27 15:15 295 查看
Remmarguts' Date

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 30907 Accepted: 8438
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

题目大意:

    给你一张有向图,求从起点到终点的第K短的路径为多少。

解题思路:

    K短路的经典解法就时使用A*进行搜索。

    我们可以基于dijkstra算法进行搜索,每次选择一个点,然后相邻的点加入堆。当终点出队K次时,当前的距离就是K短路。不过由于这里只是要求终点的K短路,堆中优先距离终点最近的点。当前点到终点的距离我们可以先对反向图跑一次最短路得到,然后就可以利用它计算估值函数,进行A*搜索。

    这种做法在遇到环的时候最坏复杂度会达到O(K * V),存在更快更稳定的做法,不过这里就不使用了。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define LL long long
#define fi first
#define se second

const int MAXV=1000+3;

struct Edge
{
int to, cost;
Edge(int t, int c):to(t), cost(c){}
};

struct Node
{
int u, g, f;//当前结点,距离起点的距离,估值函数算出的最终结果
Node(int u, int g, int f):u(u), g(g), f(f){}
bool operator < (const Node &other)const
{
if(f==other.f)
return g>other.g;
else return f>other.f;
}
};

int V, E, S, T, K;
vector<Edge> G[MAXV], rG[MAXV];//原图,反图
int dis[MAXV];//点i到终点的距离,用于估值函数计算
bool vis[MAXV];

void init()
{
for(int i=0;i<V;++i)
{
G[i].clear();
rG[i].clear();
}
}

void dij()//计算任意一点到终点的距离,用于估值函数
{
for(int i=0;i<V;++i)
{
dis[i]=INF;
vis[i]=false;
}
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;//保存候选的点, 距离,编号
dis[T]=0;
que.push(make_pair(0, T));
while(!que.empty())
{
pair<int, int> tmp=que.top(); que.pop();
int u=tmp.se;
if(vis[u])
continue;
vis[u]=true;
for(int i=0;i<rG[u].size();++i)
{
int v=rG[u][i].to;
if(!vis[v] && dis[v]>dis[u]+rG[u][i].cost)
{
dis[v]=dis[u]+rG[u][i].cost;
que.push(make_pair(dis[v], v));
}
}
}
}

int a_star()//A*搜索,基于堆优化dijkstra
{
int cnt=0;
priority_queue<Node> que;
if(S==T)//防止把不经过任何边的方案计算在次数中
++K;
if(dis[S]==INF)//不连通
return -1;
que.push(Node(S, 0, dis[S]));
while(!que.empty())
{
Node now=que.top(); que.pop();
int u=now.u;
if(u==T)//找到一条路径
{
++cnt;
if(cnt==K)
return now.g;
}
for(int i=0;i<G[u].size();++i)
{
int v=G[u][i].to;
que.push(Node(G[u][i].to, now.g+G[u][i].cost, now.g+G[u][i].cost+dis[v]));
}
}
return -1;
}

int main()
{
while(~scanf("%d%d", &V, &E))
{
init();
for(int i=0;i<E;++i)
{
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
--u;
--v;
G[u].push_back(Edge(v, c));
rG[v].push_back(Edge(u, c));
}
scanf("%d%d%d", &S, &T, &K);
--S;
--T;
dij();
printf("%d\n", a_star());
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: