POJ 1724 ROADS bfs || dfs || A*
来源:互联网 发布:笔记本风扇调节软件 编辑:程序博客网 时间:2024/06/07 01:36
题目:
http://poj.org/problem?id=1724
题意:
有
思路:
直接
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 100 + 10, M = 10000 + 10, INF = 0x3f3f3f3f;struct edge{ int to, cost, price, next;}g[M];int cnt, head[N];int ans;bool vis[N];void add_edge(int v, int u, int c, int p){ g[cnt].to = u, g[cnt].cost = c, g[cnt].price = p, g[cnt].next = head[v], head[v] = cnt++;}void dfs(int v, int t, int cost, int price, int k){ if(v == t) { ans = min(ans, cost); return; } for(int i = head[v]; i != -1; i = g[i].next) { int u = g[i].to; if(! vis[u]) { vis[u] = true; if(price + g[i].price <= k && cost + g[i].cost < ans) dfs(u, t, cost + g[i].cost, price + g[i].price, k); vis[u] = false; } }}int main(){ int k, n, m; while(~ scanf("%d%d%d", &k, &n, &m)) { cnt = 0; memset(head, -1, sizeof head); int v, u, c, p; for(int i = 1; i <= m; i++) { scanf("%d%d%d%d", &v, &u, &c, &p); add_edge(v, u, c, p); } memset(vis, 0, sizeof vis); ans = INF; vis[1] = true; dfs(1, n, 0, 0, k); if(ans == INF) ans = -1; printf("%d\n", ans); } return 0;}
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int N = 100 + 10, M = 10000 + 10, INF = 0x3f3f3f3f;struct edge{ int to, cost, price, next;}g[M];struct node{ int x, cost, price; friend bool operator< (node a, node b) { return a.cost > b.cost; }};int cnt, head[N];void add_edge(int v, int u, int c, int p){ g[cnt].to = u, g[cnt].cost = c, g[cnt].price = p, g[cnt].next = head[v], head[v] = cnt++;}int dijkstra(int s, int t, int k){ priority_queue<node> que; node e, p; e.x = s, e.cost = 0, e.price = 0; que.push(e); while(! que.empty()) { p = que.top(); que.pop(); if(p.x == t) return p.cost; for(int i = head[p.x]; i != -1; i = g[i].next) { if(p.price + g[i].price <= k) { e.x = g[i].to, e.cost= p.cost + g[i].cost, e.price = p.price + g[i].price; que.push(e); } } } return -1;}int main(){ int n, m, k; while(~ scanf("%d%d%d", &k, &n, &m)) { cnt = 0; memset(head, -1, sizeof head); int v, u, c, p; for(int i = 1; i <= m; i++) { scanf("%d%d%d%d", &v, &u, &c, &p); add_edge(v, u, c, p); } printf("%d\n", dijkstra(1, n, k)); } return 0;}
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;typedef pair<int,int> pii;const int N = 100 + 10, M = 20000 + 10, INF = 0x3f3f3f3f;struct edge{ int to, cost, price, next;}g[M];struct node{ int v, g, h, price; friend bool operator< (node a, node b) { if(a.g + a.h != b.g + b.h) return a.g + a.h > b.g + b.h; else return a.g > b.g; }};int cnt, head[N], rhead[N];int dis[N];bool vis[N];void add_edge(int v, int u, int c, int p){ g[cnt].to = u, g[cnt].cost = c, g[cnt].price = p, g[cnt].next = head[v], head[v] = cnt++;}void radd_edge(int v, int u, int c, int p){ g[cnt].to = u, g[cnt].cost = c, g[cnt].price = p, g[cnt].next = rhead[v], rhead[v] = cnt++;}void dijkstra(int s, int t){ priority_queue<pii, vector<pii>, greater<pii> > que; memset(dis, 0x3f, sizeof dis); memset(vis, 0, sizeof vis); que.push(pii(0, s)), dis[s] = 0; while(! que.empty()) { int v = que.top().second; que.pop(); if(vis[v]) continue; vis[v] = true; for(int i = rhead[v]; i != -1; i = g[i].next) { int u = g[i].to; if(dis[u] > dis[v] + g[i].cost) { dis[u] = dis[v] + g[i].cost; que.push(pii(dis[u], u)); } } }}int a_star(int s, int t, int k){ priority_queue<node> que; node e, p; e.v = s, e.g = 0, e.h = dis[s], e.price = 0; que.push(e); while(! que.empty()) { p = que.top(); que.pop(); if(p.v == t) { if(p.price <= k) return p.g; } for(int i = head[p.v]; i != -1; i = g[i].next) { e.v = g[i].to, e.g = p.g + g[i].cost, e.h = dis[g[i].to], e.price = p.price + g[i].price; if(e.price > k) continue; //剪枝 que.push(e); } } return -1;}int main(){ int k, n, m; while(~ scanf("%d%d%d", &k, &n, &m)) { cnt = 0; memset(head, -1, sizeof head); memset(rhead, -1, sizeof rhead); int v, u, c, p; for(int i = 1; i <= m; i++) { scanf("%d%d%d%d", &v, &u, &c, &p); add_edge(v, u, c, p); radd_edge(u, v, c, p); } dijkstra(n, 1); int ans = a_star(1, n, k); printf("%d\n", ans); } return 0;}
阅读全文
0 0
- POJ 1724 ROADS bfs || dfs || A*
- POJ 1724 ROADS (bfs)
- poj-1724-ROADS(dfs)
- POJ ROADS DFS BFS 优先队列 入门
- poj 1724 ROADS(dfs)
- POJ 1724 ROADS DFS+剪枝
- POJ 1724 ROADS 启发式DFS
- POJ 1724 ROADS【DFS+剪枝】
- POJ 1724 ROADS (DFS)
- POJ 1724 ROADS (有限制的最短路径DFS/BFS)
- POJ 1724 ROADS (优先队列+BFS)
- POJ 1724 ROADS(bfs最短路)
- POJ - 1724 ROADS(bfs+剪枝)
- POJ 1724 ROADS(BFS + 优先队列)
- POJ 1724 ROADS(DFS剪枝)
- [POJ 1724]ROADS[SPFA][DFS剪枝]
- (POJ 1724)ROADS <DFS + 简单剪枝>
- ROADS poj 1724(dfs + 剪枝)
- JavaScript中的值传递
- ajax实现动态加载组合框
- poj 3252 Round Numbers
- 响应式导航(从水平到垂直)的分析与实现
- hdu1072翻译火星文(未解决)
- POJ 1724 ROADS bfs || dfs || A*
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- 与Swagger集成的三种方式
- oracle11g dataguard 完全手册
- Django学习 (6):搭建简易博客
- 【leetcode】第38题 Count and Say 题目+解析+代码
- 《Drools7.0.0.Final规则引擎教程》第4章 Query查询之基础
- TensorFlow学习笔记(二十八)CNN的9大模型之AlexNet
- Ubuntu16.04下安装Truffle和TestRPC