POJ3159_Candies_差分约束_SPFA

来源:互联网 发布:只有中国网络有墙吗 编辑:程序博客网 时间:2024/04/26 05:49

/*

吐槽

这道题又是自己没好好想就去网上查题解去了- -发现是一道差分约束的题,以前没听说过这个啊-  -没办法就去百科了一下什么是差分约束,发现是转化成最短路径来解。

然后看题解上说都是最短路径,自己以为是最长路,很是郁闷- -后来自己推了推,算是半懂不懂的理解了把。

本来计划直接做呢,但是发现大家说用队列会超时,必须得用栈- -没有用过啊!!看了代码发现除了定义不一样,其他几乎一样的,这样,栈,队列,优先队列三种都做过至少一次了,形式都很像。

后悔查代码了,如果自己做的话肯定会用队列,然后TLE一次才印象深刻,这样看别人题解规避自己可能犯的错误感觉心里很别扭- -

(Ps:刚刚亲测一遍,就是把定义改一下,然后Q.pop()改成Q.front()就好了,果然TLE- -,不过心里舒坦了一些)

唉,总之昨天没做完,晚上去看09视频了,这几天效率太低,被同学拉下一周的内容了,要好好加把劲才行啊

*/

题意

        从1到N,N个点之间有关系,A,B,c代表A不能忍受B能比他多 超过c个糖果,也就是B<=A+c,给出这些关系,让你求N最多能比1多多少个糖果

思路:

          然后把这些关系转化成一个图,用最短路径解,原理与步骤详见百度百科差分约束词条//具体于本题的转化思路在代码的注释里

/*

         如果一个系统由n个变量和m个约束条件组成,其中每个约束条件形如xj-xi<=bk(i,j∈[1,n],k∈[1,m]),则称其为差分约束系统(system of difference constraints)。亦即,差分约束系统是求解关于一组变量的特殊不等式组的方法。
         求解差分约束系统,可以转化成图论的单源最短路径(或最长路径)问题。
观察xj-xi<=bk,会发现它类似最短路中的三角不等式d[v]<=d[u]+w[u,v],即d[v]-d[u]<=w[u,v]。因此,以每个变量xi为结点,对于约束条件xj-xi<=bk,连接一条边(i,j),边权为bk。我们再增加一个源点s,s与所有定点相连,边权均为0。对这个图,以s为源点运行Bellman-ford算法(或SPFA算法),最终{d[ i]}即为一组可行解。

转载于百度百科http://baike.baidu.com/view/1008149.htm

*/



原题:

Candies
Time Limit: 1500MS Memory Limit: 131072KTotal Submissions: 20216 Accepted: 5346

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 integersN andM not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 throughN. snoopy and flymouse were always numbered 1 andN. Then followM lines each holding three integers A, B and c in order, meaning that kidA 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 21 2 52 1 4

Sample Output

5

Run IDUserProblemResultMemoryTimeLanguageCode LengthSubmit Time11761910chengtbf3159Accepted2112K688MSC++1706B2013-07-11 09:15:17


代码:


/*刚开始以为是转化成最长路,因为题中是求最大差值嘛,但是网上一看都是最短路,十分困惑,就自己推一下输入是A->B(c),建立有向边,表示B<=A+c才成立,即A>=B+(-c)才成立。要求最大的差值N-1,即1-N的绝对值最大。所以有方程if(A<B+(-c))A=B+(-c),让差值尽可能大。如果A>B+(-c),那本身就满足条件,不需要松弛上面的松弛条件转化一下就是if(B>A+c)B=A+c;其实这里并不是严格的最短路径,因为状态松弛的条件跟正常的最短路径是反的,因为正常的有向边是if(A>B+C)A=B+c;而这里A、B相反,所以其实是用最短路径的模版求最大差值初始化自己刚开始也没想好,但是现在推一遍感觉有点眉目了,2到N初始化为MAXN,表示假设刚开始差了无穷大(显然实际是不可能的),但是找其中满足情况的最小路径,就是找满足条件的最大差值*/#include<cstdio>#include<queue>#include<stack>#include<algorithm>#define MAXN 2000000000//悲催啊,题中没给差值的范围- -所以尽量开大点,到20亿using namespace std;typedef struct MyStruct{int to,next,val;}EDGE;EDGE edge[150010];//边数组int head[30010];int dis[30010];//存点1到点i允许的最大差值int count_num;int flag[30010];//标记数组标记是否在栈中stack<int>Q;void addedge(int a,int b,int w){edge[count_num].to=b;edge[count_num].next=head[a];edge[count_num].val=w;head[a]=count_num++;}void SPFA(){while (!Q.empty()){Q.pop();}int temp,son,i;Q.push(1);while (!Q.empty()){temp=Q.top();flag[temp]=0;Q.pop();for ( i = head[temp]; i !=-1; i=edge[i].next){son=edge[i].to;if (dis[son]>dis[temp]+edge[i].val){dis[son]=dis[temp]+edge[i].val;if (flag[son]==0){Q.push(son);flag[son]=1;}}}}}int main(){int a,b,w,n,m,i;while (scanf("%d%d",&n,&m)!=EOF){count_num=1;//初始化dis[1]=0;memset(flag,0,sizeof(flag));memset(head,-1,sizeof(head));for ( i = 2; i <=n ; i++){dis[i]=MAXN;}//输入for ( i = 1; i <=m ; i++){scanf("%d%d%d",&a,&b,&w);addedge(a,b,w);}//处理SPFA();//输出printf("%d\n",dis[n]);}return 0;}




原创粉丝点击