大早上水一发=。=

题意:

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. 老式浏览器支持html5和css3

    在IE页面的head标签里面加入   <!-[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/ ...

  2. (6)《Head First HTML与CSS》学习笔记---结尾、《HTML5权威指南》读书笔记

    1.内联元素的外边距.内边距与块元素稍有不同. 如果一个内联元素四周都增加外边距,只能看到左边和右边会增加空间:你也可以对内联元素的上下增加内边距,不过这个内边距不会影响包围它的其他内联元素的间距—— ...

  3. 用 dojo/request/script 玩垮域

    dojo/request/script 可以用于向服务器发送跨域请求,如JSONP等.但单看官方文档有点不容易理解,特将体会记录. require(["dojo/request/script ...

  4. centOS linux 下PHP编译安装详解

    一.下载PHP源码包 wget http://php.net/distributions/php-5.6.3.tar.gz   二.添加依赖应用 yum install -y gcc gcc-c++ ...

  5. scrapy 的分页爬取 CrawlSpider

    1.创建scrapy工程:scrapy startproject projectName 2.创建爬虫文件:scrapy genspider -t crawl spiderName www.xxx.c ...

  6. mysql图形化工具获取表的源码

    打开数据库,选择要查看的表,点击右键>对象信息>DDL:

  7. 数据库中的Schema是什么?

    在数据库中,schema(发音 “skee-muh” 或者“skee-mah”,中文叫模式)是数据库的组织和结构,schemas andschemata都可以作为复数形式.模式中包含了schema对象 ...

  8. 《BUG创造队》第五次作业:项目需求分析改进与系统设计

    项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验九 团队作业5-团队项目需求改进与系统设计 团队名称 BUG创造队 作业学习目标 1.编写完整<软件需求规格说明书 ...

  9. 树莓派-3 启用root

    默认是user: pi,  password: raspberry 通过如下设置root密码并启用 pi@raspberrypi:~ $ sudo passwd root Enter new UNIX ...

  10. AutoEncoders变种

    目录 PCA V.S. Auto-Encoders Denoising AutoEncoders Dropout AutoEncoders PCA V.S. Auto-Encoders deep au ...