UVA - 11478 - Halum(二分+差分约束系统)
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
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(二分+差分约束系统)的更多相关文章
- UVA - 11478 Halum 二分+差分约束
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 题意: 给定一个有向图,每一条边都有一个权值,每次你可以 ...
- UVA 11374 Halum (差分约束系统,最短路)
题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
- UVA 11478 Halum(差分约束)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 [思路] 差分约束系统. 设结点u上的操作和为sum[u] ...
- UVA - 11090 - Going in Cycle!!(二分+差分约束系统)
Problem UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...
- UVA 11478 Halum(用bellman-ford解差分约束)
对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...
- Uva 11478 Halum操作
题目链接:http://vjudge.net/contest/143318#problem/B 题意:给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权 ...
- UVA-11478 Halum (差分约束系统)
题目大意:一张n个节点的有向带边权图,每次操作能任选一个节点v个一个整数d,使以v为终点的边权值都减少d,以v为起点的边权值都增加d,求若干次操作后的最小边权值的非负最大值. 题目分析:用sum[i] ...
- UVA 11478 Halum (差分约束)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11478 Halum
Halum Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 114 ...
随机推荐
- Java开发需掌握的常用Linux命令(持续更新)
linux命令是对Linux系统进行管理的命令.对于Linux系统来说,无论是中央处理器.内存.磁盘驱动器.键盘.鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命 ...
- 微信小程序与AspNetCore SignalR聊天实例
微信小程序与aspnetcore signalr实例 本文不对小程序与signalr做任何介绍,默认读者已经掌握 aspnetcore Signalr文档 小程序文档 写在之前 SignalR没有提供 ...
- [开源]快速构建一个WebApi项目
项目代码:MasterChief.DotNet.ProjectTemplate.WebApi 示例代码:https://github.com/YanZhiwei/MasterChief.Project ...
- SpringCloud Config客户端
SpringCloud Config服务端 1.导入依赖 <dependency> <groupId>org.springframework.cloud</groupI ...
- [六] 函数式接口的复合方法示例 predicate 谓词逻辑运算 Function接口 组合运算 比较器 逆序 比较链
复合的方法 有些函数式接口提供了允许复合的方法 也就是可以将Lambda表达式复合成为一个更加复杂的方法 之前的章节中有说到: 接口中的compose, andThen, and, or, negat ...
- DSAPI多功能组件编程应用-图形图像篇(中)
[DSAPI.DLL下载地址] 说到计算机上使用代码来处理各种图像特效,是一份太有挑战性的工作.以下涉及的所有图像效果均不是从网上复制的源码,而是本人试验数次并编写的,所以原理上会和网上的有所不同 ...
- vscode 输出乱码
文件-首选项-设置,添加如下配置: "code-runner.runInTerminal": true,
- Solr学习笔记---部署Solr到Tomcat上,可视化界面的介绍和使用,Solr的基本内容介绍,SolrJ的使用
学习Solr前需要有Lucene的基础 Lucene的一些简单用法:https://www.cnblogs.com/dddyyy/p/9842760.html 1.部署Solr到Tomcat(Wind ...
- 熟悉常用的HBase操作,编写MapReduce作业
1. 以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据: 学生表(Student) 学号(S_No) 姓名(S_Name) 性别(S_Sex) 年龄(S_Age) 201 ...
- js获取文件后缀
//获取文件后缀 function getType(file){ var filename=file; var index1=filename.lastIndexOf("."); ...