UVA 11478 Halum (差分约束)
题目链接: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 (差分约束)的更多相关文章
- Halum UVA - 11478(差分约束 + 二分最小值最大化)
题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的边的权值增加d,最后要让所有边权的最小值非负且尽量大 两个特判 1 ...
- UVA - 11478 - Halum(二分+差分约束系统)
Problem UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...
- UVA 11478 Halum(用bellman-ford解差分约束)
对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...
- UVA 11478 Halum
Halum Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 114 ...
- UVA - 11478 Halum 二分+差分约束
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 题意: 给定一个有向图,每一条边都有一个权值,每次你可以 ...
- UVA 11478 Halum(差分约束)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 [思路] 差分约束系统. 设结点u上的操作和为sum[u] ...
- Uva 11478 Halum操作
题目链接:http://vjudge.net/contest/143318#problem/B 题意:给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权 ...
- 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 ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
随机推荐
- .Net词汇表中常见缩略语汇总
.Net中存在大量的专业词汇(详细列表,请参考:Visual Studio 和 .NET Framework 词汇表),其中很多词汇常常采用缩略语的形式被大量使用. 在阅读.Net书籍或网络资料时,便 ...
- JavaWeb:基于MVC设计模式的一个小案例(一)
(未经允许,请勿转载,谢谢.) 本案例的处理过程: 客户端发送一个请求给服务器,服务器把这个请求给Servlet,Servlet 获取请求信息,根据请求信息的情况去调用 model (在这里是一个普通 ...
- 日期函数(SqlServer)
1.常用日期方法(下面的GetDate() = '2006-11-08 13:37:56.233') (1)DATENAME ( datepart ,date ) 返回表示指定日期的指定日期部分的字符 ...
- for 的多重循环--java
for的多重循环--java 利用for的多重循环打印出四种不同的三角形的图案. 图案如下: 4种不同三角形图案打印如下------------------******---------------- ...
- Linux 与 BSD 有什么不同?
Linux 与 BSD 有什么不同? 这篇文章是别人写的,并做了一点修改. 汉澳sinox就是基于bsd开发的,因此能够理解为一个bsd分支,可是由于sinox不开源,被排除在外.bsd不是商业软件, ...
- swift菜鸟入门视频教程-04-集合类型
本人自己录制的swift菜鸟入门,欢迎大家拍砖,有什么问题能够在这里留言. 主要内容: 数组(Arrays) 字典(Dictionaries) 集合的可变性(Mutability of Collect ...
- 用C/C++扩展你的PHP(转)
简 介 英文版下载: PHP 5 Power Programming PHP取得成功的一个主要原因之一是她拥有大量的可用扩展.web开发者无论有何种需求,这种需求最有可能在PHP发行包里找到.PHP发 ...
- sublime text3中的常用插件
1.All Autocomplete Sublime Text 默认的 Autocomplete 功能只考虑当前的文件,而 AllAutocomplete 插件会搜索所有打开的文件来寻找匹配的提示词. ...
- Set Windows IP by Batch
netsh interface ip set address name="Local" static 192.168.1.55 255.255.255.0 192.168.1.1 ...
- C#读取word模版并对指定域写入数据保存为新word
引用: using System;using System.Collections.Generic;using System.Aspose.Words;using System.Windows.For ...