题目传送门

题意:有两种关系,n牛按照序号排列,A1到B1的距离不超过C1, A2到B2的距离不小于C2,问1到n的距离最大是多少.如果无限的话是-2, 如果无解是-1

分析:第一种可以写这样的方程:d[v] - d[u] <= w1, u到v连一条权值为w1的边,第二种这样:d[v] - d[u] >= w2 => d[u] - d[v] <= -w2,v到u连一条-w2的边.当然根据题目还有i+1 到 i连权值0的边.最短路就是为了d[v] <= d[u] + w[u][v], 也就是d[v] - d[u] <= ans.求出最短路也就是满足了所有方程的约束条件.如果INF无解,如果负环无数解,否则就是ans了.

//#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std; const int N = 1e3 + 5;
const int E = 2e4 + 5;
const int INF = 0x3f3f3f3f;
struct Edge {
int v, w, nex;
Edge() {}
Edge(int v, int w, int nex) : v (v), w (w), nex (nex) {}
}edge[E*2];
int head[N];
int d[N];
bool vis[N];
int cnt[N];
int n, m1, m2, e; void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, int w) {
edge[e] = Edge (v, w, head[u]);
head[u] = e++;
} bool SPFA(int s) {
memset (vis, false, sizeof (vis));
memset (cnt, 0, sizeof (cnt));
memset (d, INF, sizeof (d));
d[s] = 0; cnt[s] = 0; vis[s] = true;
queue<int> que; que.push (s);
while (!que.empty ()) {
int u = que.front (); que.pop ();
vis[u] = false;
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v, w = edge[i].w;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
if (!vis[v]) {
vis[v] = false; que.push (v);
if (++cnt[v] > n) return true;
}
}
}
}
return false;
} int main(void) {
while (scanf ("%d%d%d", &n, &m1, &m2) == 3) {
init ();
for (int i=1; i<n; ++i) {
add_edge (i+1, i, 0);
}
for (int u, v, w, i=1; i<=m1; ++i) {
scanf ("%d%d%d", &u, &v, &w);
add_edge (u, v, w);
}
for (int u, v, w, i=1; i<=m2; ++i) {
scanf ("%d%d%d", &u, &v, &w);
add_edge (v, u, -w);
}
if (SPFA (1)) puts ("-1");
else {
bool ok = true;
for (int i=1; i<=n; ++i) {
if (d[i] == INF) {
ok = false; break;
}
}
if (!ok) puts ("-2");
else printf ("%d\n", d[n]);
}
} return 0;
}

  

差分约束系统 POJ 3169 Layout的更多相关文章

  1. 图论--差分约束--POJ 3169 Layout(超级源汇建图)

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...

  2. POJ 3169 Layout (差分约束系统)

    Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...

  3. POJ 3169 Layout 差分约束系统

    介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...

  4. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...

  5. poj 3169 Layout(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Descriptio ...

  6. poj 3169 Layout (差分约束)

    3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...

  7. POJ 3169 Layout(差分约束啊)

    题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...

  8. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...

  9. POJ 3169 Layout (HDU 3592) 差分约束

    http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...

随机推荐

  1. Yii里获取当前controller和action的id

    Yii里获取当前controller和action的id 在控制器里$name = $this->getId();  // controller$name = $action->id;  ...

  2. XP/Win7下QTP11循环试用30天的破解方法

    XP/Win7下QTP11循环试用30天的破解方法. XP下:1.找到以下路径:C:\Documents and Settings\All Users\Application Data\SafeNet ...

  3. 解决window删除文件时提示: 源文件名长度大于系统支持的长度

    import java.io.File; /** */ public class DeleteFiles { public static void deleteFiles( File file ){ ...

  4. IE6中使用通用选择器模拟子选择器效果

    IE6及更低版本不支持高级选择器:IE7有个bug,对于子选择器和相邻同胞选择器,如果父元素和子元素有HTML注释,会出问题. 下面我们使用通用选择器来模拟子选择器的效果. 原理:首先在所有后代上应用 ...

  5. 三、jQuery--jQuery基础--jQuery基础课程--第1章 初识jQuery

    环境搭建 搭建一个jQuery的开发环境非常方便,可以通过下列几个步骤进行. 下载jQuery文件库 在jQuery的官方网站(http://jquery.com)中,下载最新版本的jQuery文件库 ...

  6. web.config详解 -- asp.net夜话之十一

    1.配置文件节点说明    1.1 <appSettings>节点    1.2 <connectionStrings>节点    1.3 <compilation> ...

  7. 什么是DMI,SMBIOS,符合SMBIOS规范的计算机的系统信息获取方法

    转自:http://www.cnblogs.com/gunl/archive/2011/08/08/2130719.html DMI是英文单词Desktop Management Interface的 ...

  8. .net学习之CTS、CLS和CLR

    CLR:公共语言运行时,就是所有.net语言写的程序的公共运行时环境,比如C#.VB.Net等语言写的程序需要运行在CLR上,然后CLR解析执行操作系统的相关指令,CLR是.net程序运行在操作系统的 ...

  9. 全局压缩http响应头

    见代码: public class CompressAttribute : ActionFilterAttribute { public override void OnActionExecuting ...

  10. 【openGL】画正弦函数图像

    #include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...