大早上水一发=。=

题意:

n头牛按编号顺序站成一列,给定n头牛之间的位置关系,求出第n头牛和第一头牛之间的最大距离。

分析:

差分约束系统,这题不等式关系还是挺好找的。注意因为按照顺序排列,所以有d[i+1]>=d[i]

代码:

#include<cstdio>
#include<iostream>
using namespace std;
struct edge{int u, v, w; };
const int maxn = 1005, maxm = 25005, INF = 0X3fffffff;
edge e[maxm];
int d[maxn];
int n, tot;
int bellmanford()
{
fill(d, d+n+1,INF);
d[1] = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < tot; j++){
edge te = e[j];
if(d[te.u] < INF&&d[te.v]>d[te.u]+te.w){
d[te.v] = d[te.u] + te.w;
if(i == n) return -1;
}
}
}
return d[n]==INF?-2:d[n];
}
int main (void)
{
int ml, md;
int a, b, d;
scanf("%d%d%d",&n, &ml, &md);
for(int i = 1; i < n; i++){
e[tot++] = (edge){i +1, i, 0};
}
for(int i = 0; i < ml; i++){
scanf("%d%d%d",&a,&b,&d);
e[tot++] = (edge){a, b, d};
}
for(int i = 0; i < md; i++){
scanf("%d%d%d",&a,&b,&d);
e[tot++]=(edge){b, a, -d};
} printf("%d\n", bellmanford());
return 0;
}

POJ 3169_Layout的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ 2739. Sum of Consecutive Prime Numbers

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

  7. POJ 2255. Tree Recovery

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

  8. 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 ...

  9. poj 2352 Stars 数星星 详解

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

随机推荐

  1. AJPFX编写cmd界面下一键编译、执行java代码的bat脚本

    此脚本适合刚接触java的同学,在cmd界面下用jc取代 复杂的  javac *.java + java main使用说明:把脚本内容复制到txt文本中,修改后缀名为.bat,运行一次即可完成配置, ...

  2. Tomcat源码分析----eclipse中搭建源码环境

    前提:JDK,至少1.7,ant,要设置ANT_HOME环境变量,需要再classpath中增加ant的lib目录,在path变量中增加ant的bin目录 1.官网下载tomcat源码包:apache ...

  3. 微信小程序组件解读和分析:十五、switch 开关选择器

    switch 开关选择器组件说明: switch,开关选择器.只能选择或者不选.这种属于表单控件或者查询条件控件. switch 开关选择器示例代码运行效果如下: 下面是WXML代码: [XML] 纯 ...

  4. git ---查看工作状态和历史提交

    1.git查看状态 -git status 2.版权声明 版权声明:新建一个   LICENSE.txt   文件 开源协议:MIT   //开源许可里面的最宽松的一个协议,别人可以随便用你的代码,但 ...

  5. iOS Programming Dynamic Type 1

    iOS Programming Dynamic Type 1  Dynamic Type is a technology introduced in iOS 7 that helps realize ...

  6. sql把两值之和当作条件进行查询

    目的:把表中两个字段之和作为where条件进行过滤查询 //查询在没有过期的记录select a,b from test where a+b>now();// a:存入时间 b:有效期时间段 进 ...

  7. JAVA——不简单的fianl关键字

    protected用来修饰 域,代表域的访问权限是:包权限 或者 不同包,但是是子类 : final 修饰常量只要是该常量代入的计算式,在编译时期,就会被执行计算,以减轻运行时的负担.(只对基本数据类 ...

  8. day25-2 OSI协议和socket抽象层

    目录 OSI协议 物理层 数据链路层 以太网协议 Mac地址 广播地址 网络层 获取对方Mac地址(ARP协议) 传输层 TCP协议 UDP协议 应用层 socket抽象层 OSI协议 互联网的本质就 ...

  9. mysql.connector.errors.IntegrityError: 1048 (23000): Column 'request_url' cannot be null

    箭头指向的2的方向 少了一个request_url的值 调试: 直接 执行sql语句 是没问题的, 这样就知道问题是出在代码的逻辑 处理了: INSERT INTO response_time (nu ...

  10. CAD嵌套打印(网页版)

    当用户需要打印两个CAD控件的图纸时,可以采用嵌套打印实现.点击此处在线演示. 实现嵌套打印功能,首先将两个CAD控件放入网页中,js代码如下: <p align="center&qu ...