大早上水一发=。=

题意:

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. jquery基础知识点总结

    Jquery是一个优秀的js库,它简化了js的复杂操作,不需要关心浏览器的兼容问题,提供了大量实用方法. Jquery的写法 方法函数化 链式操作 取值赋值合体] $(“p”).html();   取 ...

  2. LN : leetcode 312 Burst Balloons

    lc 312 Burst Balloons 312 Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is pa ...

  3. php学习知识点

    1.PHP 代码被包含在特殊的起始符和结束符中 <? ?> 2.php的用途 服务端脚本 命令行脚本. 编写桌面应用程序.3.输出语句.文本 echo printf4.$_SERVER 是 ...

  4. PAT甲级考前整理(2019年3月备考)之三,持续更新中.....

    PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...

  5. Uml 建模 一(类图建模和startuml的使用)

    本文将分三个部分介绍Uml建模:Uml建模的作用.类图.startuml的使用 Uml的作用 本文以java为例介绍Uml,在当前的软件开发中大多数使用面向对象开发(OO),面向对象的就是将现实世界中 ...

  6. EasyUI edatagrid插件使用小计

    html片段 <table id="menuview" style="width:100%"> <thead> <tr> & ...

  7. Chrome 引起的蓝屏 MULTIPLE_IRP_COMPLETE_REQUESTS (44)

    如果你使用Chrome的时候出现经常性蓝屏, 可以试试这么做, 或许问题就解决了.

  8. mysql中对order by的函数substring_index() , find_in_set()使用

    题目是这样的:sql = "select  *  from table  where  id  in(3,1,2,5)";  怎样使得查询的结果按照 3 ,1 , 2, 5来排序: ...

  9. 进程的互斥运行:CreateMutex函数实现只运行一个程序实例

    HANDLE hMutex=CreateMutex(NULL,TRUE,"HDZBUkeyDoctorTool"); if(hMutex) { if(ERROR_ALREADY_E ...

  10. sql server update+select(子查询修改)20190304

    if OBJECT_ID('tempdb..##t2') is not null drop table ##t2;create table ##t2( a int, b int, c datetime ...