uva 10740 Not the Best (最短路 A*算法)
来源:互联网 发布:西安爱知 编辑:程序博客网 时间:2024/06/06 01:44
uva 10740 Not the Best
题目大意:求第K短路。
解题思路:A*算法。这题数据量比较小,也可以暴力。
A*算法
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace std;typedef long long ll;const int N = 105;const int INF = 0x3f3f3f3f;int n, m;int s, t, K;int h[N]; bool vis[N];struct Node { int to, dis;};int gra[N][N], gra2[N][N];bool operator < (const Node& a, const Node& b) { return (a.dis + h[a.to] > b.dis + h[b.to]);}void init() { memset(vis, 0, sizeof(vis)); for(int i = 0; i <= n + 1; i++) h[i] = INF; h[t] = 0; for(int i = 1; i <= n; i++) { int x, m = INF; for(int j = 1; j <= n; j++) { if(!vis[j] && m > h[j]) { m = h[j]; x = j; } } vis[x] = true; for (int j = 1; j <= n; j++) { if (!vis[j] && gra2[x][j] + h[x] < h[j]) { h[j] = gra2[x][j] + h[x]; } } }}int Astar() { priority_queue<Node> Q; init(); if (s == t) K++; if (h[s] == INF) return -1; int ans = -1, count = 0; Node p; p.to = s; p.dis = 0; Q.push(p); while(!Q.empty()) { p = Q.top(); Q.pop(); if (p.to == t) count++; if (count == K) { ans = p.dis; break; } for (int i = 1; i <= n; i++) { if (gra[p.to][i] != INF) { Q.push((Node){i, p.dis + gra[p.to][i]}); } } } return ans;}void input() { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { gra[i][j] = INF; gra2[i][j] = INF; } } scanf("%d %d %d", &s, &t, &K); int u, v, d; for (int i = 0; i < m; i++) { scanf("%d %d %d", &u, &v, &d); gra[u][v] = min(gra[u][v], d); gra2[v][u] = min(gra2[v][u], d); }}int main() { while (scanf("%d %d", &n, &m) == 2) { if (n == 0 && m == 0) break; input(); printf("%d\n", Astar()); } return 0;}
暴力
#include<cstdio> #include<algorithm> #include<vector> #include<cstring> #include<stack> #include<iostream> #include<queue> using namespace std; const int N = 105; int n, m; int s, t, k; struct Edge{ int to, dis; }; vector<Edge> G[N]; int vis[N]; int BFS(){ int ans = -1; priority_queue<pair<int, int> > q; memset(vis, 0, sizeof vis); q.push(make_pair(0, s)); while(!q.empty()){ pair<int, int> p = q.top(); q.pop(); int dist = -p.first; int pos = p.second; if(vis[pos] > k) continue; vis[pos]++; if(pos == t && vis[pos] == k){ ans = dist; break; } for(int i = 0;i < G[pos].size();i++){ int to = G[pos][i].to; int dis = G[pos][i].dis; q.push(make_pair(-(dis + dist), to)); } } return ans;} int main(){ while(scanf("%d%d", &n, &m)){ if(n == 0 && m == 0) break; scanf("%d%d%d", &s, &t, &k); for(int i = 0; i <= n; i++) G[i].clear(); for (int i = 0; i < m; i++) { int u, v, d; scanf("%d%d%d", &u, &v, &d); G[u].push_back((Edge){v, d}); } printf("%d\n", BFS()); } return 0; }
0 0
- uva 10740 Not the Best (最短路 A*算法)
- (intermediate) 最短路(第k短路) UVA 10740 - Not the Best
- uva 10740 - Not the Best(第k短路)
- hdu2680 Choose the best route 最短路(Dijkstra算法)
- 【最短路】LightOJ 1099 - Not the Best 第二短路
- HDU2680 Choose the best route (最短路)(Dijkstra算法)
- HDU - 2680 - Choose the best route (经典最短路问题dijkstra算法!!)
- hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)
- hdu 2680 Choose the best route (dijkstra算法 最短路问题)
- HDU 2680 Choose the best route 最短路(Dijkstra算法)
- (beginer) 最短路 UVA 658 It\'s not a Bug, it\'s a Feature!
- UVA 658 It's not a Bug, it's a Feature!(状压+隐式图+最短路)
- 【UVa】10917 A Walk Through the Forest 最短路+DP
- LightOj 1099 Not the Best(次短路)
- lightoj 1099 - Not the Best(次短路)
- hdu Choose the best route(最短路 --- Dijkstra)
- HDU 2680Choose the best route(dijk最短路)
- HDOJ 2680 Choose the best route(最短路,dijkstra)
- 零基础学python-11.2 python语法规则
- Mac OS X中MacPorts安装和使用
- 数据结构之链表
- 标准的ActionRecord效验错误信息
- Python中模块和包的概念
- uva 10740 Not the Best (最短路 A*算法)
- 数组排序
- 数据结构之栈
- 1.OA系统概述
- Java中隔离容器的实现
- 使用活动条ActionBar---实现Tab以及下拉式导航---导航方便你我他
- 指针与数组
- vs2013 c++ ATL 制作COM组件
- 对数组名取地址得到的是什么?