poj2449 Remmarguts' Date

来源:互联网 发布:ios蓝牙调试软件 编辑:程序博客网 时间:2024/06/15 06:08
Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 19745 Accepted: 5374

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 21 2 52 1 41 2 2

Sample Output

14


模板题  求k短路


学这个目的是为了学习A*算法,感觉k短路运用的A*很好理解,而且对于g这个估价函数的预处理也很好。

因为要使g最好,那么当前点到终点的距离尽可能最小,也就是说除终点以外的点到终点的距离最小,

就可以转换为单源最短路径,首先想到dijkstra,把终点设为起点,还有spfa。

下面就要用到A*算法,求k短路,h表示当前走过的距离,首先把起点入队,每次都把花费f=g+h最小的出队,

使用优先级队列,而且要记录每次到达终点的次数,如果是第k次,就把h返回。


代码 :

#include <stdio.h>#include <string.h>#include <queue>using namespace std;struct Node{int to, w, next;};struct ASTAR{int to;int g, h;bool operator<(const ASTAR& a) const{return a.g+a.h<g+h;}};#define N 100010Node g[N<<1];int head[N], tail[N], count[N];char vis[N];int dis[N], cnt, n;int s, t, k;void addedge(int u, int v, int w){g[cnt].to = v;g[cnt].w = w;g[cnt].next = head[u];head[u] = cnt++;g[cnt].to = u;g[cnt].w = w;g[cnt].next = tail[v];tail[v] = cnt++;}void dijkstra(){memset(vis, 0, sizeof(vis));memset(dis, 0x7f, sizeof(dis));dis[t] = 0;//vis[t] = 1;int kk, to;for (int q=0; q<n; ++q){int min = 0x7f7f7f7f;for (int i=1; i <=n; ++i){if (!vis[i] && dis[i] < min){min = dis[i];kk = i;}}vis[kk] = 1;int v;for (int i=tail[kk]; i != -1; i = g[i].next){v = g[i].to;if (!vis[v] && dis[v] > min+g[i].w){dis[v] = min + g[i].w;}}}}int astar(){int v;priority_queue<ASTAR> q;ASTAR cur, node;memset(count, 0, sizeof(count));cur.to = s;cur.g = 0;cur.h = dis[s];q.push(cur);while (!q.empty()){cur = q.top();q.pop();count[cur.to]++;if (cur.to == t && count[cur.to] == k){return cur.g;}for (int i=head[cur.to]; i != -1; i = g[i].next){v = g[i].to;node.to = v;node.g = cur.g + g[i].w;node.h = dis[v];q.push(node);}}return -1;}int main(){int m, u, v, w;while (~scanf("%d%d", &n, &m)){memset(tail, -1, sizeof(tail));memset(head, -1, sizeof(head));for (int i=0; i<m; ++i){scanf("%d%d%d", &u, &v, &w);addedge(u, v, w);}scanf("%d%d%d",&s, &t, &k);if (s==t)k++;dijkstra();int ans = astar();printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击