POJ1201Intervals(差分约束系统)
昨天看了下差分约数系统的含义,其实就是如果有n个变量在m个形如aj-ai>=bk条件下,求解的此不等式的方法。
而这种不等式的解法其实就是转化为图论的最小路的算法求解的。我们将上面的不等式边形后得到aj>=ai+bk正好就可以看做是从ai到aj权值是bk的一条路径最短的边。这样一来,只要依照题目的条件写出一系列这样的不等式,也就是相当于按照题意增加了一些合法的边,也就完全转化为了最短路的算法。
再看这道题,题目说[ai, bi]区间内和点集Z至少有ci个共同元素,那也就是说如果我用Si表示区间[0,i]区间内至少有多少个元素的话,那么Sbi - Sai >= ci,这样我们就构造出来了一系列边,权值为ci,但是这远远不够,因为有很多点依然没有相连接起来(也就是从起点可能根本就还没有到终点的路线),此时,我们再看看Si的定义,也不难写出0<=Si - Si-1<=1的限制条件,虽然看上去是没有什么意义的条件,但是如果你也把它构造出一系列的边的话,这样从起点到终点的最短路也就顺理成章的出现了。
我们将上面的限制条件写为同意的形式:
Sbi - Sai >= ci
Si - Si-1 >= 0
Si-1 - Si >= -1
这样一来就构造出了三种权值的边,而最短路自然也就没问题了。
但要注意的是,由于查分约束系统里常常会有负权边,所以为了避免负权回路,往往用Bellman-Ford或是SPFA求解(存在负权回路则最短路不存在)。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABTMAAABKCAIAAADouVc2AAAgAElEQVR4nO1dy3IcyXXtz4EwUvBXYAuS7AWX/ANDoQc2Dq3wAYKkkCXYEQqFVxNcCGZ4YUOSwzHh3TCCi0FXAySI7moAHJIgQc0ADXSlF/XKx82srOyqysquU3EWZD8Kt09l3ntPPm6OGC5cuHDhwoULFy5cuHDhwoXL3zV6svccaBuMMe82AM5Iu4p3M3oCUAEAfoE+6MwbqHOmzrsNgQLUgToAqAUo8y4A/xI0kM9JbHi3AQCGDPRBZ95AnTN13m0IFKAO1AFALUCZdwH4l6CBfE5iw7sNADBkoA868wbqnKnzbkOgAHWgDgBqAcq8C8C/BA3kcxIb3m0AgCEDfdCZN1DnTJ13GwIFqA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uJD3VC7AHjU0jp2gbqq6Hn2dXJxw97esqu/sddvueh58TCZ36uI5vdRvHj15iHdds5Hz7SY3GS+HMfitDkly2mZTXOik0jC69YDHOW3VA/J+wGLG2rdlOBqqmW59j4lOeRNyPXRRk6IlSClAcRYEm2P+Jm+5SpNzpkDOtRS5qqbK1YyO4O4Q9UYM1Cg1TWQwQFUuMCYW7x6k8Qf2Pwjm39MB+/v05H+KF5E8X1xWKuIRRQvJvP7l1cP0/ds/jFdlZdM5vfH09vx7G4yX0axZkmeCEMdB6CfQB905g3UOVPn3YZAAepWpa7H0RMA2gCUeRewdc3UTu9xzPZXG86hV3cfsX1Pi7uCA/I5iQ3vNoQHY27x+m2WWEzfs5dXLM0b0tQhiheG3CL9TPr1vJjNA5T52gN90Jk3UOdMnXcbAgWoW5W6HkdPAGgDUOZdwNI1kyeEGcqMW4I46sxYIw2QgHxOYsO7DeFh1dxiySHNLe6j+D6KF5P54uzrrPLN+Tt2ermM4rvx7DaK78exvsAsB7XKI9BzoA868wbqnKnzbkOgAHWrUtfj6AkAbQDKvAusss+8kT088mYtJOJ1gHxOYsO7DeHBPbe4j+LyNJdolvBj/+PZfRQv0mI28Qc2fc9evVmeXCzGs2/Hs9vq3EJX0AHoN9AHnXkDdc7UebchUIC6VanrZ/QEgNYAZd4F4JqDBvI5iQ3vNoQH99xCTCzE9GI8W4xnd6eXy+l7Nrtms2v2+m1yenkfxbdfnX97PHvw/8OBFoA+6MwbqHOmzrsNgQLUrUodoicwMPw/I2CBdQFiW9IAAAAASUVORK5CYII=" alt="" width="697" height="38" />
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-15
#define MAXN 50005
#define INF 1000000007
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem(a) memset(a,0,sizeof(a)) struct EDGE//使用邻接表存边
{
int v;
int w;
int next;
}edge[*MAXN];
int head[MAXN], d[MAXN], vis[MAXN],N, n, Max,Min; void AddEdge(int u,int v,int w)//添加新边
{
edge[N].v = v;
edge[N].w = w;
edge[N].next = head[u];
head[u] = N++;
} void SPFA()//此题数据较大,用Bellman-ford恐怕过不了
{
for(int i=Min;i<=Max;i++) d[i] = -INF;
d[Min] = ;
queue<int>q;
q.push(Min);
while(!q.empty())
{
int x = q.front(); q.pop();
vis[x] = ;
for(int e=head[x];e != -;e=edge[e].next)if(d[edge[e].v] < d[x] + edge[e].w)
{
d[edge[e].v] = d[x] +edge[e].w;
if(!vis[edge[e].v])
{
q.push(edge[e].v);
vis[edge[e].v] = ;
}
}
}//由于题目说一定有解,我就略去了判断是否存在负权回路
} int main()
{
while(~scanf("%d", &n))
{
int u,v,w; N = ;
memset(head,-,sizeof(head));
mem(vis); mem(edge);
Min = INF; Max = -INF;
for(int i=;i<n;i++)
{
scanf("%d%d%d", &u,&v,&w);//题目条件的边
AddEdge(u,v+,w);
Min = MIN(Min, u);//在这里记录最小值和最大值,所求的就是以Min为源点
Max = MAX(Max, v+);//以Max为终点的最短路
}
for(int i = Min;i < Max; i++)//添加新边
{
AddEdge(i,i+,);
AddEdge(i+,i,-);
}
SPFA();
printf("%d\n", d[Max]);
}
return ;
}
POJ1201Intervals(差分约束系统)的更多相关文章
- UVA11478 Halum [差分约束系统]
https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...
- BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5395 Solved: 1750[Submit][Status ...
- ACM/ICPC 之 差分约束系统两道(ZOJ2770-POJ1201)
当对问题建立数学模型后,发现其是一个差分方程组,那么问题可以转换为最短路问题,一下分别选用Bellmanford-SPFA解题 ZOJ2770-Burn the Linked Camp //差分约束方 ...
- POJ1201 Intervals(差分约束系统)
与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
- UVA 11374 Halum (差分约束系统,最短路)
题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
- Burn the Linked Camp(bellman 差分约束系统)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- POJ 3169 Layout (差分约束系统)
Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...
- POJ 3169 Layout 差分约束系统
介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...
随机推荐
- Jenkins+Maven+SVN搭建自动部署、自动测试环境
.打开http://10.3.15.78:8080/jenkins/,第一次进入里面没有数据,我们需要创建job,我们这有2个项目,需要创建2个job.http://10.3.34.163:9890/ ...
- C语言中的三值合一
在学习C语言中我们会发现这样一种情况: #include<stdio.h> Int main() { Int ar[10]; printf(“%p\n”,ar); printf(“%p\n ...
- php 获取301 302的真实地址
function getRealURL($url){ $header = get_headers($url,1); if (strpos($header[0],'301') || strpos($he ...
- 【英语】Bingo口语笔记(51) - 相信怀疑的表达
- Excel2007条件格式怎么用
Excel2007的条件格式功能十分的强大实用,较2003版改进十分的大,下面我们以经验记录为例做一简单的操作示范.注意前部分有二点技巧可借鉴,即不规则选取和不规则统一填充. 工具/原料 EXCEL2 ...
- OpenCV 连接 Android IP摄像头
0.下载IP摄像头(android软件)并安装 比如这个(图标是一个灰色的摄像头的那个软件) 1.新建cpp文件,编译 #include "opencv2/opencv.hpp" ...
- 嵌入式 uboot以及kernel添加看门狗临时记录(个人记录未整理乱)
Uboot_Kernerl_Add_Watch_Dog: U-Boot 2010.06 (Nov 01 2013 - 15:28:44) DRAM: 128 MiBCheck spi flash c ...
- pthread_attr_t 线程属性(一)
1. 线程属性: 使用pthread_attr_t类型表示,我们需要对此结构体进行初始化, 初始化后使用,使用后还要进行去除初始化! ...
- free-jqGrid
PM> Install-Package free-jqGrid jqGrid 是一个用来显示网格数据的jQuery插件,通过使用jqGrid可以轻松实现前端页面与后台数据的ajax异步通信.文档 ...
- nsDATA 转结构体
很多时候需要将c,c++形式的struct转换为 NSData来处理.但是怎么转换呢? 假设有这么一个结构体: struct MYINFO { int a; long b; char c ...