题目链接:http://vjudge.net/contest/143318#problem/B

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

分析:

最小值尽量大,二分,最大不能超过最大边,要是最大边的话,其他边满足不了非负;

题意说的各种操作,他互不影响;也就变成了操作各边。

对于各点的操作来说:

令sum(u) 是作用于 u 上的所有 d 之和;

a—> b边的权值就是: w(a,b) +sum(a) - sum(b)>=x(答案);

对上式 变形: sum(b) - sum(a) <= w(a,b) -x;

sum(b) - sum(a) 就是对这条边的操作。

这就是一个差分约束系统。

枚举这个sum(b) - sum(a) ,要是有负环,就是查分系统无解。

没有负环,说明,这个最小值还可以大一点。

#include <bits/stdc++.h>
using namespace std; const int maxn = + ; struct Edge
{
int from,to;
double dist;
}; struct BellmanFord
{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn];
double d[maxn];
int p[maxn];
int cnt[maxn]; void init(int n)
{
this->n = n;
for(int i = ; i < n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, double dist)
{
edges.push_back((Edge)
{
from, to, dist
});
m = edges.size();
G[from].push_back(m-);
} bool negativeCycle()
{
queue<int> Q;
memset(inq, , sizeof(inq));
memset(cnt, , sizeof(cnt));
for(int i = ; i < n; i++)
{
d[i] = ;
inq[] = true;
Q.push(i);
} while(!Q.empty())
{
int u = Q.front();
Q.pop();
inq[u] = false;
for(int i = ; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist)
{
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to] = true;
if(++cnt[e.to] > n) return true;
}
}
}
}
return false;
}
}; BellmanFord solver; bool test(int x)
{
for(int i=; i<solver.m; i++)
{
solver.edges[i].dist -=x;
}
bool ret = solver.negativeCycle();
for(int i=; i<solver.m; i++)
{
solver.edges[i].dist +=x;
}
return !ret;
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{ solver.init(n);
int ub = ; for(int i=; i<m; i++)
{
int u,v,dist;
scanf("%d%d%d",&u,&v,&dist);
ub = max(ub,dist);
u--;
v--;
solver.AddEdge(u,v,dist);
} if(test(ub+)) puts("Infinite");
else if(!test()) puts("No Solution");
else
{
int L = , R = ub, ans = ;
while(L <= R)
{
int M = L + (R-L)/;
if(test(M)) //没有负环
{
ans = M;
L = M+;
}
else R = M-;
}
printf("%d\n", ans);
}
}
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

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

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

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

  4. UVA 11478 Halum(差分约束)

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

  5. UVA 11478 Halum (差分约束)

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

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

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

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

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

  8. 【Halum操作-UVA 11478】

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

  9. Halum UVA - 11478 差分约束

    输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 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 ...

随机推荐

  1. imac上php环境php+apache+mysql

    ---恢复内容开始--- Mac OS X系统已预装集成了Apache+php,但是在新的系统中苹果取消了图形界面,所以只能从命令行开启了. 启用apache: 打开终端 在终端中可以查看集成的php ...

  2. Qt常用命令收集

    qt的命令很多,用到的时候到网上查,常常不能一下查到.这里记录下一些备用 1 从.ui文件生成头文件: uic xxx.ui > xxx.h 2 moc生成 moc yourfilename.h ...

  3. larave5.1l队列

    官方文档http://laravel.com/docs/5.1/queues#dealing-with-failed-jobs 1.队列容器设置为数据库 config/queue.php 'defau ...

  4. XE5 ImageList的BUG?

    今天做界面, 在imagelist里加载一个带有半透明通道的PNG图, 结果发现图片居然发暗, 如下: 原图: IDE里加载以后的图: 明显变暗...查询了源码, 无果 然后又用2010去测试, 发现 ...

  5. Geolocation

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 怎样将BigDecimal转换成Int

    BigDecimal a=new BigDecimal(12.88); int b=a.intValue(); System.out.println(b);//b=12;

  7. jedis例子

    @Test public void testDiscoverNodesAutomatically(){ Set<HostAndPort> jedisClusterNode=new Hash ...

  8. 移动端网站或APP点击后出现闪动或灰色背景(转)

    遇到这个问题了,记录下,备用~ 文章来源:http://www.lxway.com/846165591.htm --------------------------- 隐藏文本框阴影:(去除文本框默认 ...

  9. in-list expansion

    in-list expansion也被称作or expansion --针对in后面是常量集合的另外一种处理方法.优化器会把目标sql中in后面的常量集合拆开,把里面的每个常量都提出来形成一个分支,各 ...

  10. .net下的跨域问题

    环境: IIS7.0 MVC 4.0     公司官网 asp.net      需要的报名系统,需要有后台管理 由于是配合传统产业,所以MVC系统的数据,是由AIPS系统提供. (制作前是考虑去年用 ...