uva10859 - Placing Lampposts
来源:互联网 发布:黑龙江省人工智能学会 编辑:程序博客网 时间:2024/06/01 10:50
题意:
有n个点m条边的无向无环图,在尽量少的点放灯,让所有边都能被照,且尽量多的边同时被找到。
思路:
题目有两个地方要控制,a:一个是放灯的点——要尽量少,b:第二个是被两盏灯照到的边—-尽量多。
学到一个小技巧,就是将第二问题转为b:被一盏灯照到的边 — 尽量少,所求即x=Ma+b最小,M为一个比a,b大得多的数。
然后就是控制x使其最小了。
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int N = 1005;const int M = 2000;int vis[N], dp[N][2];vector <int> ege[N];int n, m;void dfs(int u) { vis[u] = 1; dp[u][0] = 0;//u点不放灯 dp[u][1] = M;//u点放灯 for (int i = 0; i < ege[u].size(); i++) { int v = ege[u][i]; if (vis[v]) continue; dfs(v); dp[u][0] += dp[v][1] + 1; if (dp[v][0] < dp[v][1]) dp[u][1] += dp[v][0] + 1; else dp[u][1] += dp[v][1]; }}int main() { int cas; scanf("%d", &cas); while (cas--){ memset(dp, 0, sizeof(dp)); memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) ege[i].clear(); scanf("%d%d", &n, &m); int u, v; for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); ege[u].push_back(v); ege[v].push_back(u); } int ans = 0; for (int i = 0; i < n; i++) { dfs(i); ans += min(dp[i][0], dp[i][1]); } printf("%d %d %d\n", ans / M, m - (ans%M), ans%M); } return 0;}
0 0
- Placing Lampposts------UVA10859-----DP
- uva10859 Placing Lampposts
- Uva10859 - Placing Lampposts
- uva10859 - Placing Lampposts
- 【UVA10859】Placing Lampposts
- uva10859 - Placing Lampposts(动归)
- uva10859 - Placing Lampposts 无根树转有根树 DP
- UVA10859[Placing Lampposts] 动态规划 求值技巧
- uva10859 Placing Lampposts (树形dp+求两者最小值方法)
- UVa10859 Placing Lampposts(双元限制的dp)
- Placing Lampposts
- Placing Lampposts
- Uva-10859-Placing Lampposts
- UVA 10859 Placing Lampposts
- [动态规划]Placing lampposts
- uvaoj 10859 - Placing Lampposts
- UVa10895 Placing Lampposts
- UVa 10859 Placing Lampposts
- C++虚函数表详细解释及实例分析
- TensorFlow在图像识别中的应用
- 2的次幂表示 蓝桥杯
- javaWeb笔记(六) 分页 显示分页页码列表
- HDU-1012-u Calculate e( C++ && 2000年纽约水果)
- uva10859 - Placing Lampposts
- 虽鸡汤,但够补
- uvaoj-401:回文词
- 查看Eclipse版本号,及各个版本区别
- 蓝桥杯 历届试题 错误票据
- Ubuntu14.04 下apt-get安装Code::Blocks
- POJ 2239 Selecting Courses(二分图最大匹配)
- 多人聊天室
- 算法学社 -- "这是一道难题"第一期