欢迎来到个人简历网!永久域名:gerenjianli.cn (个人简历全拼+cn)
当前位置:首页 > 范文大全 > 实用文>POJ 3159 Candies 还是差分约束(栈的SPFA)

POJ 3159 Candies 还是差分约束(栈的SPFA)

2023-05-07 07:54:53 收藏本文 下载本文

“闹易德”通过精心收集,向本站投稿了10篇POJ 3159 Candies 还是差分约束(栈的SPFA),下面就是小编整理后的POJ 3159 Candies 还是差分约束(栈的SPFA),希望大家喜欢。

POJ 3159 Candies 还是差分约束(栈的SPFA)

篇1:POJ 3159 Candies 还是差分约束(栈的SPFA)

poj.org/problem?id=3159

题目大意:

n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大,

思路:

嗯,我先揭发一下,1号是分糖果的孩子,班长大人!(公报私仇啊。。。,欺负N号的小朋友~ 好吧,我开玩笑的)

嗯,这题要求最短路径。为啥是最短?你以前都在玩最长呀~

因为这题要求的是最大的。图的三角不等式有:d[v]- d[u]<=w(u,v); d[v]<=d[u]+w(u,v); 即而松弛的条件为: if(d[v]>d[u]+w(u,v)) d[v]<=d[u]+w(u,v); 通过不断的松弛,使得d的值不断变小,直到满足所有条件,也就是说满足条件的时候就是最大的了~

建立图,b - a <=x 然后就是spfa了,不过这题竟然卡队列了。。看discuss人家用stack,然后我也改了,就这样过了。。。。。。

还有这题不能建立超级源点。

设第i个小孩子分到的糖果为s[i],那么 有s[i]>=0

而上面的是b-a<=x,由于这题求的是最短路径,所以要改为小于号也就是 -s[i]<=0,然后如果添加一个点比如说0那么就是应该从i到0了,那么就无用了。。。。

#include

#include

#include

using namespace std;

const int MAXN=30000+10;

const int MAXM=350000;

const int INF=-999999;

int n,m,head[MAXN],len,dis[MAXN];

bool vis[MAXN];

struct edge

{

int to,val,next;

}e[MAXM];

void add(int from,int to,int val)

{

e[len].to=to;

e[len].val=val;

e[len].next=head[from];

head[from]=len++;

}

void spfa

{

int s=1;

stackq;

q.push(s);

vis[s]=true;

dis[s]=0;

while(!q.empty())

{

int cur=q.top();

q.pop();

vis[cur]=false;

for(int i=head[cur];i!=-1;i=e[i].next)

{

int id=e[i].to;

if(dis[id] >dis[cur] + e[i].val)

{

dis[id]=dis[cur]+e[i].val;

if(!vis[id])

{

q.push(id);

vis[id]=true;

}

}

}

}

}

int main()

{

while(~scanf(“%d%d”,&n,&m))

{

for(int i=1;i<=n;i++)

{

head[i]=-1;

dis[i]=-INF;

vis[i]=false;

}

len=0;

for(int i=0;i

篇2:poj1275 差分约束

这道题很特殊,与以前做的差分约束完全不一样,因为在它的约束条件中竟然还有变量,

建图方法:

说明:

r[i]-------第i小时需要的人

t[i]-------第i小时去应聘的人

s[i]-------第0到i小时总共招聘的人

约束系统:

1.s[i]-s[i-1]<=t[i]

2.s[i]-s[i-1]>=0

3.s[j]-s[i]>=r[i],j=(i+8)%24,j>i

4.s[j]+sum-s[i]>=r[i],j=(i+8)%24,j

5.s[24]-s[0]>=sum

算法思想:Bellman_Ford()+枚举sum (此处也可以用二分枚举,我是直接枚举的)

[cpp]

#include

#include

using namespace std;

struct{

int u,v,w;

}e[200];

int t[25],r[25],s[25],d[25];

int n,m;

bool relax(int u,int v,int w)

{

if(d[u]+w>d[v])

{

d[v]=d[u]+w;

return true;

}

return false;

}

bool Bellman_Ford()

{

int i,j;

bool flag=true;

for(i=0;i<=n;i++)

d[i]=100000000;

d[0]=0;

for(i=0;i

#include

#include

using namespace std;

struct{

int u,v,w;

}e[200];

int t[25],r[25],s[25],d[25];

int n,m;

bool relax(int u,int v,int w)

{

if(d[u]+w>d[v])

{

d[v]=d[u]+w;

return true;

}

return false;

}

bool Bellman_Ford()

{

int i,j;

bool flag=true;

for(i=0;i<=n;i++)

d[i]=100000000;

d[0]=0;

for(i=0;i

int main()

{

int Case,sum;

int i,j;

scanf(“%d”,&Case);

while(Case--)

{

for(i=1;i<=24;i++)

{

t[i]=0;

scanf(“%d”,r+i);

}

scanf(“%d”,&n);

while(n--)

{

int time;

scanf(“%d”,&time);

t[time+1]++;

}

for(i=1;i<=24;i++)

s[i]=s[i-1]+t[i];

///////////////////////////

//此部分建固有边

m=0;

n=24;

for(i=1;i<=24;i++)

{

e[m].u=i-1;

e[m].v=i;

e[m].w=0;

m++;

e[m].u=i;

e[m].v=i-1;

e[m].w=-t[i];

m++;

}

for(i=1;i<=16;i++)

{

j=i+8;

e[m].u=i;

e[m].v=j;

e[m].w=r[j];

m++;

}

//////////////////////////////

int mm=m;

for(sum=0;sum<=s[24];sum++)

{

m=mm;

for(i=17;i<=24;i++)

{

j=i-16;

e[m].u=i;

e[m].v=j;

e[m].w=r[j]-sum;

m++;

}

e[m].u=0;

e[m].v=24;

e[m].w=sum;

m++;

if(Bellman_Ford())

break;

}

if(sum<=s[24])

printf(“%d\n”,sum);

else

printf(“No Solution\n”);

}

return 0;

}

篇3:poj 1275 Cashier Employment 差分约束

差分约束模板题,差分约束是判断联立不等式组是否有解的一种方法,建图是关键,

代码:

//poj 1275//sep9#include#includeusing namespace std;const int maxM=10024;const int maxN=32;struct Edge{ int v,w,nxt; }edge[maxM];int t[maxN],c[maxN],head[maxN],vis[maxN],inq[maxN],dis[maxN];int e;void addedge(int u,int v,int w){ edge[e].v=v;edge[e].w=w;edge[e].nxt=head[u]; head[u]=e++; }bool spfa{ memset(inq,0,sizeof(inq)); for(int i=0;i<=24;++i) dis[i]=INT_MIN; memset(vis,0,sizeof(vis)); queueQ; Q.push(0); dis[0]=0; inq[0]=1; vis[0]=1; while(!Q.empty()){ int u=Q.front();Q.pop();inq[u]=0; for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].v,w=edge[i].w; if(dis[v]=25)return false; } } } } return true;}int main(){ int cases; scanf(“%d”,&cases); while(cases--){ memset(head,-1,sizeof(head)); memset(t,0,sizeof(t)); for(int i=1;i<=24;++i) scanf(“%d”,&c[i]); int n; scanf(“%d”,&n); for(int i=0;i

篇4:差分定位

差分定位(Differentialpositioning),也叫相对定位,是根据两台以上接收机的观测数据来确定观测点之间的相对位置的方法,它既可采用伪距观测量也可采用相位观测量,大地测量或工程测量均应采用相位观测值进行相对定位,

差分定位

篇5:差分GPS

差分GPS(DGPS)是在正常的GPS外附加(差分)改正信号,此改正信号改善了GPS的精度,

差分GPS

篇6:POJ 3169 Layout (差分约束系统 + Bellmanford算法)

Layout

Time Limit:1000MSMemory Limit:65536KTotal Submissions:7613Accepted:3658

Description

Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2<= N<= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).

Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1<= ML<= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1<= MD<= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.

Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.

Input

Line 1: Three space-separated integers: N, ML, and MD.

Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1<= A< B<= N. Cows A and B must be at most D (1<= D<= 1,000,000) apart.

Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1<= A< B<= N. Cows A and B must be at least D (1<= D<= 1,000,000) apart.

Output

Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.

Sample Input

4 2 11 3 102 4 202 3 3

Sample Output

27

Hint

Explanation of the sample:

There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.

The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.

Source

USACO December Gold

题意:一个牛舍里有N头牛,有一些牛的关系比较好,他们希望彼此不超过一定的距离,

POJ 3169 Layout (差分约束系统 + Bellmanford算法)

当然也有些牛关系不好,他们希望彼此超过一定的距离。有ML对牛的关系比较好,并给出每对牛的所不超过的距离D;同样,有MD对牛的关系不好,并给出每对牛的所超过的距离D。问是否有满足这样的安排方案满足所有牛的要求。若不存在,输出-1;若存在,但是牛1和牛N之间的距离可以任意大,输出-2;否则,输出牛1和牛N之间的最大距离。

解析:记第i号牛的位置是d[i],首先,牛市按照编号顺序排的,所以有d[i]<= d[i+1]成立。其次,对于关系好的牛的最大距离限制有d[AL] + DL >= d[BL],同样对于关系不好的牛的最小距离限制有d[AD] + DD<= d[BD]。这样,原来的问题就可以转化为在满足着三个不等式组的情况下,求解d[N] - d[1]的最大值问题。当然可以用线性规划的方法去解决。但是这道题还可以更简单的求解。这个可以抽象为一个无向图,各个牛为顶点,他们之间的好或不好的关系为边,限制的距离作为边尚德权值。这样可以将这三个不等式转化一下:d[i+1] + 0 >= d[i]表示从i+1到i连一条权值为0的边,d[AL] + DL >= d[BL]表示从AL到BL连一条权值为DL的边,d[BD] - DD >= d[AD]表示从BD到AD连一条权值为-DD的边。所求d[N] - d[1]的最大值,对应于d[1]到d[n]之间的最短路径。由于存在负权边,所以用bellman-ford算法。

AC代码:

#include#include#include#include #include#include#include#include#include#include#include#include#includeusing namespace std;#define INF 123456789#define LL long long#define MID(a, b) a+(b-a)/2const int maxn = 1000 + 10;const int maxm = 10000 + 10;int al[maxm], bl[maxm], dl[maxm];int ad[maxm], bd[maxm], dd[maxm];int d[maxn];int N, ML, MD;void solve{ fill(d, d+N, INF); //初始化d[] d[0] = 0; for(int i=0; i

篇7:动态差分GPS

动态差分GPS(DynamicdifferentialGPS)是由一个或多个控制站(或参考站)传送讯号改正值,以提供使用者进行实时改正之技术,

动态差分GPS

篇8:静态差分GPS

静态差分GPS(StaticdifferentialGPS)是由两个(含)以上接收仪,进行较长时间(通常为半小时以上)的测量,其包含了一组接收仪间基线向量的决定,

静态差分GPS

篇9:最优差分方案

最优差分方案

在数值预报和数值模拟中,描述空间微分项的最主要的方法是有限差分法,但使用差分方法会引入截断误差.伍荣生1979年指出,通过在原物理场的基础上构造一个新的物理场,替代原物理场进行差分计算,可以达到减小误差的目的.该文是伍荣生1979年工作的继续,目的在于解释伍荣生1979年所构造的.差分格式并得到更为一般化的差分格式.文中给出新的差分格式结合了经典有限差分方法的快速计算和谱方法的高精度的优点.如果在一个给定的网格上对气象要素场进行离散傅里叶级数展开,则基函数(正弦或余弦)的频谱是事免已知的.作者将伍荣生1979年构造物理场的方法视为对物理场的一次平滑,探讨了获取二次平滑场、多次平滑的一般化方法.获取平滑场的基奉原理是使得在固定频谱上的差分逼近程度达到最优.通过对频谱上的累计误差的下降速度分析表明,平滑次数的上限为3次.数值分析的结果表明,二次平滑的最大误差是未作任何平滑的最大误差的0.04倍,在使用相同计算代价的情况下,二次平滑的最大误差是经典的差分格式的0.3倍.平流试验的结果也表明,新的差分格式即一次平滑、二次平滑方案的结果远远优于经典的差分格式.新的差分格式意义在于,在不加密网格的情况下提供了一条提高数值计算精度的途径.

作 者:黄文誉 伍荣生 HUANG Wenyu WU Rongsheng  作者单位:南京大学大气科学系,中尺度灾害性天气教育部重点实验室,南京,210093 刊 名:气象学报  ISTIC PKU英文刊名:ACTA METEOROLOGICA SINICA 年,卷(期): 67(6) 分类号:P456.7 关键词:差分逼近程度   平滑   频谱   中短波   累积误差   Difference accuracy   Smoothing   Frequency spectrum   Short and middle wave   Cumulative error  

篇10:差分格式的稳定性讨论

关于差分格式的稳定性讨论

本文在差分格式稳定性的概念的基础上,按照稳定性的.定义来验证某个差分格式,验证差分格式是否稳定,往往比较复杂.讨论稳定性有很多方法,如矩阵方法,离散扰动方法.Hitt呲启示性方法和Fouder方法等等.每个方法各有优劣,本文讨论应用最广泛,也较为方便的Fourier方法.

作 者:李卓识  作者单位:吉林农业大学,吉林,长春,130118 刊 名:网络财富 英文刊名:INTEMET FORTUNE 年,卷(期):2009 “”(23) 分类号:G64 关键词:差分格式稳定性   Lax等价定理   Foutier方法  

【POJ 3159 Candies 还是差分约束(栈的SPFA)】相关文章:

1.离婚选择分协议还是诉讼

2.浅析GPS差分算法及数据处理

3.StarFireTM差分GPS技术引进及水利行业应用

4.一类高阶有理差分方程的全局渐近稳定性

5.振动杆有限差分模型的一类特征值反问题

下载word文档
《POJ 3159 Candies 还是差分约束(栈的SPFA).doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度: 评级1星 评级2星 评级3星 评级4星 评级5星
点击下载文档

文档为doc格式

POJ 3159 Candies 还是差分约束(栈的SPFA)相关文章
最新推荐
猜你喜欢
  • 返回顶部