大早上水一发=。=

题意:

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. 设置电脑IP

    1.首先在Win7桌面上找到“网络”入口,如下图:   进入Win7网络 2.进入网络之后我们再点击顶部的“网络共享中心”,如下图:   进入Win7网络共享中心 3.进入Win7网络共享中心之后,我 ...

  2. Java基础教程(24)--集合

    一.Java集合框架   集合,有时也称为容器,是一个用来存储和管理多个元素的对象.Java中的集合框架定义了一套规范,用来表示和操作集合,使具体操作与实现细节解耦.集合框架都包含下列内容: 接口:这 ...

  3. Heart Beat

    实现关键: 1.纯css实现心形图(如果使用图片则无需) html代码: <!DOCTYPE html> <html> <head> <meta charse ...

  4. Unity笔记(4)自学第六天

    今天主要是写了demo的策划案 [关卡设计部分]: [关卡数值设计]:

  5. iOS Programming Editing UITableView

    iOS Programming Editing UITableView 1.1 Editing mode  UITableView has an editing property, and when ...

  6. IIS ARR(Application Request Route)与反向代理(Reverse Proxy)

    为何要用反向代理? 这里说说我的场景, 我在服务器上假设了SVN(Visual SVN)用的端口是:8080, 而我想通过输入svn.niusys.com就可以访问我的SVN服务器,也就是要通过80端 ...

  7. jboss中JVM监控

    1)打开 http://server-name-or-ip/jmx-console/HtmlAdaptor2)在 jboss.system 节点找到 type=ServerInfo ,点击进入3)找到 ...

  8. 浅谈Key-value 存储——SILT

    摘要:本文以文章SILT: A Memory Efficient High Performance Key-Value Store 为基础,探讨SILT存储系统是如何实现内存占用低和高性能的设计目标, ...

  9. opencv实现人脸,人眼,微笑检测

    1.首先实现人脸检测 import cv2 img = cv2.imread("people.jpg",1) #读入图像 #导入人脸级联分类器引擎,“.xml”文件里包含了训练出来 ...

  10. [CodeForces]1059D Nature Reserve

    大意:给你一个平面上N(N<=100000)个点,问相切于x轴的圆,将所有的点都覆盖的最小半径是多少. 计算几何???Div2的D题就考计算几何???某人昨天上课才和我们说这种计算几何题看见就溜 ...