首页 > 代码库 > POJ 2449 Astar算法 (A*) + Dijkstra
POJ 2449 Astar算法 (A*) + Dijkstra
Remmarguts‘ Date
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 21396 | Accepted: 5823 |
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 21 2 52 1 41 2 2
Sample Output
14
题目意思:
给一个n个点和m条边的图,然后给出x,y,k即从x到y第k短的路径长度,若没有第k短输出-1.
思路:
裸的K短路。。只要会K短路就一定能写出来。。
第一次写A*算法...好不容易学会并且写出来了但是wa了一下午。。。无奈先放着,晚上放学后继续研究代码看哪里错了,还是无解。。。忽然我改了一下dijkstra代码就AC了...只是把先压入y改为什么也不压进去就AC,表示很脑残...
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 #include <vector> 7 using namespace std; 8 9 #define N 1005 10 #define inf 999999999 11 12 struct mem{ 13 int y, w; 14 }; 15 16 struct node{ 17 int x, g, h; 18 bool operator<(node a)const{ 19 return a.g+a.h<g+h; //以g+h最小的优先 出队 20 } 21 }; 22 23 int visited[N]; 24 int dis[N]; 25 vector<mem>fan[N], zh[N]; 26 int n, m; 27 int st, en, k; 28 int cnt[N]; 29 30 void dijkstra(){ 31 int i, j, u, v, tmp; 32 mem p, q; 33 memset(visited,0,sizeof(visited)); 34 for(i=0;i<=n;i++) dis[i]=inf; 35 dis[en]=0; 36 for(i=1;i<=n;i++){ //刚开始我把end先压进去,然后i从2开始,但是会wa,改成这样就行了 37 int tmp=inf; 38 for(j=1;j<=n;j++){ 39 if(dis[j]<tmp&&!visited[j]){ 40 v=j;tmp=dis[j]; 41 } 42 } 43 if(tmp==inf) break; 44 visited[v]=1; 45 for(j=0;j<fan[v].size();j++){ 46 p=fan[v][j]; 47 if(!visited[p.y]&&dis[p.y]>dis[v]+p.w){ 48 dis[p.y]=dis[v]+p.w; 49 } 50 } 51 } 52 } 53 54 int Astar(){ //A*算法模板 55 56 int i, j; 57 node p, q; 58 mem pp; 59 priority_queue<node>Q; 60 memset(cnt,0,sizeof(cnt)); 61 while(!Q.empty()) Q.pop(); 62 p.x=st;p.g=0;p.h=dis[st]; 63 Q.push(p); 64 while(!Q.empty()){ 65 p=Q.top(); 66 Q.pop(); 67 cnt[p.x]++; 68 if(cnt[p.x]>k) continue; 69 if(cnt[en]==k) return p.g+p.h; //当en出队k次即找到第k短路 70 for(i=0;i<zh[p.x].size();i++){ 71 pp=zh[p.x][i]; 72 q.x=pp.y; 73 q.g=p.g+pp.w; 74 q.h=dis[q.x]; 75 Q.push(q); 76 } 77 } 78 return -1; 79 } 80 81 main() 82 { 83 int i, j; 84 while(scanf("%d %d",&n,&m)==2){ 85 for(i=0;i<N;i++){ 86 zh[i].clear(); 87 fan[i].clear(); 88 } 89 int x, y, z; 90 mem p; 91 while(m--){ 92 scanf("%d %d %d",&x,&y,&z); 93 p.y=y;p.w=z; 94 zh[x].push_back(p); 95 p.y=x; 96 fan[y].push_back(p); 97 } 98 scanf("%d %d %d",&st,&en,&k); 99 if(st==en) k++;100 dijkstra();101 // for(i=1;i<=n;i++) printf("%d ",dis[i]);102 int ans=Astar();103 printf("%d\n",ans);104 }105 }
POJ 2449 Astar算法 (A*) + Dijkstra
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。