Problem  UVA - 11478 - Halum

Time Limit: 3000 mSec

Problem Description

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 directe

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

题解:二分+差分约束系统,解决方法很固定,跑个spfa判负环就能判断差分约束系统是否有解,这个题是让最小边权最大,所以显然二分,还加了一点说是要边权大于0,所以先判断最小边权是1行不行,不行的话显然就是无解得情况,再判断最小边权是所有边中最大的+1行不行,如果可以,说明根本就没有回路,这时最小权值想多大就多大(让入度为0的点增加INF),所以是无穷大的情况,除此之外就是二分了,没什么难度。还有个地方需要说明一下,从代码可以看出这和普通的spfa有一点区别就是没有源点,看似是个大问题,其实很容易理解,差分约束系统其实一般是要添加一个源点的,从源点到所有点的距离都是0,这里省略了加点,加边的操作,直接将第一次松弛后的状态作为初始状态,这显然是可以的,因为即便加边也是加的从源点到各个点的有向边,第一次松弛结束后不可能再通过源点松弛,所以直接省略源点即可。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct Edge
{
int to, next, w;
} edge[maxm]; int n, m, st;
int head[maxn], tot; void init()
{
memset(head, -, sizeof(head));
tot = ;
} void AddEdge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
} int dist[maxn], cnt[maxn];
bool vis[maxn]; bool spfa()
{
queue<int> que;
memset(cnt, , sizeof(cnt));
for(int i = ; i < n; i++)
{
que.push(i);
dist[i] = ;
vis[i] = true;
}
while (!que.empty())
{
int u = que.front();
que.pop();
vis[u] = false;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] > dist[u] + edge[i].w)
{
dist[v] = dist[u] + edge[i].w;
if (!vis[v])
{
vis[v] = true;
que.push(v);
if (++cnt[v] > n)
{
return false;
}
}
}
}
}
return true;
} bool Judge(int lim)
{
for (int i = ; i < m; i++)
{
edge[i].w -= lim;
}
bool ok = spfa();
for (int i = ; i < m; i++)
{
edge[i].w += lim;
}
return ok;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
while (cin >> n >> m)
{
init();
int u, v, d;
int le = , ri = ;
for (int i = ; i < m; i++)
{
cin >> u >> v >> d;
u--, v--;
AddEdge(u, v, d);
ri = max(ri, d);
} if(!Judge(le))
{
cout << "No Solution" << endl;
continue;
}
else if(Judge(ri + ))
{
cout << "Infinite" << endl;
continue;
} int ans = -INF;
while (le <= ri)
{
int mid = (le + ri) / ;
if (Judge(mid))
{
le = mid + ;
ans = mid;
}
else
{
ri = mid - ;
}
}
cout << ans << endl;
}
return ;
}

UVA - 11478 - Halum(二分+差分约束系统)的更多相关文章

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

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

  2. UVA 11374 Halum (差分约束系统,最短路)

    题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...

  3. UVA 11478 Halum(差分约束)

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

  4. UVA - 11090 - Going in Cycle!!(二分+差分约束系统)

    Problem  UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...

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

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

  6. Uva 11478 Halum操作

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

  7. UVA-11478 Halum (差分约束系统)

    题目大意:一张n个节点的有向带边权图,每次操作能任选一个节点v个一个整数d,使以v为终点的边权值都减少d,以v为起点的边权值都增加d,求若干次操作后的最小边权值的非负最大值. 题目分析:用sum[i] ...

  8. UVA 11478 Halum (差分约束)

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

  9. UVA 11478 Halum

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

随机推荐

  1. AndroidStudio意外崩溃,电脑重启,导致重启Find In Path...查找功能失效,搜索结果缺失

    解决方案: <AndroidStudio意外崩溃,电脑重启,导致重启打开Androidstudio后所有的import都出错>

  2. SpringBoot入门教程(七)整合themeleaf+bootstrap

    Thymeleaf是用于Web和独立环境的现代服务器端Java模板引擎.Thymeleaf的主要目标是将优雅的自然模板带到您的开发工作流程中—HTML能够在浏览器中正确显示,并且可以作为静态原型,从而 ...

  3. 【c#】RabbitMQ学习文档(四)Routing(路由)

    (使用Net客户端) 在上一个教程中,我们构建了一个简单的日志系统,我们能够向许多消息接受者广播发送日志消息. 在本教程中,我们将为其添加一项功能 ,这个功能是我们将只订阅消息的一个子集成为可能. 例 ...

  4. OI回忆录?

    序 一直觉得,回忆录这种东西是签了清北约进了集训队的大爷们站在金字塔的顶端带着一丝轻描淡写的忧愁说一句"退役了啊~",对于我这种省选就退役的人好像没什么资格写这种东西.可还是敲起了 ...

  5. 在windows中创建.gitignore文件

    1.先任意创建一个文件,例如:1.txt 2.打开cmd命令行窗口,到1.txt目录下 windows7/8输入ren 1.txt .gitignore修改成功 windows10输入mv 1.txt ...

  6. MySQL递归查询_函数语法检查_GROUP_CONCAT组合结果集的使用

    1-前言: 在Mysql使用递归查询是很不方便的,不像Sqlserver可以直接使用声明变量,使用虚拟表等等.如:DECLARE,BEGIN ...  END   ,WHILE ,IF 等等. 在My ...

  7. SQL Server表名为添加中括号[]执行出错

    执行SQL语句: Update Check Set EOBTypeID=102 where E0BID='123344' 结果竟然报错,给表名添加中括号,写成这样: Update [Check] Se ...

  8. WPF 语言格式化文本控件

    前言 本章讲述正确添加语言资源的方式,以及一段语言资源的多种样式显示. 例如:“@Winter,你好!感谢已使用软件 800 天!” 在添加如上多语言资源项时,“XX,你好!感谢已使用软件 X 天!” ...

  9. java学习笔记 线程的实现与同步

    2019.4.2 线程实现的两种方式 继承线程,复写其中的run方法 实现runnable接口,复写run方法 使用: MyThread target = new MyThread(); new Th ...

  10. C# 消息队列-Microsoft Azure service bus 服务总线

    先决条件 Visual Studio 2015或更高版本.本教程中的示例使用Visual Studio 2015. Azure订阅. 注意 要完成本教程,您需要一个Azure帐户.您可以激活MSDN订 ...