您的位置:首页 > 其它

poj 2449 Remmarguts' Date (k短路)

2017-02-12 16:27 260 查看
Remmarguts' Date

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 28894 Accepted: 7862
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
[Submit]   [Go Back]   [Status]  
[Discuss]

题目大意:给出一个有向图,给出起点终点s,t求s->t的第k短路

题解:spfa+A*

K短路问题可以用搜索去解决,从队列中取出一个点去更新其他的节点时,当节点i第k次出队的时候,那么当前得到的答案就是第k短路。

搜索的时候需要用启发式搜索就是A*,设立估价函数。

设估价函数为f[i]。f[i]=g[i]+h[i] 其中g[i]表示从起点到达i的当前长度,h[i]为i到终点的最短路径长度。然后每次从优先队列中选取f[i]最小的出来更新相邻的节点,直到找到答案为止。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define N 100010
#define M 2003
#define inf 1000000000
using namespace std;
int n,m,tot,k,s,t,tot1;
int point[M],v
,nxt
,dis[M],len
,cnt[M],can[M];
int point1[M],nxt1
,v1
,len1
;
struct data
{
int g,h,to;
bool operator <(data a) const {
return a.g+a.h<g+h;
}
};
void add(int x,int y,int z)
{
tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; len[tot]=z;
tot1++; nxt1[tot1]=point1[y]; point1[y]=tot1; v1[tot1]=x; len1[tot1]=z;
}
void spfa()
{
queue<int> p;
for (int i=1;i<=n;i++) dis[i]=inf;
dis[t]=0; can[t]=1;
p.push(t);
while (!p.empty()) {
int now=p.front(); p.pop();
for (int i=point1[now];i;i=nxt1[i])
if (dis[now]+len1[i]<dis[v1[i]]) {
dis[v1[i]]=dis[now]+len1[i];
if (!can[v1[i]]) {
can[v1[i]]=1;
p.push(v1[i]);
}
}
can[now]=0;
}
}
int astar(int k)
{
memset(cnt,0,sizeof(cnt));
data cur,nt;
priority_queue <data> p;
cur.to=s; cur.g=0; cur.h=dis[s];
p.push(cur);
while (!p.empty()) {
cur=p.top(); p.pop();
cnt[cur.to]++;
if (cnt[cur.to]>k) continue;
if (cnt[t]==k) return cur.g;
for (int i=point[cur.to];i;i=nxt[i]){
nt.to=v[i];
nt.g=cur.g+len[i];
nt.h=dis[v[i]];
p.push(nt);
}
}
return -1;
}
int main()
{
freopen("a.in","r",stdin);
//freopen("my.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++) {
int x,y,z; scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
scanf("%d%d%d",&s,&t,&k);
spfa();
// for (int i=1;i<=n;i++) cout<<dis[i]<<" ";
// cout<<endl;
if (s==t) k++;
int ans=astar(k);
printf("%d\n",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: