题目链接:

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 二分+差分约束的更多相关文章

  1. UVA 11478 Halum(差分约束)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 [思路] 差分约束系统. 设结点u上的操作和为sum[u] ...

  2. 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)

    layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...

  3. UVA - 11478 - Halum(二分+差分约束系统)

    Problem  UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...

  4. UVA 11478 Halum(用bellman-ford解差分约束)

    对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...

  5. UVA 11478 Halum

    Halum Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 114 ...

  6. POJ1275 Cashier Employment 【二分 + 差分约束】

    题目链接 POJ1275 题解 显然可以差分约束 我们记\(W[i]\)为\(i\)时刻可以开始工作的人数 令\(s[i]\)为前\(i\)个时刻开始工作的人数的前缀和 每个时刻的要求\(r[i]\) ...

  7. UVA11478 Halum (差分约束)

    每次操作是独立的,而且顺序并不影响,作用在同一个结点上的d可以叠加,所以令x(u) = sigma(dui). 最后就是要确定所有的x(u). 因为m越大,满足条件的边就越少,二分答案m. 对于一条边 ...

  8. UVA 11478 Halum (差分约束)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. Uva 11478 Halum操作

    题目链接:http://vjudge.net/contest/143318#problem/B 题意:给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权 ...

随机推荐

  1. 最新百度音乐api

    一直都想做网络音乐播放器,但是自己又没有服务器,根本就不能实现,也没那个能力实现.唯一的办法就是借助别人的API. 网上公布的API特别少,像能够直接得到音乐文件的真是地址的几乎没有,有的也只是截取流 ...

  2. CentOS下修复grub引导程序

    1.早上装系统,试了好几次.最终grub修复成功! 解决方法大致如下: 开机遇见 grub>的时候 下面看解决方法 首先是既然插上U盘能进系统  那我们插上U盘 正常进入系统  然后看操作 [r ...

  3. 刚开始学IOS遇到的类和方法

    框架:Core FoundationCFGetRetainCount. 类:NSRunLoop.NSAutoreleasePool.NSStringFormClass.UIApplicationMai ...

  4. Outlook 2007无法打开链接"由于本机的限制 该操作已被取消"

    编写人:CC阿爸 2014-2-17 近来在日常维护中,经常性的遇到用户在outlook中打开链接,提示[由于本机的限制,该操作已被取消],第一次的在网上搜索到解决办法后, 第二次再处理时,又没能记住 ...

  5. luigi学习6--parameters详解

    parameter就好比是一个task的构造方法.luigi要求你在类的scope上定义parameter. 如下面就是一个定义parameter的例子: class DailyReport(luig ...

  6. Eval is Devil-MongoDB master/slave上运行Eval遇到的问题

    随便写一句,以免有跟我一样的人遇到这个问题. 驱动版本:MongoDB C# Driver 1.7.0 当在Master/Slave集群上使用Eval的时候,Eval操作只会在Master结点上运行, ...

  7. sql,插入最大值加1

    insert into aa (id,name) values((select case when max(id) is null then 1 else max(id)+1 end from aa) ...

  8. jQuery操作 input type=checkbox的实现代码

    代码如下: <input type="checkbox">: 2012欧洲杯"死亡之组"小组出线的国家队是:<br> <input ...

  9. WCF全面解析第一章 WCF 简介

    1.WCF中的 "A","B","C" 介绍 我们先看个生活中的例子,某一天,公司的领导让你去送一份合同文件,送文件的过程你可以选择的交通方 ...

  10. ok6410的DMA裸机总结

    1.为何使用DMA:为了提高CPU的工作效率,避免多余的等待时间 2.关于DMA控制器:(1)通道数:2440有4个通道,6410有4个DMA控制器(初始化的时候要选择),32个通道.210有两种DM ...