题目链接:

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. webservice简介以及简单使用

    本文主要是简单的介绍webservice是什么.webservice的由来.webservice的作用,以及后面会介绍webservice的使用. webservice是什么? 目前,Web serv ...

  2. sublime简单配置

    Preferences------->settings user { "font_face": "Courier New", "font_siz ...

  3. 重拾C,一天一点点_10

    来博客园今天刚好两年了,两年前开始学编程. 忙碌近两个月,项目昨天上线了,真心不容易,也不敢懈怠,接下来的问题会更多.这两天调试服务器,遇到不少麻烦. 刚出去溜达了一下,晚上天凉了,现在手感觉凉的有点 ...

  4. PhpStrom 配置Xdebug

    1 到 http://xdebug.org/download.php下载xdebug.注意找到自己对应的php版本.或者可以通过 http://xdebug.org/wizard.php页面,将php ...

  5. a标签至于flash之上的时候,IE浏览器无法点击连接的问题

    <a style="display: block;height: 131px;position: absolute;z-index: 999;width: 222px;backgrou ...

  6. MIME类型

    多用途互联网邮件扩展(MIME,Multipurpose Internet Mail Extensions)是一个互联网标准,它扩展了电子邮件标准,使其能够支持非ASCII字符.二进制格式附件等多种格 ...

  7. OJ推荐【转】

    来自:http://blog.csdn.net/zdp072/article/details/16207111 一. Online Judge简介: Online Judge系统(简称OJ)是一个在线 ...

  8. 新浪博客如何显示高亮代码,DIY

    新浪博客对代码的支持功能不尽完美,或者说一点都不好,可是对于一个追求完美的技术痴而言,代码不能够完美的显示,心里总有那么一些不爽,那么如何在新浪中显示那些带颜色的代码呢?经过探究,可以如下设置:   ...

  9. Hadoop伪分布式搭建CentOS

    所需软件及版本: jdk-7u80-linux-x64.tar.gz hadoop-2.6.0.tar.gz 1.安装JDK Hadoop 在需在JDK下运行,注意JDK最好使用Oracle的否则可能 ...

  10. IOS下载资源zip到本地然后读取

    思路是 1.ios下载服务器上的zip资源包(图片,声音等经过zip压缩的资源包)到本地 2.解压zip到程序目录 3.从程序目录加载资源文件 一.下载zip资源 [cpp]-(NSString*)D ...