POJ——2449 Remmarguts' Date

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

Solution:

  K短路的板子题,直接spfa+A*。

  简单讲下A*的思路,就是在优先队列广搜的基础上,对于当前状态有一个估价函数,每次选择估价函数和当前实际值之和最优的去扩展。

  对于本题,我们先spfa预处理出源点到各点的最短距离。再反向进行A*(反向是因为处理出的最短距离$dis[i]$表示的是源点到$i$的最短路,要用$dis$作为估价函数,就必须使源点变为终点),设$f(x)$表示估价函数,$g(x)$表示$t$到$x$的实际距离,那么显然$f(x)=g(x)+dis[x]$,最后广搜出前$k$小的值就好了。

  注意,设$h(x)$表示实际的代价,$h‘(x)$表示估计的代价,则必须满足$h(x)\geq h‘(x)$(这很显然,大于就会切掉正确的方案)。

  然后本题坑点是$s$可能等于$t$,但又必须往外走一遍回来,所以当$s==t$时记得$k++$。

代码:

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 #include<cstdio>
 9 #define il inline
10 #define ll long long
11 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
12 #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
13 #define Max(a,b) ((a)>(b)?(a):(b))
14 #define Min(a,b) ((a)>(b)?(b):(a))
15 using namespace std;
16 const int N=200005,inf=233333333;
17 int s,t,n,m,k,w[N],W[N],dis[N],ans[N],tot;
18 int to[N],net[N],h[N],cnt1,To[N],Net[N],H[N],cnt2;
19 struct node{
20     int f,g,id;
21     bool operator<(const node a)const{return f>a.f;}
22 };
23 bool vis[N];
24 priority_queue<node>Q;
25
26 il int gi(){
27     int a=0;char x=getchar();bool f=0;
28     while((x<‘0‘||x>‘9‘)&&x!=‘-‘)x=getchar();
29     if(x==‘-‘)x=getchar(),f=1;
30     while(x>=‘0‘&&x<=‘9‘)a=(a<<3)+(a<<1)+x-48,x=getchar();
31     return f?-a:a;
32 }
33
34 il void add(int u,int v,int c){
35     to[++cnt1]=v,net[cnt1]=h[u],h[u]=cnt1,w[cnt1]=c;
36     To[++cnt2]=u,Net[cnt2]=H[v],H[v]=cnt2,W[cnt2]=c;
37 }
38
39 il void spfa(){
40     queue<int>q;
41     For(i,1,n) dis[i]=inf;
42     dis[s]=0,vis[s]=1,q.push(s);
43     while(!q.empty()){
44         int u=q.front();vis[u]=0;q.pop();
45         for(int i=h[u];i;i=net[i])
46             if(dis[to[i]]>dis[u]+w[i]){
47                 dis[to[i]]=dis[u]+w[i];
48                 if(!vis[to[i]])q.push(to[i]),vis[to[i]]=1;
49             }
50     }
51 }
52
53 il void Astar(){
54     if(dis[t]==inf) return;
55     node tmp;
56     tmp.g=0,tmp.f=dis[t],tmp.id=t;
57     Q.push(tmp);
58     while(!Q.empty()){
59         tmp=Q.top();Q.pop();
60         if(tmp.id==s) {ans[++tot]=tmp.g;if(tot>=k)return;}
61         for(int i=H[tmp.id];i;i=Net[i]){
62             node tp;
63             tp.g=tmp.g+W[i];
64             tp.f=tp.g+dis[To[i]];
65             tp.id=To[i];
66             Q.push(tp);
67         }
68     }
69 }
70
71 int main(){
72     while(scanf("%d%d",&n,&m)==2){
73         memset(h,0,sizeof(h));cnt1=0;
74         memset(H,0,sizeof(H));cnt2=0;
75         int u,v,c;
76         For(i,1,m) u=gi(),v=gi(),c=gi(),add(u,v,c);
77         s=gi(),t=gi(),k=gi();
78         if(s==t)k++;
79         spfa();
80         Astar();
81         if(tot<k)cout<<-1;
82         else cout<<ans[k];
83     }
84     return 0;
85 }

POJ——2449 Remmarguts' Date

原文地址:https://www.cnblogs.com/five20/p/9242853.html

时间: 2024-08-02 10:28:01

POJ——2449 Remmarguts' Date的相关文章

图论(A*算法,K短路) :POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 244964-bit integer IO format: %lld      Java class name: Main "Good man never makes girls wait or breaks an appointment!" said the mandari

poj 2449 Remmarguts&#39; Date k短路

/*poj 2449 k短路 A* 估价函数是 s到i的距离+i到t的距离 */ #include<cstdio> #include<queue> #include<vector> #define inf 1e7 #define maxn 100010 using namespace std; int n,m,S,T,K,num1,num2,head1[maxn],head2[maxn],dis[maxn]; int q[maxn],hea,tai,f[maxn],cn

POJ 2449 Remmarguts&#39; Date (第k短路 A*搜索算法模板)

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22412   Accepted: 6085 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

POJ 2449 Remmarguts&#39; Date ( Dijkstra + A* 求解第K短路 )

#include <iostream> #include <cstring> #include <queue> #include <fstream> using namespace std; #define E 100005 #define V 1005 #define INF 1 << 30 int heads[V], r_heads[V]; int dists[V]; bool visits[V]; int nEdgeNum, nNodeNu

poj 2449 Remmarguts&#39; Date(K短路,A*算法)

http://poj.org/problem?id=2449 大致题意:给出一个有向图,求从起点到终点的第K短路. K短路与A*算法详解  学长的博客... 算法过程 #include <stdio.h> #include <iostream> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #in

poj 2449 Remmarguts&#39; Date A*+spfa求第k短路

题意: 经典的第k短路,A*算法的经典应用之一. 分析: A*,已走的路程g+到终点的最短距离为启发函数,搜索过程中不判重,第k次到t节点时就求出了第k短路. 代码: //poj 2449 //sep9 #include <iostream> #include <queue> using namespace std; const int maxN=1024; const int maxM=100024; int n,m,s,t,k,e,ne; int head[maxN],nhea

poj 2449 Remmarguts&#39; Date (k短路模板)

Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30772   Accepted: 8397 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly tou

POJ 2449 Remmarguts&#39; Date (A*搜索求K短路)

传送门 这是一道裸的K短路的问题,我们将会用A*解决. 我们设计估值函数h的时候可以像这样想.因为h(n) <= h*(n)而且要尽量接近h*(n),所以我们想到,可以求一个从目标节点到其余节点的最短路,这个一定是小于等于实际值的.然后就用A*从起点开始搜索,找到一个节点v,就使cnt[v]加1.当cnt[v] > k时就可以剪枝了,因为这一定不再K短路的路线上了.很好通过反证法得到证明.当目标节点被搜索到了第k次的时候就可以结束搜索了. 要注意这道题有一个很坑的地方,就是若给出的起点=终点,

(最短路+A*搜索)POJ 2449 - Remmarguts&#39; Date

题意: 给一个DAG,要求s到t的第K短路,很经典的问题. 分析: 我们可以看到k<=1000,这个值不是很大,我可以想到直接bfs走遍所有情况,最多也就有1000中情况,但是1000个点显然会M. 既然是要求k短路,也就是说最终计算出来的到达t的花费必然是递增的,也就是说我们在搜索的时候肯定要用到优先队列. 这时应该很明显了,必然需要A*搜索,但是A*的预估函数依然是个问题,我想了很久没有想到,后来偶然在一本书上发现了这题. 书上的处理是用dijsktra预处理每点到t的最短路,就是每个状态到