Uva 11478 Halum(差分约束系统 + 二分)
来源:互联网 发布:linux中expect 编辑:程序博客网 时间:2024/05/13 13:37
题意:一个有向图,每条边有权值,你可以每次选择一个点v和一个整数d,所有以v为起点的边的权值增加d,以v为终点的权值减小d,问是否能让所有边权为正数
思路:二分答案ans,设有边(a, b),对a操作的总和为sum(a),对b操作的总和为sum(b),w(a, b)变为了w(a, b) + sum(a) - sum(b),可知对所有边:w(a, b) + sum(a) - sum(b) >= ans,移项得sum(b) <= sum(a) + w(a, b) - ans,这是一个差分约束系统,在建立一个源点到所有点的距离为0,之后spfa判断是否有负环并二分查找,可得出结果
#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<iostream>#include<algorithm>const int maxn = 1010;const int INF = 1e9;const int inf = 1e5 + 1e4;using namespace std;struct P { int to, cost; P(int t, int c) : to(t), cost(c) {}};int n, m, u, v, dis;int vis[maxn], cnt[maxn];int d[maxn];vector<P> G[maxn];bool spfa(int from, int x) { fill(d, d + maxn, INF); memset(vis, 0, sizeof(vis)); memset(cnt, 0, sizeof(cnt)); d[from] = 0; vis[from] = 1; queue<int> q; q.push(from); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for(int i = 0; i < G[u].size(); i++) { P st = G[u][i]; int v = st.to, w = 0; if(u) w = st.cost - x; if(d[v] <= d[u] + w) continue; d[v] = d[u] + w; if(vis[v]) continue; q.push(v); vis[v] = 1; if(++cnt[v] > n) return true; } } return false;}int bsearch(int l, int r) { while(l < r) { int mid = l + (r - l + 1) / 2; bool hv = spfa(0, mid); if(hv) r = mid - 1; else l = mid; } return l;}int main() { while(scanf("%d %d", &n, &m) != EOF) { for(int i = 0; i < maxn; i++) G[i].clear(); while(m--) { scanf("%d %d %d", &u, &v, &dis); G[u].push_back(P(v, dis)); } for(int i = 1; i <= n; i++) { G[0].push_back(P(i, 0)); } int ans = bsearch(0, inf); if(!ans) printf("No Solution\n"); else if(ans >= inf) printf("Infinite\n"); else printf("%d\n", ans); } return 0;}
0 0
- Uva 11478 Halum(差分约束系统 + 二分)
- UVA 11478V Halum 二分答案+差分约束系统
- UVA 11478 Halum(二分 + 差分约束)
- UVA - 11478 Halum(差分约束系统)
- UVA 11478 Halum(差分约束系统+Bellman-Ford)
- UVA 11478 Halum(差分约束)
- uva 11478 Halum (差分约束+二分+SPFA判负环)
- UVA 11478 Halum 二分+差分约束+SPFA
- UVA 11478 - Halum 差分约束
- uva 11478 Halum(图论-差分约束)
- 差分约束(Halum操作,UVA 11478)
- UVA 11478 Halum(差分约束 SPFA判负环)
- 11478 - Halum(差分约束)
- UVA11478 [Halum] 二分答案+SPFA差分约束系统
- uva11478 - Halum 二分+差分约束
- uva11478 Halum【二分+差分约束】
- UVA 11478 Halum(差分约束,5级)
- UVA 11478 - Halum(差分约束+最短路)
- iOS富文本设置样式,插入图片,点击事件
- 在多线程情况下,block比delegate有优势。
- hihocoder 1384 Genius ACM
- Leetcode -- Verify Preorder Serialization of a Binary Tree
- PCB课程设计5
- Uva 11478 Halum(差分约束系统 + 二分)
- Address already in use: JVM_Bind(端口冲突)
- ElasticSearch windows 单机多节点
- 伸展树splay之求区间极值
- kiwi简介
- RecyclerView使用问题集合http://www.jianshu.com/p/333fe22cabc6
- Linux信号系统详解
- 基于TextRank的中文摘要抽取算法(一)
- 对象 数组及表格处理