Halum

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVA. Original ID: 11478
64-bit integer IO format: %lld      Java class name: Main

You are given a directed graph G(V,E) with a set of vertices and edges. Each edge (i,j) that connects some vertex i to vertex j has an integer cost associated with that edge.

Define the operation Halum(v, d) to operate on a vertex v using an integer d as follows: subtract d from the cost of all edges that enter v and add d to the cost of every edge that leaves v.

As an example of that operation, consider graph G that has three vertices named (1, 2, 3) and two edges. Edge (1, 2) has cost -1, and edge (2,3) has cost 1. The operation Halum(2,-3) operates on edges entering and leaving vertex 2. Thus, edge (1, 2) gets cost -1-(-3)=2 and the edge (2, 3) gets cost 1 + (-3) = -2.

Your goal is to apply the Halum function to a graph, potentially repeatedly, until every edge in the graph has at least a certain cost that is greater than zero. You have to maximize this cost.

Input

Two space-separated integers per case: V(V≤500) and E(E≤2700). E lines follow. Each line represents a directed edge using three space-separated integers (u, v, d). Absolute value of cost can be at most 10000.

Output

If the problem is solvable, then print the maximum possible value. If there is no such solution print “No Solution”. If the value can be arbitrary large print “Infinite”

Sample Input

2 1
1 2 10
2 1
1 2 -10
3 3
1 2 4
2 3 2
3 1 5
4 5
2 3 4
4 2 5
3 4 2
3 1 0
1 2 -1

Sample Output

Infinite
Infinite
3
1

解题:差分约束

 #include <cstdio>
#include <deque>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = ;
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[];
int head[maxn],tot,n,m;
void add(int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
int d[maxn],cnt[maxn];
bool in[maxn];
bool spfa(int x) {
deque<int>q;
for(int i = ; i <= n; ++i) {
cnt[i] = ;
d[i] = ;
in[i] = true;
q.push_back(i);
}
while(!q.empty()) {
int u = q.front();
q.pop_front();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next) {
int tmp = e[i].w - x;
if(d[e[i].to] > d[u] + tmp) {
d[e[i].to] = d[u] + tmp;
if(!in[e[i].to]) {
if(++cnt[e[i].to] > n) return false;
in[e[i].to] = true;
if(!q.empty() && d[q.front()] > d[e[i].to])
q.push_front(e[i].to);
else q.push_back(e[i].to);
}
}
}
}
return true;
}
int main() {
int u,v,w;
while(~scanf("%d%d",&n,&m)) {
memset(head,-,sizeof head);
int low = ,high = ;
for(int i = tot = ; i < m; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
high = max(high,w);
}
if(!spfa()) puts("No Solution");
else if(spfa(high+)) puts("Infinite");
else {
int ret;
while(low <= high) {
int mid = (low + high)>>;
if(spfa(mid)) {
ret = mid;
low = mid+;
} else high = mid - ;
}
printf("%d\n",ret);
}
}
return ;
}

UVA 11478 Halum的更多相关文章

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

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

  2. UVA 11478 Halum (差分约束)

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

  3. Uva 11478 Halum操作

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

  4. UVA - 11478 Halum 二分+差分约束

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 题意: 给定一个有向图,每一条边都有一个权值,每次你可以 ...

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

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

  6. UVA 11478 Halum(差分约束)

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

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

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

  8. 【Halum操作-UVA 11478】

    ·英文题,述大意:      输入有向图一个(什么边的端点啊,边权啊).每次可以选择一个节点和一个整数,然后把这个结点的出边边权加上该整数,入边边权减去该整数,目标:使得所有边的最小值非负且尽量大. ...

  9. Halum UVA - 11478(差分约束 + 二分最小值最大化)

    题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的边的权值增加d,最后要让所有边权的最小值非负且尽量大 两个特判 1 ...

随机推荐

  1. Guava工具类

    原文链接:http://blog.csdn.net/mnmlist/article/details/53425865 Objects类 Objects类有几个比较不错的方法,toString.hash ...

  2. 关于工作,学习中定时备份的几个方法(cron,git,mail)

    首先介绍一下cron这个定时备份的工具: crontab -e : 运行文字编辑器来设定时程表,内定的文字编辑器是 VI.假设你想用别的文字编辑器.则请先设定 VISUAL 环境变数来指定使用那个文字 ...

  3. 【C/C++多线程编程之十】pthread线程私有数据

    多线程编程之线程私有数据      Pthread是 POSIX threads 的简称.是POSIX的线程标准.         线程同步从相互排斥量[C/C++多线程编程之六]pthread相互排 ...

  4. Keyboard的显示与隐藏

    一个控制键盘显示与隐藏的工具类分享给大家 public class KeyBoardTool { /** * 假设输入法在窗体上已经显示.则隐藏.反之则显示 * @param context */ p ...

  5. Windows 8.1内置微软五笔输入法

    微软五笔输入法採用86版编码,不是Windows 8.1系统的中文语言的缺省输入法,你在使用它之前须要把它加入到系统输入法中. 在控制面板双击"",然后加入微软五笔输入法. wat ...

  6. 微信小程序怎么获取当前页面的url

    使用getCurrentPages可以获取当前加载中所有的页面对象的一个数组,数组最后一个就是当前页面. var pages = getCurrentPages() //获取加载的页面 var cur ...

  7. 2015.04.23,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 33

    1.eat, drink, and be merry 拉丁动词vivo(to live),加上名词vita(life),是许多重要英文词汇的来源. convivo是拉丁动词to live togeth ...

  8. Mac OS下PHP开发环境的搭建——基于XAMPP和IntelliJ IDEA

    简单记录一下在MacOS下,搭建PHP的开发环境吧.其实,从本质上来说,Mac对于PHP的支持还是很好的,默认带了PHP和Apache,但是由于前期对系统本身不熟悉,所以还是略微走了一些弯路--也就是 ...

  9. 不使用Store安装WSL

    Windows Store经常会因为各种原因打不开, 这时候我们可以尝试直接下载安装WSL     1. PowerShell里运行下载: PS C:\WINDOWS\system32> Inv ...

  10. ubuntu安装之后root用户配置

    安装ubuntu之后发现不切换到root显示:su: Authentication failure   需要进行一下操作   表示成功切换到root用户