zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统
// 火烧连营
// n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量
// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i-1,i) 权值为 C[i]
// 2.S(i-1)-Si<=0 这里 建边(i,i-1) 权值为 0
// 3.S(j)-S(i-1)>=k => S(i-1)-Sj<=-k 这里建边 (j,i-1) 权值为 -k
// 题目求的事 Sn-S0的最小值 Sn-S0>=m 中符合条件的m最大值就是答案
// 等价 S0-Sn<=-m 求点 Sn 到 S0的最小值 就是求最短路径 (这里可能不好理解,需要慢慢体会)
// #include <iostream>
#include <map>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define MOD 1000000007
#define maxn 20010 // 开始我天真了 maxn=10010 和 m 差不错大 WA的好难过 图中边数应该为 m+2n
#define MAX 100000000
struct node{
int to;
int next;
int val;
}E[maxn];
int num;
int V[];
int C[];
int d[],cnt[];
bool f[];
bool sfpa(int s,int t){// s既表示起点 又表示节点个数
queue <int> Q;
int u,v;
int e;
Q.push(s);
d[s]=;
f[s]=true;
while(!Q.empty()){
u=Q.front(); Q.pop();
cnt[u]++;
if(cnt[u]>s) return false;
f[u]=false;
for(e=V[u];e!=-;e=E[e].next){
v=E[e].to;
if(d[u]+E[e].val<d[v]){
d[v]=d[u]+E[e].val;
if(!f[v])
{
f[v]=true;
Q.push(v);
}
}
}
}
return true;
}
int main(){
int i,j,k;
int n,m;
while(scanf("%d %d",&n,&m)!=EOF){
for(i=;i<=n;i++)
{
scanf("%d",&C[i]);
V[i]=-;
d[i]=MAX;
cnt[i]=;
f[i]=false;
}
num=;
i=;
V[i]=-;
d[i]=MAX;
cnt[i]=;
f[i]=false;
while(m--){
scanf("%d %d %d",&i,&j,&k);
E[num].to=i-;
E[num].val=-k;
E[num].next=V[j];
V[j]=num++;
}
for(i=n;i>=;i--){
E[num].to=i;
E[num].val=C[i];
E[num].next=V[i-];
V[i-]=num++;
E[num].to=i-;
E[num].val=;
E[num].next=V[i];
V[i]=num++;
}
if(sfpa(n,))
printf("%d\n",-d[]);
else
printf("Bad Estimations\n"); } }
zoj 2770 Burn the Linked Camp (差分约束系统)的更多相关文章
- ZOJ 2770 Burn the Linked Camp 差分约束
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...
- ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...
- zoj 2770 Burn the Linked Camp
今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...
- ZOJ 2770 Burn the Linked Camp(spfa&&bellman)
//差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...
- zoj2770 Burn the Linked Camp --- 差分约束
有n个营地,每一个营地至多容纳Ci人.给出m个条件:第i到第j个营地之间至少有k人. 问n个营地总共至少有多少人. 此题显然差分约束.要求最小值.则建立x-y>=z方程组,建图求最长路. 用d[ ...
- 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 Burn the Linked Camp (查分约束)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- ZOJ2770 Burn the Linked Camp(差分约束系统)
区间和一定要联系到前缀和. 这题,把前缀和看作点,从s0到sn: 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi: 对于每一个区间的评估i,j,k,有sj-si-1> ...
- ZOJ 2770 差分约束+SPFA
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
随机推荐
- codeforces 397B
#include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...
- [C++]虚函数-同名访问
首先来看一下派生类和基类成员同名事的处理规则: 派生类内定义了一个与基类同名的成员,该现象称为同名覆盖,此时,无论派生类内部成员函数还是派生类的对象访问同名成员,如果未加任何特殊标识,则访问派生类中重 ...
- 青鸟 王云鹏老师写的SqlHelper 泛型方法,反射,支持实体类
1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: ...
- POJ3083Children of the Candy Corn
题意:给你一个迷宫,入口处标为S,出口处标为E,可以走的地方为“.”,不可以走的地方为#,求左转优先时从出口到入口的路程,再求右转优先时,出口到入口的路程,最后求从出口到入口的最短路程. 思路:求前两 ...
- 【转载】Ssh整合开发介绍和简单的登入案例实现
Ssh整合开发介绍和简单的登入案例实现 Ssh整合开发介绍和简单的登入案例实现 一 介绍: Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开 ...
- Python图片转换成矩阵,矩阵数据转换成图片
# coding=gbk from PIL import Image import numpy as np # import scipy def loadImage(): # 读取图片 im = Im ...
- 对cost函数的概率解释
Likehood函数即似然函数,是概率统计中经常用到的一种函数,其原理网上很容易找到,这里就不讲了.这篇博文主要讲解Likelihood对回归模型的Probabilistic interpretati ...
- 表连接到底咋回事,就是产生中间结果啊!用于给select/insert等操作用
1.表连接到底咋回事,就是产生中间结果啊!用于给select/insert等操作用啊. 2.表连接产生的结果用于select/insert用 3.表连接产生的结果用于select/insert用 比如 ...
- Ping 命令
Ping”命令是我们在判断网络故障常用的命令 它是用来检查网络是否通畅或者网络连接速度的命令. 它所利用的原理是这样的:网络上的机器都有唯一确定的IP地址,我们给目标IP地址发送一个数据包, 对方就要 ...
- 多线程进行n皇后计算
在浏览zhihu的时候, 看到了这个问题:Linux c++服务器端这条线怎么走? http://www.zhihu.com/question/22608820 , 其中排第一的答案说的很不错.针对他 ...