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

题解:

首先我们可以得到的约束条件形如 xi - xj <= b[k] ,即可以转换为 j - > i连边,且权值为b[k],这样建图后我们判断是否有解,只需要用spfa跑负圈就可以了.

如果存在负圈,原差分系统定然无解.

简单证明:

我们不妨设这个环为 x1,x2...xn

即有不等式 x1 <= x2 + y1 , x2 <= x3 + y2 ..... xn <= x 1 + yn

全部累加得 0 <= sigma (y)

所以有解必须满足sigma(y) >= 0 ,如果存在负圈,肯定是无解的.

那么对于原图,如何判断infinate的情况呢?

注意到约束条件必须是环,所以我们只需要检测一下图中是否有环就可以了.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 5e2 + ;
const double eps = 1e-;
struct Edge{int v , nxt , w;};
Edge e[maxn * ];
int n , m , head[maxn] , tot , cnt[maxn] ,inq[maxn] ,d[maxn],c[maxn];
queue<int>q;
void addedge(int u,int v,int w){e[tot].v=v,e[tot].nxt=head[u],e[tot].w=w,head[u]=tot++;} void edge_init(int x)
{
for(int i = ; i < tot ; ++ i) e[i].w += x;
} bool check()
{
while(!q.empty()) q.pop();
memset(cnt , , * (n + ));
memset( d , , * (n + ) );
for(int i = ; i <= n ; ++ i)
{
inq[i] = ;
q.push(i);
}
while(!q.empty())
{
int x = q.front();q.pop();inq[x]=;
for(int i = head[x] ; ~i ; i = e[i].nxt)
{
int v = e[i].v;
double neww = d[x] + e[i].w;
if(neww < d[v])
{
d[v] = neww;
if(!inq[v])
{
inq[v] = ;
if(++cnt[v] > n) return true;
q.push(v);
}
}
}
}
return false;
} bool dfs(int u)
{
c[u]=-;
for(int i = head[u] ; ~i ; i = e[i].nxt)
{
int v = e[i].v;
if(c[v]==) continue;
if(c[v]==-) return true;
if(dfs(v)) return true;
}
c[u]=;
return false;
} int main(int argc,char *argv[])
{
while(~scanf("%d%d",&n,&m))
{
memset(head,-,*(n+));tot=;memset(c , , * (n + ));
for(int i = ; i < m ; ++ i)
{
int u ,v,w ;scanf("%d%d%d",&u,&v,&w);
addedge( u , v, w);
}
int flag = ;
for(int i = ; i <= n ; ++ i)
if(c[i]==)
if(dfs(i))
{
flag=;
break;
}
if(!flag)
{
printf("Infinite\n");
continue;
}
int L = , R = ;
while(L < R)
{
int mid = L + (R-L+)/;
edge_init(-mid);
if(check()) R = mid-;
else L = mid;
edge_init(mid);
}
edge_init(-L);
if(L == || check()) printf("No Solution\n");
else printf("%d\n",L);
}
return ;
}

UVA 11478 Halum (差分约束)的更多相关文章

  1. Halum UVA - 11478(差分约束 + 二分最小值最大化)

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

  2. UVA - 11478 - Halum(二分+差分约束系统)

    Problem  UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...

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

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

  4. UVA 11478 Halum

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

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

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

  6. UVA 11478 Halum(差分约束)

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

  7. Uva 11478 Halum操作

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

  8. 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 ...

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

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

随机推荐

  1. ie浏览器中 overflow:hidden无作用的解决方案

    原因: overflow:hidden失效 当父元素的直接子元素或者下级子元素的样式拥有position:relative属性时,父元素的overflow:hidden属性就会失效. 我在ie内发现子 ...

  2. HTML5 简单实现刮刮乐效果

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 多次绑定click及ajax提交常用方法

    <script> $(document).ready(function() { //绑定click $(".exchange_ecv").bind("clic ...

  4. JS(五)

    感觉JS里面还是有很多小技巧的,知道套路了,其实实现起来其实也还没有想象中的那么复杂.不过我觉得还是要把所学的知识融会贯通吧,不能学了JS就忘了前面的知识,结合起来才会威力无穷. 1.跑马灯:弹弹弹 ...

  5. iOS 时区问题总结 NSTimeZone

    基本概念 GMT 0:00 格林威治标准时间; UTC +00:00 校准的全球时间; CCD +08:00 中国标准时间 [来自百度百科] 夏时制,英文"DaylightSavingTim ...

  6. 内嵌cuzySDK的App——礼物购已登陆App store

    内嵌cuzySDK的App——礼物购已登陆App store.每天为你搜罗特别的礼物,可分类挑选礼物,直接连接淘宝购买,做最贴心的小清新礼物助手,欢迎各位亲朋好友去下载体验.@cuzySDK  @re ...

  7. firefox关于about:config的常用配置

    about:config是火狐的设置页面,火狐提供了不少高级设置选项在这里以便让你可以更加详细地控制火狐的运行方式.(官方不推荐用户手工修改about:config的设置.所以,如果你对于你想修改的内 ...

  8. Swift2.0下UICollectionViews拖拽效果的实现

    文/过客又见过客(简书作者)原文链接:http://www.jianshu.com/p/569c65b12c8b著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文UICollecti ...

  9. 〖转〗request.getparameter()和request.getAttribute()的区别

    getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...

  10. jquery之onchange事件2

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...