题意

当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些。FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食。奶牛排在队伍中的顺序和它们的编号是相同的。因为奶牛相当苗条,所以可能有两头或者更多奶牛站在同一位置上。即使说,如果我们想象奶牛是站在一条数轴上的话,允许有两头或更多奶牛拥有相同的横坐标。
一些奶牛相互间存有好感,它们希望两者之间的距离不超过一个给定的数L。另一方面,一些奶牛相互间非常反感,它们希望两者间的距离不小于一个给定的数D。给出ML条关于两头奶牛间有好感的描述,再给出MD条关于两头奶牛间存有反感的描述。(1<=ML,MD<=10000,1<=L,D<=1000000)
你的工作是:如果不存在满足要求的方案,输出-1;如果1号奶牛和N号
奶牛间的距离可以任意大,输出-2;否则,计算出在满足所有要求的情况下,1号奶牛和N号奶牛间可能的最大距离。


题解

差分约束,第一种A向B连权值为v的边,第二种B向A连-v的边。
SPFA当有环时,-2;跑不到n, -1 我打的是DFS


常数巨大的丑陋代码

# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# include <math.h>
# define RG register
# define IL inline
# define ll long long
# define mem(a, b) memset(a, b, sizeof(a))
# define Min(a, b) (((a) > (b)) ? (b) : (a))
# define Max(a, b) (((a) < (b)) ? (b) : (a))
using namespace std; IL int Get(){
RG char c = '!'; RG int x = 0, z = 1;
for(; c > '9' || c < '0'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + c - '0';
return x * z;
} const int MAXN = 1001, MAXM = 20001, INF = 2147483647;
int n, dis[MAXN], vis[MAXN], ft[MAXN], m1, cnt, m2, flag;
struct Edge{
int to, f, nt;
} edge[MAXM]; IL void Add(RG int u, RG int v, RG int f){
edge[cnt] = (Edge){v, f, ft[u]}; ft[u] = cnt++;
} IL void Dfs(RG int u){
vis[u] = 1;
if(flag) return;
for(RG int e = ft[u]; e != -1; e = edge[e].nt){
RG int v = edge[e].to, f = edge[e].f + dis[u];
if(dis[v] > f){
dis[v] = f;
if(vis[v]){
flag = 1;
return;
}
Dfs(v);
}
}
vis[u] = 0;
} int main(){
mem(dis, 63); mem(ft, -1);
n = Get(); m1 = Get(); m2 = Get();
for(RG int i = 1; i <= m1; i++){
RG int u = Get(), v = Get(), f = Get();
Add(u, v, f);
}
for(RG int i = 1; i <= m2; i++){
RG int u = Get(), v = Get(), f = Get();
Add(v, u, -f);
}
dis[1] = 0; Dfs(1);
if(flag) printf("-1\n");
else if(dis[n] == dis[0]) printf("-2\n");
else printf("%d\n", abs(dis[n]));
return 0;
}

poj-3169Layout的更多相关文章

  1. POJ——3169Layout(差分约束)

    POJ——3169Layout Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14702   Accepted ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

  10. poj 2352 Stars 数星星 详解

    题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...

随机推荐

  1. Linux常用命令手册

    Linux常用命令手册 NO 分类 PS1 命令名 用法及参数 功能注解 对应章节 1 文件管理 # ls ls -a 列出当前目录下的所有文件,包括以.头的隐含文件     文件管理 # ls ls ...

  2. ApiManager搭建

    piManager 作为一个Api 文档管理工具,而且是开源的,作为开发者使用,还是蛮不错的,整体的界面也很友好,下面就来看一下吧. 下面就来介绍下ApiManager在centos 6下的搭建过程吧 ...

  3. [记]WIndow/Linux 获取本机(全部)IPv4、IPv6、MAC地址方法 (C/C++)

    Linux 获取本机IP.MAC地址用法大全 //#include <sys/types.h> #include <ifaddrs.h> #include <sys/io ...

  4. angular2-qrcode (转)

    插件选择 angular2-qrcode npm install angular2-qrcode --savecnpm install angular2-qrcode --save 参考github ...

  5. 自制PHP高防防盗链(不是一般的高)(思路)

    原理:根据IP,资源ID,时间戳,一次性Access_Token,APPKEY(暴露在前台)和APPSERECT(后台)来生成参数,具体见下面: 浏览器请求页面=>后台引用防盗链代码=>生 ...

  6. appium+Python 启动app(二)

    我们上步操作基本完成,下面介绍编写Python脚本启动app 打开我们pycharm新建.py文件 第一步:输入Python脚本代码: #coding=utf-8 from appium import ...

  7. MySQL取得某一范围随机数

    ①直接取值 若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1)). 例如, 若要在7 到 12 的范围(包括7和12) ...

  8. Python——Web.py詳解

    ubuntu安裝Web.py sudo pip install web.py 測試代碼: import web urls = ( '/(.*)','hello' ) app = web.applica ...

  9. MongoDB基础介绍安装与使用

    MongoDB已经日益成为流程和主流的数据库了,原因有两个:第一个就是技术优势,第二就是便利性,个人使用部署都很方便. MongoDB的优缺点,以及使用场景 优点: 面向文档存储(自由读高,不需要定义 ...

  10. Redis--配置密码

    可以通过以下方法进行密码的配置: ① 修改配置文件设置密码 ② 通过命令修改密码(重启redis后,新设置的密码会失效) 此处介绍第一种 1. 找到redis的配置文件,一般在/etc/redis.c ...