UVA - 11478 Halum 二分+差分约束
题目链接:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651
题意:
给定一个有向图,每一条边都有一个权值,每次你可以选择一个节点v和一个整数d,把所有以v结尾的边权值减小d,把所有以v为起点的边的权值增加d,最后要让所有边权的最小值大于0且尽量大。
题解:
最小值最大,可以用二分,这样可以得到一个差分约束系统,然后每次都用最短路跑。
代码:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<vector>
- #include<queue>
- #include<algorithm>
- using namespace std;
- const int maxn = ;
- const int maxm = ;
- struct Edge {
- int v, w;
- Edge(int v, int w) :v(v), w(w) {}
- Edge() {}
- };
- vector<Edge> egs;
- vector<int> G[maxn];
- int tot = ;
- int n, m;
- void addEdge(int u, int v, int w) {
- egs.push_back(Edge(v, w));
- tot = egs.size();
- G[u].push_back(tot - );
- }
- bool inq[maxn];
- int d[maxn];
- int cnt[maxn];
- bool spfa(int x) {
- bool ret = true;
- for (int i = ; i < n; i++) {
- for (int j = ; j < G[i].size(); j++) {
- Edge& e = egs[G[i][j]];
- e.w -= x;
- }
- }
- memset(inq, , sizeof(inq));
- memset(cnt, , sizeof(cnt));
- memset(d, 0x3f, sizeof(d));
- queue<int> Q;
- d[] = ; inq[] = true; Q.push();
- while (!Q.empty()) {
- int u = Q.front(); Q.pop();
- inq[u] = false;
- for (int i = ; i < G[u].size(); i++) {
- Edge& e = egs[G[u][i]];
- if (d[e.v] > d[u] + e.w) {
- d[e.v] = d[u] + e.w;
- if (!inq[e.v]) {
- Q.push(e.v); inq[e.v] = true;
- if (++cnt[e.v] > n) {
- ret = false; break;
- }
- }
- }
- }
- if (ret == false) break;
- //printf("u:%d\nx:%d\n", u,x);
- //printf("in circle\n");
- }
- for (int i = ; i < n; i++) {
- for (int j = ; j < G[i].size(); j++) {
- Edge& e = egs[G[i][j]];
- e.w += x;
- }
- }
- return ret;
- }
- void init() {
- for (int i = ; i < n; i++) G[i].clear();
- egs.clear();
- }
- int main() {
- while (scanf("%d%d", &n, &m) == && n) {
- n++;
- init();
- int l = , r = -;
- for (int i = ; i < m; i++) {
- int u, v, w;
- scanf("%d%d%d", &u, &v, &w);
- r = max(r, w);
- addEdge(u, v, w);
- }
- for (int i = ; i < n; i++) {
- addEdge(, i, );
- }
- r++;
- if (!spfa()) {
- printf("No Solution\n");
- }
- else if (spfa(r)) {
- printf("Infinite\n");
- }
- else {
- while (l + < r) {
- int mid = l + (r - l) / ;
- if (spfa(mid)) l = mid;
- else r = mid;
- //printf("here!\n");
- }
- printf("%d\n", l);
- }
- }
- return ;
- }
- /*
- 1
- 2 10
- 1
- 2 -10
- 3
- 2 4
- 3 2
- 1 5
- 5
- 3 4
- 2 5
- 4 2
- 1 0
- 2 -1
- */
UVA - 11478 Halum 二分+差分约束的更多相关文章
- UVA 11478 Halum(差分约束)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 [思路] 差分约束系统. 设结点u上的操作和为sum[u] ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
- UVA - 11478 - Halum(二分+差分约束系统)
Problem UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...
- UVA 11478 Halum(用bellman-ford解差分约束)
对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...
- UVA 11478 Halum
Halum Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 114 ...
- POJ1275 Cashier Employment 【二分 + 差分约束】
题目链接 POJ1275 题解 显然可以差分约束 我们记\(W[i]\)为\(i\)时刻可以开始工作的人数 令\(s[i]\)为前\(i\)个时刻开始工作的人数的前缀和 每个时刻的要求\(r[i]\) ...
- UVA11478 Halum (差分约束)
每次操作是独立的,而且顺序并不影响,作用在同一个结点上的d可以叠加,所以令x(u) = sigma(dui). 最后就是要确定所有的x(u). 因为m越大,满足条件的边就越少,二分答案m. 对于一条边 ...
- UVA 11478 Halum (差分约束)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Uva 11478 Halum操作
题目链接:http://vjudge.net/contest/143318#problem/B 题意:给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权 ...
随机推荐
- sublime text2支持ng
这里面记录了sublime text3的一些破解和sublime text2支持ng的方法. http://weblogs.asp.net/dwahlin/archive/2013/08/30/usi ...
- Windows 和 Linux 下 禁止ping的方法
Windows 和Linux 下 禁止ping的方法 目的: 禁止网络上的其他主机或服务器ping自己的服务器 运行环境: Windows 03.08 linux 方法: Windows 03下: ...
- C#winform导出数据到Excel的类
/// <summary> /// 构造函数 /// </summary> public ExportData() { } /// <summary> /// 保存 ...
- phpmyadmin误删表后的恢复过程(心惊胆跳啊)
话说今天不知道是抽风了还是失魂了,在用phpmyadmin删除测试数据时,竟然将整个表删除了: 等程序运行出错时,才出现整个表都没有了,而且之前也没有备份好!这下蛋疼了,这个可是production服 ...
- VB 进制转换大全
'二进制转十进制 Public Function B2D(vBStr As String) As Long Dim vLen As Integer '串长 Dim vDec As Long '结果 D ...
- 文件系统 第八次迭代 VFS相关说明
麻烦访问evernote链接 http://www.evernote.com/shard/s133/sh/53e5b5ac-1192-4910-8bd5-6886218562af/59516c32a5 ...
- 菜鸟学习Spring——60s让你学会动态代理原理
一.为什么要使用动态代理 当一个对象或多个对象实现了N中方法的时候,由于业务需求需要把这个对象和多个对象的N个方法加入一个共同的方法,比如把所有对象的所有方法加入事务这个时候有三种方法 ...
- SpringMvc中Interceptor拦截器用法
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆等. 一. 使用场景 1 ...
- css权重及优先级问题
css权重及优先级问题 几个值的对比 初始值 指定值 计算值 应用值 CSS属性的 指定值 (specified value)会通过下面3种途径取得: 在当前文档的样式表中给这个属性赋的值,会被优先使 ...
- C++ STL vector 内存分配
vector为了支持快速的随机访问,vector容器的元素以连续方式存放,每一个元素都紧挨着前一个元素存储. 当vector添加一个元素时,为了满足连续存放这个特性,都需要重新分配空间.拷贝元素.撤销 ...