poj 3159 Candies 差分约束+SPFA+栈优化

前端之家收集整理的这篇文章主要介绍了poj 3159 Candies 差分约束+SPFA+栈优化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 14575 Accepted: 3742

Description

During the kindergarten days,flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did,he should never get a certain number of candies fewer than B did no matter how many candies he actually got,otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.

snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher,what was the largest difference he could make out of it?

Input

The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A,B and c in order,meaning that kid A believed that kid B should never get over c candies more than he did.

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

Sample Input

2 2
1 2 5
2 1 4

Sample Output

5

Hint

32-bit signed integer type is capable of doing all arithmetic.
本题是一道典型的差分约束系统的题目,题目原意是这样子的:
fq是幼稚园班上的老大,一天老师给小朋友们买了一堆的糖果,由fq来分发,在班上,fq和llw是死对头,两人势如水火,不能相容,因此fq希望自己分得的糖果数尽量多余llw,而对于其他小朋友而言,不患寡而患不均的意识甚是强烈,比如A小朋友强烈希望自己的糖果数不能少于B小朋友m个,即B-A<=m,A,B分别为A、B小朋友的分得的糖果数。如此,班上的小朋友存在若干这样的意识,题目要求你在满足其他小朋友的要求的情况下,使fq和llw分得的糖果数差别最大,即max(VN-V1),VN为fq获得的糖果数,V1为llw获得的糖果数。
因此根据题意,可以列出如下的不等式:
Vb1-Va1 <= a1b1
Vb2-Va2 <= a1b2 (1)
.....
Vbm-Vam <= ambm
目标函数为:max(VN-V1)
题目分析:(1)中的不等式正好可以构成最短路的三角不等式:du <= dv + w(v,w)(其中du、dv分为为u v点到起点的最短路经),因此可以利用最短路经来解求该不等式的解,同时利用最短路经得到的解能满足max{dn-d1},这一点在上一篇评论中得到证明(因为图上的每个点的最短路经都是由其前驱节点(已计算出最短路径的节点)经过该点和前驱节点的边松弛得到的),因此最短路经得到的解X >= 满足该不等式的解。由于我们考虑的只是小朋友分得的糖果数的相对量,而不关心其绝对量,因此我们可以假定d1=0,即llw获得0个糖果数。因此d1==0,而dN达到最大值,固能满足{dN-d1}达到最大值。
这道题与poj1716 Integer Intervals、poj1201 Intervals不同 还在于:该题不需要添加虚拟节点V0,因为V1可以从某种程度认为是虚拟节点,即起点。虚拟节点V0在差分约束系统中的作用是作为一个基准量,其他未知数以此基准量并根据不等式条件来得到解,这有点类似于水准高的定义,只是一个相对量。这里得声明下:虚拟节点的添加是为保证其他每个顶点均可从V0到达,从而在V0==0的情况下求解其他未知数,即只能以V0为起点,不能使用其他点作为起点,即其他点的未知量不为0。而本题,可以直接令V1=0,即以V1为起点,因为题目考虑的是VN-V1的相对量,因此,直接让V1=0,让V1成为起点,便可以。可以说,题目分析到这里,已经完成了1/3,后面的工作量还是比较大的。
同时这道题还有一个难度在于数据量比较大,点有30000个,边有150000条,如果采用bellman_ford算法,需要的时间开销为30000*150000=4.5*e9,要在1s的时间算完比较困难。那么可以用dijkstra么?当然可以,因为图中的路径权都是正的,因此可以用dijkstra,但问题是dijkstra的时间复杂度为o(V*V+E)=9*e8,与bellman_ford算法其实相差无几,因此还需要优化。所以在使用dijkstra算法的同时,利用最小二分堆来实现最小优先队列的维护,其时间复杂度为o(ElgV)=1.5*lg(30000)*e5~近似等于=1.5*128*sqrt(2)*e5,比9*e8小了小了一个数量级,实践证明,程序运行时间约等于1s,刚好过,比较险。
在《算法导论》中提到,利用斐波那契堆来实现优先队列,可以将运行时间提升到o(VlgV+E),目前还不知道斐波那契堆,有机会要摸一摸,体现一下告诉最短路经的快感。

#include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; const int maxn=1001000; const int inf=(1<<27); struct edge//邻接表 { int t,w;//s->t=w; int next;//数组模拟指针 }; int p[maxn];//邻接表头节点 edge G[maxn];//邻接表 int V,E;//点数[1-n] 边数 int dis[maxn]; int stc[maxn],top;//模拟队列 int vis[maxn]; int instc[maxn];//入队次数 bool spfa(int s0) { top=0; for(int i=1;i<=V;i++) dis[i]=inf;dis[s0]=0; memset(vis,sizeof(vis)); memset(instc,sizeof(instc)); stc[++top]=s0;vis[s0]=1;instc[s0]++; while(top>0) { int u=stc[top];top--; vis[u]=0; for(int i=p[u];i!=-1;i=G[i].next) { int s=u,t=G[i].t,w=G[i].w; if(dis[t]>dis[s]+w) { dis[t]=dis[s]+w; if(vis[t]==0) { stc[++top]=t,vis[t]=1; instc[t]++; if(instc[t]>V) return false; } } } } return true; } int main() { while(scanf("%d%d",&V,&E)==2) { memset(p,-1,sizeof(p)); for(int i=0;i<E;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); //u->v=w; G[i].t=v; G[i].w=w; G[i].next=p[u]; p[u]=i; } spfa(1); printf("%d/n",dis[V]); } return 0; }
原文链接:https://www.f2er.com/vb/261335.html

猜你在找的VB相关文章