POJ 3169_Layout
大早上水一发=。=
题意:
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的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- 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 ...
- poj 2352 Stars 数星星 详解
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- PHP + ORACLE 远程连接数据库环境配置
在ORACLE官网下载instantclient_11_2,放在D盘 把instantclient_11_2目录下的所有dll文件复制到C:\Windows\SysWOW64 和 D:\phpS ...
- java之java.sql.SQLException: ResultSet is from UPDATE. No Data.
问题解释:java调用存储过程的时候,查询结果不能通过ResultSet来查询,需要通过CallableStatement来查询, 比如: ResultSet rs = callableStateme ...
- Unity中,保存在OnInspectorGUI中改变的值
using UnityEngine; using System.Collections; using UnityEditor; [CustomEditor( typeof( MessageLog ) ...
- 使用Jenkins进行android项目的自动构建(5)
之前在项目中引入的单元测试使用的是JUnit,可以在构建前进行测试,这里在介绍一下使用Instrumentation 进行单元测试.使用Instrumentation进行测试,比之前多一些步骤,需要把 ...
- iOS----创建静态库
静态库 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.根据源代码的公开情况,库可以分为2种类型 开源库 公开源代码,能看到具体实现 比如SDWebImage.AFNetworkin ...
- leetcode_654. Maximum Binary Tree
https://leetcode.com/problems/maximum-binary-tree/ 给定数组A,假设A[i]为数组最大值,创建根节点将其值赋为A[i],然后递归地用A[0,i-1]创 ...
- Java IO(一)--File类
File类不是单指文件,它既可以代表一个文件名称,又可以代表一个目录下的一组文件.可以用来创建.删除.遍历文件等 public static void main(String[] args) { St ...
- rsync_ssh
rsync -av -e "ssh" /data/wwwroot/a1 node2:/data/wwwroot/
- BZOJ1079: [SCOI2008]着色方案 (记忆化搜索)
题意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块. 所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得很 ...
- [css或js控制图片自适应]
[css或js控制图片自适应]图片自动适应大小是一个非常常用的功能,在进行制作的时候为了防止图片撑开容器而对图片的尺寸进行必要的控制,我们可不可以用CSS控制图片使它自适应大小呢?此个人博客想到了一个 ...