ZOJ 2770_Burn the Linked Camp
题意:
给定每个兵营的最大容量,以及第i到第j个兵营至少有多少个士兵,问所有兵营一共至少有多少个士兵?
分析:
差分约束系统,注意
- 第i到第j至少有k个
- 第i到第j最多有最大容量之和个
- 每个兵营至少有0个
- 每个兵营最多有最大容量个
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
#define mem(s,a) memset(s, a, sizeof(s))
typedef long long ll;
const int maxm = 25005, maxn = 1005;
#define INF 0x3ffffffff
struct edge{int u,v;ll w;};
edge e[maxm];
int c[maxn];
long long d[maxn];
int m, n, tot;
bool bellford()
{
fill(d,d+n+1,INF);
d[n] = 0;
for(int i = 0; i < n + 1; i++){
for(int j = 0; j < tot; j++){
int u1= e[j].u, v1 = e[j].v;
if(d[u1]!=INF&&d[v1]-d[u1]>e[j].w){
d[v1] = d[u1] + e[j].w;
if(i == n) return false;
}
}
}
return true;
}
int main (void)
{
int t;
while(~scanf("%d%d",&n, &m)){
mem(c,0);
for(int i = 1; i <= n; i++){
scanf("%d",&t);
c[i] = c[i-1] +t;
}
int i, j, k;
tot = 0;
for(int a = 0; a < m; a++){
scanf("%d%d%d",&i,&j,&k);
e[tot++] = (edge){j, i-1, -k};
e[tot++] = (edge){i-1, j, c[j]-c[i-1]};
}
for(int b= 0; b < n; b++){
e[tot++] = (edge){b, b + 1, c[b + 1] - c[b]};
e[tot++] = (edge){b+1, b, 0};
}
if(bellford()) printf("%I64d\n",d[n]-d[0]);
else printf("Bad Estimations\n");
}
return 0;
}
之前一直怕k会很大,就用了long long,可是INF忘记改,一直PE,后来改了INF,AC,改成int,AC,可是到现在也不明白为什么是PE而不是WA。
先放着再想想吧。
ZOJ 2770_Burn the Linked Camp的更多相关文章
- zoj Burn the Linked Camp (查分约束)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- ZOJ 2770 Burn the Linked Camp 差分约束
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...
- Burn the Linked Camp(bellman 差分约束系统)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- ZOJ 2770 Burn the Linked Camp(spfa&&bellman)
//差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...
- zoj 2770 Burn the Linked Camp
今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...
- ZOJ2770-Burn The Linked Camp(火烧连营Orz 差分约束-线性约束+最长路(OR反向最短路))
It is well known that, in the period of The Three Empires, Liu Bei, the emperor of the Shu Empire, w ...
- ZOJ2770 Burn the Linked Camp(差分约束系统)
区间和一定要联系到前缀和. 这题,把前缀和看作点,从s0到sn: 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi: 对于每一个区间的评估i,j,k,有sj-si-1> ...
随机推荐
- 关于jquery获取单选框value属性值为on的问题
当取单选框的value值的时候,前提是要有value这个属性,如果没有value属性那么取出来的就会为on 取value值的常见三种方式为 $("input[name='XXX']:chec ...
- for循环的两种写法哪个快
结果如下: 其实工作中,也没有这么多数据需要遍历,基本上用foreach
- poj3368 Frequent values
思路: 转化为RMQ. 实现: #include <cstdio> #include <cstring> #include <algorithm> using na ...
- 使用Jenkins进行android项目的自动构建(1)
环境搭建 1. 下载JDK,安装,并将JDK的安装目录加入到环境变量JAVA_HOME,将JDK的bin目录加入到环境变量PATH. 2. 下载Android SDK,解压,并将SDK的安装目录加入到 ...
- 程序员段子:世界上最大的同性交友平台github
程序员(又名程序猿)因为总是冲锋在网络的最前端,还有程序猿的各种特殊性,大家在茶余饭后都有很多关于程序员的段子流传.大多都是程序员自黑的,先说在前面,程序猿还是很好的!下面看看你有没有中枪的那一条呢? ...
- HDU_1237_简单计算器
运算符为+,-,*,/:操作数为整数:且没有括号 设定符号优先级,先在栈底压运算符0 #include<iostream> #include<cstdio> #include& ...
- bzip2 一种块排序文件压缩软件
总览 bzip2 [ -cdfkqstvzVL123456789 ] [ filenames ... ] bunzip2 [ -fkvsVL ] [ filenames ... ] bzcat [ - ...
- console.log()与console.dir()
console.log()可以取代alert()或document.write(),在网页脚本中使用console.log()时,会在浏览器控制台打印出信息. console.dir()可以显示一个对 ...
- vue之package.json文件解析
1.package.json是什么? 什么是Node.js的模块(Module)?在Node.js中,模块是一个库或框架,也是一个Node.js项目.Node.js项目遵循模块化的架构,当我们创建了一 ...
- 根据截至日期格式获取倒计时&&时间戳转日期格式
//时间戳转日期格式,传入时间戳必须为数字类型function currentDate(shijianchuo) { var date = new Date(shijianchuo); var y = ...