Remmarguts' Date
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 26115 | Accepted: 7097 |
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.
"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).
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
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include <stack>
#include <string>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define maxn 100000+10
const int INF = 0x3f3f3f3f;
const int MAXX = 1000+10;
int n, m;
int head[maxn], head1[maxn];
bool vis[MAXX];
struct Edge
{
int to, w, next;
}edge[maxn], edge1[maxn];
struct node
{
int to;
int f, g;
bool operator < (const node &a)const
{
if (a.f == f)
return g>a.g;
return f>a.f;
}
};
using namespace std;
int cnt;
int c;
int dis[MAXX];
int outq[MAXX];
void addedge(int u, int v, int c)
{
edge[cnt].to = v;
edge[cnt].w = c;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;
}
void add(int u, int v, int cc)
{
edge1[c].to = v;
edge1[c].w = cc;
edge1[c].next = head1[u];
head1[u] = c;
c++;
}
bool SPFA(int t)
{
memset(dis, INF, sizeof(dis));
memset(outq, 0, sizeof(outq));
queue<int>q;
memset(vis, false, sizeof(vis));
q.push(t);
vis[t] = true;
dis[t] = 0;
int u, v;
while (!q.empty())
{
u = q.front();
q.pop();
outq[u]++;
vis[u] = false;
if (outq[u] > n)
return false;
for (int i=head1[u]; i!=-1; i=edge1[i].next)
{
v = edge1[i].to;
if (dis[v] > dis[u]+edge1[i].w)
{
dis[v] = dis[u]+edge1[i].w;
if (!vis[v])
{
q.push(v);
vis[v] = true;
}
}
}
}
return true;
}
int a_star(int s, int t, int k, int head[MAXX], Edge edge[MAXX])
{
node e, ne;
int tt=0;
priority_queue<node>q;
if (s == t)
k++;
if (dis[s] == INF)
return -1;
e.to = s;
e.g = 0;
e.f = e.g+dis[e.to];
q.push(e);
while (!q.empty())
{
e = q.top();
q.pop();
if (e.to == t)
tt++;
if (tt == k)
return e.g;
for (int i=head[e.to]; i!=-1; i=edge[i].next)
{
ne.to = edge[i].to;
ne.g = e.g+edge[i].w;
ne.f = ne.g+dis[ne.to];
q.push(ne);
}
}
return -1;
}
int main()
{
while (scanf("%d%d", &n, &m)!=EOF)
{
c = cnt = 0;
memset(head, -1, sizeof(head));
memset(head1, -1, sizeof(head1));
for (int i=0; i<m; i++)
{
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
addedge(u, v, c);
add(v, u, c);
}
int s, t, k;
scanf("%d%d%d", &s, &t, &k);
SPFA(t);
int kthlength = a_star(s, t, k, head, edge);
printf("%d\n", kthlength);
}
return 0;
}
本文介绍了一种寻找从起点到终点的第K条最短路径的算法,该问题出现在一个有趣的场景中,即王子需要找到迎接公主的第K条最短路径。文章详细解释了如何构建图模型并使用SPFA算法预处理负环问题,接着采用A*启发式搜索算法求解最终答案。
1万+

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



