Remmarguts' Date(POJ2449+最短路+A*算法)

Posted dillonh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Remmarguts' Date(POJ2449+最短路+A*算法)相关的知识,希望对你有一定的参考价值。

题目链接:http://poj.org/problem?id=2449

题目:

技术分享图片

技术分享图片

题意:求有向图两点间的k短路。

思路:最短路+A*算法

代码实现如下:

  1 #include <set>
  2 #include <map>
  3 #include <queue>
  4 #include <stack>
  5 #include <cmath>
  6 #include <bitset>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstdlib>
 11 #include <cstring>
 12 #include <iostream>
 13 #include <algorithm>
 14 using namespace std;
 15 
 16 typedef long long ll;
 17 typedef unsigned long long ull;
 18 
 19 #define lson i<<1
 20 #define rson i<<1|1
 21 #define bug printf("*********
");
 22 #define FIN freopen("D://code//in.txt", "r", stdin);
 23 #define debug(x) cout<<"["<<x<<"]" <<endl;
 24 #define IO ios::sync_with_stdio(false),cin.tie(0);
 25 
 26 const double eps = 1e-8;
 27 const int mod = 10007;
 28 const int maxn = 1e5 + 7;
 29 const double pi = acos(-1);
 30 const int inf = 0x3f3f3f3f;
 31 const ll INF = 0x3f3f3f3f3f3f3f;
 32 
 33 inline int read() {//读入挂
 34     int ret = 0, c, f = 1;
 35     for(c = getchar(); !(isdigit(c) || c == -); c = getchar());
 36     if(c == -) f = -1, c = getchar();
 37     for(; isdigit(c); c = getchar()) ret = ret * 10 + c - 0;
 38     if(f < 0) ret = -ret;
 39     return ret;
 40 }
 41 
 42 int n, m, s, t, u, v, w, k, tot;
 43 int d[maxn<<1], cnt[maxn<<1], vis[maxn<<1];
 44 int head[maxn<<1], head1[maxn<<1];
 45 pair<int, int> P;
 46 
 47 struct b {
 48     int v, w, next;
 49 }biao[maxn<<1];
 50 
 51 struct node {
 52     int g, h;
 53     int to;
 54     bool operator < (node a) const {
 55         return (a.h + a.g) < h + g;
 56     }
 57 };
 58 
 59 void add(int u, int v, int w) {
 60     biao[tot].v = v;
 61     biao[tot].w = w;
 62     biao[tot].next = head[u];
 63     head[u] = tot++;
 64 
 65     biao[tot].v = u;
 66     biao[tot].w = w;
 67     biao[tot].next = head1[v];
 68     head1[v] = tot++;
 69 }
 70 
 71 void init() {
 72     tot = 0;
 73     memset(head, -1, sizeof(head));
 74     memset(head1, -1, sizeof(head1));
 75 }
 76 
 77 void spfa() {
 78     memset(vis, 0, sizeof(vis));
 79     memset(d, inf, sizeof(d));
 80     d[t] = 0;
 81     vis[t] = 1;
 82     queue<int> q;
 83     q.push(t);
 84     while(!q.empty()) {
 85         int u = q.front();
 86         q.pop();
 87         vis[u] = 0;
 88         for(int i = head1[u]; i != -1; i = biao[i].next) {
 89             int v = biao[i].v;
 90             if(d[v] > d[u] + biao[i].w) {
 91                 d[v] = d[u] + biao[i].w;
 92                 if(!vis[v]) {
 93                     q.push(v);
 94                     vis[v] = 1;
 95                 }
 96             }
 97         }
 98     }
 99 }
100 
101 int AA() {
102     memset(cnt, 0, sizeof(cnt));
103     priority_queue<node> Q;
104     node p, q;
105     p.g = 0;
106     p.h = d[s];
107     p.to = s;
108     Q.push(p);
109     while(!Q.empty()) {
110         p = Q.top(), Q.pop();
111         cnt[p.to]++;
112         if(cnt[p.to] > k) continue;
113         if(cnt[t] == k) return p.g;
114         int u = p.to;
115         for(int i = head[u]; i != -1; i = biao[i].next) {
116             int v = biao[i].v;
117             q.to = v;
118             q.g = p.g + biao[i].w;
119             q.h = d[v];
120             Q.push(q);
121         }
122     }
123     return -1;
124 }
125 
126 int main() {
127     //FIN;
128     scanf("%d%d", &n, &m);
129     init();
130     while(m--) {
131         scanf("%d%d%d", &u, &v, &w);
132         add(u, v, w);
133     }
134     scanf("%d%d%d", &s, &t, &k);
135     spfa();
136     if(s == t) k++;
137     int ans = AA();
138     printf("%d
", ans);
139     return 0;
140 }

 

以上是关于Remmarguts' Date(POJ2449+最短路+A*算法)的主要内容,如果未能解决你的问题,请参考以下文章

poj 2449 Remmarguts' Date

POJ2449 Remmarguts' Date

POJ 2449 Remmarguts' Date -K短路

POJ——2449 Remmarguts' Date

POJ 2449Remmarguts' Date 第K短路

poj 2449 Remmarguts' Date (k短路模板)