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. Content Security Policy (CSP) 介绍

    当我不经意间在 Twitter 页面 view source 后,发现了惊喜. <!DOCTYPE html> <html lang="en"> <h ...

  2. Kafka控制器选举流程剖析

    1.概述 平时在使用Kafka的时候,可能关注的更多的是Kafka系统层面的.今天来给大家剖析一下Kafka的控制器,了解一下Kafka控制器的选举流程. 2.内容 Kafka控制器,其实就是一个Ka ...

  3. Typora - Markdown 语法说明

    Typora 是 Windows 下最好的 Markdown 编辑器!不接受反驳~ 导图 快捷键标题:Ctrl + 数字 Ctrl + 123456 代表 H1-H6 级标题Ctrl + 0 恢复普通 ...

  4. [十三]基础数据类型之AbstractStringBuilder

      String内部是一个private final char value[]; 也就意味着每次调用的各种处理方法,返回的字符串都是一个新的,性能上,显然.... 所以,对于可变字符序列的需求是很明确 ...

  5. Windows 花屏问题

    已经有2台电脑 Windows 10 系统出现花屏现象,表现为比较炫的界面出现花屏.文字显示不全.移位.闪烁等,如果点击“设置”.“开始”,Chrome浏览器等:比较平素的界面显示正常,比如资源管理器 ...

  6. 第58章 Profile Service - Identity Server 4 中文文档(v1.0.0)

    IdentityServer通常在创建令牌或处理对userinfo或内省端点的请求时需要有关用户的身份信息.默认情况下,IdentityServer仅具有身份验证cookie中的声明,以便为此身份数据 ...

  7. javascript常用的41个经典技巧

    1. 将彻底屏蔽鼠标右键 <table border oncontextmenu=return(false)><td>no</table> 可用于Table 2. ...

  8. JavaScript类型化数组(二进制数组)

    0.前言 对于前端程序员来说,平时很少和二进制数据打交道,所以基本上用不到ArrayBuffer,大家对它很陌生,但是在使用WebGL的时候,ArrayBuffer无处不在.浏览器通过WebGL和显卡 ...

  9. Net中获取程序集路径

      从内存中加载的程序集,无路径   IIS中路径 protected void Page_Load(object sender, EventArgs e) { Response.Write(&quo ...

  10. 总结:BGP和静态路由并存,达到故障自动倒换的目的。

    总体结论: 在上云的场景中,客户需要本地数据中心到云上VPC,出现网络故障时做到自动倒换,保证业务不中断. 一.客户需求 1.客户有总厂.分厂.总厂是通过专线和VPN连接上云,分厂是通过专线先连接到总 ...