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 ...
随机推荐
- asdasd
adasdasd asdasd asdasd asd
- @IBDesignable和@IBInspectable
近期一直在看苹果公司提供的两本swift官方教程电子书,一部是<The Swift Programming Language>,还有一部是<Using Swift With Coco ...
- C语言排序算法复习
排序算法有很多种,这里在复习和分析的基础上,做一个自己的总结: 首先要知道有哪些排序算法,google一下,有云C语言7大经典排序算法(也有8大).主要包括冒泡排序,快速排序,选择排序,插入排序,希尔 ...
- 世界最大射电望远镜(Arecibo)用于探測地外文明
请看下图: 这是眼下世界最大口径(305米)射电望远镜的外观图,位于美国中西部的PuertoRico州,建成于1963年,工作波段在3厘米至1米波段范围内,正好适合用于探測地外文明(SETI)的科学研 ...
- iOS-BLE蓝牙开发持续更新
文/煜寒了(简书作者)原文链接:http://www.jianshu.com/p/84b5b834b942著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 在写这个博客之前,空余时间抽看 ...
- cpio.gz 解压
linux下cpio.gz文件的解压方法:今天下载了 10201_database_linux_x86_64.cpio.gz 文件,解压方法如下:1. gunzip 10201_database_li ...
- js中substring/substr和C#中Substring的用法
一:在js中截取字符串的方法有两个:substring和substr 1.方法: substring(int stringIndex,[int endIndex]) 截取从索引为stringIndex ...
- ORACLE 物化视图
最近几天,我负责的P项目环境中提供给W系统的一个视图,由于查询逻辑复杂,数据量比较大,导致每次查询视图的时候,查询速度慢,效率低下,遭到了w系统人员的投诉.想了想,还是改成物化视图吧,用了物化视图,腰 ...
- Swift 字符串连接
// 使用+直接相加 var i = var str = "Hello" str = str + " jinpangpang" // 可以使用这种方式连接整值 ...
- dbutils的使用Demo
首先了解一下 Queryrunner.query —————只可以执行select语句. Queryrunner.update —————只可以接收update,delte,insert语句 ...