[POJ 1724]ROADS[SPFA][DFS剪枝]
来源:互联网 发布:abs函数 c语言 编辑:程序博客网 时间:2024/05/30 07:14
题目链接:[POJ 1724]ROADS[SPFA][DFS剪枝]
题意分析:
求从点1到点N费用不超过K的最短路。
解题思路:
用spfa根据最小花费,跑出一组距离作为起始答案,如果该答案费用大于K,那么就无解。
确认有解后,我们从点1开始DFS,用花费和之前算的距离作为剪枝。
个人感受:
差点弃疗了。1A也是蛮爽的。
具体代码如下:
#include<algorithm>#include<cctype>#include<cmath>#include<cstdio>#include<cstring>#include<iomanip>#include<iostream>#include<map>#include<queue>#include<set>#include<sstream>#include<stack>#include<string>#define ll long long#define pr(x) cout << #x << " = " << (x) << '\n';using namespace std;const int INF = 0x3f3f3f3f;const int MAXN = 1e4 + 111;struct Edge { int to, next, l, t;}edge[MAXN];int head[111], tol = 0, n, k, ans, cost[111], dis[111];bool in[111];void add_edge(int u, int v, int l, int t) { edge[tol].to = v; edge[tol].l = l; edge[tol].t = t; edge[tol].next = head[u]; head[u] = tol++;}bool spfa() { memset(in, 0, sizeof in); memset(cost, 0x3f, sizeof cost); cost[1] = 0; dis[1] = 0; in[1] = 1; queue<int> q; q.push(1); while (q.size()) { int u = q.front(); q.pop(); in[u] = 0; for (int i = head[u]; ~i; i = edge[i].next) { int v = edge[i].to; if (cost[v] > cost[u] + edge[i].t) { cost[v] = cost[u] + edge[i].t; dis[v] = dis[u] + edge[i].l; if (!in[v]) { in[v] = 1; q.push(v); } } } } return (cost[n] <= k);}void dfs(int x, int len, int cos) { if (cos > k || len >= ans) return; if (x == n) { ans = len; return; } for (int i = head[x]; ~i; i = edge[i].next) { int v = edge[i].to; dfs(v, len + edge[i].l, cos + edge[i].t); }}int main(){ int r, u, v, l, t; scanf("%d%d%d", &k, &n, &r); memset(head, -1, sizeof head); for (int i = 0; i < r; ++i) { scanf("%d%d%d%d", &u, &v, &l, &t); add_edge(u, v, l, t); } if (!spfa()) printf("-1\n"); else { ans = dis[n]; dfs(1, 0, 0); printf("%d\n", ans); } return 0;}
0 0
- [POJ 1724]ROADS[SPFA][DFS剪枝]
- POJ 1724 ROADS DFS+剪枝
- POJ 1724 ROADS【DFS+剪枝】
- POJ 1724 ROADS(DFS剪枝)
- (POJ 1724)ROADS <DFS + 简单剪枝>
- ROADS poj 1724(dfs + 剪枝)
- Poj 1724 Roads(DFS 可行性剪枝 最优性剪枝 向量)
- POJ 1724 ROADS (搜索+剪枝)
- poj-1724-ROADS(dfs)
- poj 1724 dfs+剪枝
- POJ - 1724 ROADS(bfs+剪枝)
- poj 1724 ROADS(dfs)
- POJ 1724 ROADS 启发式DFS
- POJ 1724 ROADS (DFS)
- POJ 1724 ROADS(优先队列+spfa)
- POJ 1724 ROADS bfs || dfs || A*
- poj 1724 最短路变形 DFS+剪枝
- poj 2192 DFS+剪枝
- php向前端发送的字符串中存在HTML标签如何正常显示
- CentOS下安装JDK7
- 简洁的onchange事件动态添加input
- Crash dump中需要重点关注的信息
- extern用法:C文件和C++文件之间的函数互调
- [POJ 1724]ROADS[SPFA][DFS剪枝]
- DOMINO移动OA需求迫切,移动中间件或为最佳选择
- 屏幕截图
- 字符串分割-记录
- 数论 算法 不定方程 欧几里得 同余定理 相关知识
- [poj 3122] Pie 二分
- C\C++宏定义笔记
- Android RecyclerView 使用完全解析 体验艺术般的控件
- 添加库的路径