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 ...
随机推荐
- [每日一题] 11gOCP 1z0-053 :2013-09-30 ASMCMD.......................................................8
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12206095 正确答案:BCD 为了使ASM文件管理更简单,Oracle提供了一个命令实用 ...
- double精度的坑与BigDecimal
近期经常接触支付相关的功能,在开发及测试过程中,开始金额都使用的是double类型,而近期新进的需求存在支付时打折的情况,也就是会出现如 1.23元的情况,那么这时候问题来了,如果是直接使用1.23进 ...
- next数组
首先看看next数组值的求解方法例如: 模式串 a b a a b c a c next值 0 1 1 2 2 3 1 2 next数组的求解方法是:第一位的next值为0 ...
- 忘记mysql 5.7的密码
for windows: http://blog.chinaunix.net/uid-27570589-id-3511820.html 一.将net stop mysql; 二.在命令行中 C:\Us ...
- jquery之营销系统
// //////////////////////////优惠券开始//////////////////////////// // 给附加条件选择框添加事件 function inputFuJia() ...
- Spinner( 微调) 组件
本节课重点了解 EasyUI 中 Spinner(微调)组件的使用方法,这个组件依赖于ValidateBox(验证框)组件. 一. 加载方式Spinner(微调)组件是其他两款高级微调组件的基础组件, ...
- css伪类选择器详细解析及案例使用-----伪类选择器(1)
动态伪类选择器:E:link :选择匹配的E元素,并且匹配元素被定义了超链接并未被访问过.E:visited :选择匹配的E元素,而且匹配的元素被定义了连接并已被访问过.E:active :选择匹配的 ...
- DNN Module - Responsive Html Tabs 3 介绍
Responsive Html Tabs 模块可以运行于DNN7及以上版本,支持响应式.用户界面较为友好,可以插入自定义的HTML. Demo This is a user-friendly, ful ...
- iOS字体 UIFont 字体名字大全
打印所有的字体 NSArray *familyNames = [UIFont familyNames];//所有的家族名称 for(NSString *familyName in familyName ...
- QML学习心得
Qt Quick之于QML,正如Qt 之于 C++,QML是Qt中开发的一个新的语言,而Qt Quick是这个语言的一个组件库,其中包含了很多用QML写的可以现成使用的组件. QML Hello Wo ...