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 ...
随机推荐
- SpringBoot入门教程(八)配置logback日志
Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access.logback-c ...
- SpringBoot读取yml中的配置,并分离配置文件
前言 在项目中经常遇到需要读取配置文件中的配置信息,这些配置信息之所以不写在代码中是因为实际项目发布或者部署之后会进行更改,而如果写在代码中编译之后没有办法进行修改. 之前使用的是properties ...
- 精读《useEffect 完全指南》
1. 引言 工具型文章要跳读,而文学经典就要反复研读.如果说 React 0.14 版本带来的各种生命周期可以类比到工具型文章,那么 16.7 带来的 Hooks 就要像文学经典一样反复研读. Hoo ...
- flag.xls
Topic Link http://ctf5.shiyanbar.com/misc/flag.xls 1) 打开表格发现需要密码 但是flag又在表里,但我们可以改他的格式为.txt文本,因为这样就 ...
- Python2 编码问题分析
本文浅显易懂,绿色纯天然,手工制作,请放心阅读. 编码问题是一个很大很杂的话题,要向彻底的讲明白可以写一本书了.导致乱码的原因很多,系统平台.编程语言.多国语言.软件程序支持.用户选择等都可能导致无法 ...
- [转]在node.js中,使用基于ORM架构的Sequelize,操作mysql数据库之增删改查
本文转自:https://www.cnblogs.com/kongxianghai/p/5582661.html Sequelize是一个基于promise的关系型数据库ORM框架,这个库完全采用Ja ...
- Oracle 11g设置IP访问限制
出于数据安全考虑,对Oracle数据库的IP做一些限制,只有固定的IP才能访问. 修改$ORACLE_HOME/network/ADMIN/sqlnet.ora文件 增加以下内容(红色表示注释): # ...
- Docker 安装rabbitMQ
Docker 安装rabbitMQ docker pull rabbitmq:3.7.7-management 使用:docker images 查看所有镜像 4.根据下载的镜像创建和启动容器 doc ...
- Java AQS 概述
AQS 概述 AQS(队列同步器,AbstractQueuedSynchronizer),是用来构建锁或其他同步组件的核心基础框架(比如 ReentrantLock.ReentrantReadWrit ...
- asp.net DES加密解密
数据加密标准DES加密算法是一种对称加密算法,DES 使用一个 56 位的密钥以及附加的 8 位奇偶校验位,产生最大 64 位的分组大小.这是一个迭代的分组密码,使用称为 Feistel 的技术,其中 ...