日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

POJ 3159 Candies(差分约束+SPAF)

發(fā)布時(shí)間:2023/12/4 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3159 Candies(差分约束+SPAF) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題意:

給n個(gè)小朋友分發(fā)糖果,但小朋友們之間有嫉妒心。接下來(lái)m行,每行三個(gè)數(shù),分別表示小朋友A希望B得到的糖果不能比他多x個(gè)。要求你計(jì)算在滿(mǎn)足所有小朋友的條件的情況下最多需要準(zhǔn)備多少顆糖。

題目:

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.

分析:

1.這道題目是典型的差分約束系統(tǒng),小于等于是最短路 大于等于是最長(zhǎng)路,核心在于:xj-xi<=bk,會(huì)發(fā)現(xiàn)它類(lèi)似最短路中的三角不等式d[v]<=d[u]+w[u,v],即d[v]-d[u]<=w[u,v],即從u指向v建邊,num[B]<=num[A]+x。求最大值的話(huà)就是跑1-n的最短路,對(duì)應(yīng)求最小值就是跑1-n的最長(zhǎng)路。此題用隊(duì)列的話(huà)會(huì)T掉,可以改成棧或者想辦法優(yōu)化。
2.關(guān)于差分約束選擇隊(duì)列還是堆棧:當(dāng)存在回路,SPFA的隊(duì)列實(shí)現(xiàn)會(huì)超時(shí),堆棧實(shí)現(xiàn)可以,堆棧實(shí)現(xiàn)SPFA(有時(shí)候堆棧確實(shí)比較快)
都大三的老阿姨了,差分約束還不熟練,加油吧,查缺補(bǔ)漏中。。。。

AC代碼

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int inf=0x3f3f3f3f; const int M=3e4+10; const int N=15e4+10; int n,m,k,a,b,c; int vis[M]/**在隊(duì)列標(biāo)志*/; int head[M]/**每個(gè)結(jié)點(diǎn)的頭指針(或者編號(hào))*/; int q[M]/**堆棧*/,dis[M]; struct node {int b;int c;int next; }edge[N];void add(int a,int b,int c)//加邊 {edge[k].b=b;edge[k].c=c;edge[k].next=head[a];head[a]=k++; } void SPFA(int start,int n) {int top=0;for(int i=1;i<=n;i++)//初始化{if(i==start){q[top++]=i;//入棧vis[i]=true;dis[i]=0;}else{vis[i]=false;dis[i]=inf;}}while(top!=0){int a=q[--top];vis[a]=false;for(int i=head[a];i!=-1;i=edge[i].next){int b=edge[i].b;if(dis[b]>dis[a]+edge[i].c){dis[b]=dis[a]+edge[i].c;if(!vis[b]){vis[b]=true;q[top++]=b;}}}} } int main() {while(~scanf("%d%d",&n,&m)){k=0;//加邊計(jì)數(shù),這個(gè)不要忘memset(head,-1,sizeof(head));for(int i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);add(a,b,c);}SPFA(1,n);//printf("*****\n");printf("%d\n",dis[n]);//此處求n比1最多多多少糖果}return 0; } /**給n個(gè)人派糖果,給出m組數(shù)據(jù),每組數(shù)據(jù)包含A、B、c 三個(gè)數(shù), 意思是A的糖果數(shù)比B少的個(gè)數(shù)不多于c,即B的糖果數(shù) - A的糖果數(shù)<= c 。 最后求n 比 1 最多多多少糖果。*/

總結(jié)

以上是生活随笔為你收集整理的POJ 3159 Candies(差分约束+SPAF)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。