【最短路】【k短路】Remmarguts' Date

该博客介绍了一个求解图中从起点S到终点T的第K短路问题,允许路径重复经过点或边。通过构建反向图并结合启发式搜索方法,解决从起点到每个节点再到终点的最短路径问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

poj2449

Description

给定一张N个点,M条边的有向图,求从起点S到终点T的第K短路的长度,路径允许重复经过点或边。其中 1≤S,T≤
N≤1000,0≤M≤〖10〗^5,1≤K≤1000。注:不熟悉最短路的读者可以简单浏览0x61节,或者假设我们能够在一张
图上求出单源最短路(从一个点出发到其他所有点的最短路径长度),然后在此基础上考虑本题。

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短路的板子题

在建图的时候,我们可以反向再建一张图,然后用反向建出来的这张图求出每个节点到终点的最短路,也就是说把终点 t 看成起点求它与每个节点的最短路径。

然后从起点s到终点t的路径就变成了从s到当前节点 + 从当前节点到t,因为后者我们已经算出来了,所以只需要正向跑一个最短路,同时使用一个启发式函数,当找寻到第k短的最短路径时自动退出就大功告成了。

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct edge {
    int v, w, next;
} e1[100010], e2[100010];
int head1[1010], head2[1010], tot;

int n, m, k;
int dis[1010];
bool vis[1010];

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();}
    return x * f;
}

struct node {
    int id, f, g;
    node(int dd, int ff, int gg) {
	id = dd, f = ff, g = gg;
    }
    bool operator < (const node & x) const {
	if(x.f == f) return x.g < g;
	return x.f < f;
    }
};

void add(int u, int v, int w) {
    e1[++tot].v = v;
    e1[tot].w = w;
    e1[tot].next = head1[u];
    head1[u] = tot;
    e2[tot].v = u;
    e2[tot].w = w;
    e2[tot].next = head2[v];
    head2[v] = tot;
}
void spfa(int u) {
    queue<int> q;
    for(int i = 1; i <= n; i++) dis[i] = 1e9;
    dis[u] = 0;
    vis[u] = 1;
    q.push(u);
    while(!q.empty()) {
	u = q.front();
	q.pop();
	vis[u] = 0;
	for(int i = head2[u]; i; i = e2[i].next) {
	    int v = e2[i].v, w = e2[i].w;
	    if(dis[v] > dis[u] + w) {
		dis[v] = dis[u] + w;
		if(!vis[v]) {
		    vis[v] = 1;
		    q.push(v);
		}
            }
	}
    }
}

void A_star(int s, int t) {
    if(s == t) k++;
    priority_queue<node> q;
    q.push(node(s, 0, 0));
    int cnt = 0;
    while(!q.empty()) {
	node h = q.top();
	q.pop();
	if(h.id == t) {
	    if(++cnt == k) {
		printf("%d", h.f);
		return;
	    }
	}
	for(int i = head1[h.id]; i; i = e1[i].next) {
	    int v = e1[i].v, w = e1[i].w;
	    q.push(node(v, h.g + w + dis[v], h.g + w));
	}
    }
    puts("-1");
}
int main() {
    n = read(), m = read();
    for (int i = 1; i <= m; i++) {
	int u = read(), v = read(), w = read();
	add(u, v, w);
    }
    int s = read(), t = read();
    k = read();
    spfa(t);
    A_star(s, t);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值