日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 在线观看亚洲色图 | av黄色网址 | 欧美亚一区二区三区 | 一级做a爰片久久毛片潮喷 天天透天天干 | 超碰在线免费 | 999久久久久 | 性做久久久久久久免费看 | 亚洲天堂日韩av | 成人免费一级片 | 国内自拍视频网站 | 色姑娘综合 | 亚洲精品中文字幕在线 | 中文字幕精品亚洲 | 黄色91在线观看 | 超碰在线播放97 | 国产又黄又粗又硬 | 极品国产91在线网站 | av资源导航 | 日本不卡在线观看 | 毛片网站免费观看 | 51精品国产人成在线观看 | 亚洲在线中文字幕 | 欧美午夜精品久久久久久人妖 | 精品熟妇无码av免费久久 | 国内精品免费 | 香港三级日本三级 | 艳妇av | 久久99久久99精品免观看软件 | 欧美毛片网站 | 美女靠逼app | 欧美最猛黑人xxxx黑人猛交 | 西欧毛片 | 青青青免费视频观看在线 | 亚洲第一在线视频 | 日韩一区二区精品 | 我要看黄色大片 | 久久精品一| 乖女从小调教h尿便器小说 欧美韩一区二区 | 在线观看视频一区 | 成人免费毛片xxx | 97色在线 | 欧美天天视频 | 91九色高潮 | 男女在线免费观看 | www.成人免费 | 91福利视频网站 | 欧美大片xxxx | 亚洲av日韩av高潮潮喷无码 | 久久mm | 亚洲 欧美 国产 另类 | av中出在线| 亚洲中国色老太 | 久久字幕| 精品无码人妻一区二区三 | 香蕉小视频 | 国产高清在线观看视频 | 原创av | 日韩精品一区二区亚洲av性色 | 欧美日韩精品一区二区在线观看 | 欧美三级在线看 | 椎名由奈在线观看 | 亚洲av综合色区 | 少妇全黄性生交片 | heyzo国产| 天堂av8在线| 欧美首页| 亚洲石原莉奈一区二区在线观看 | 国产一区二区精品 | 欧美蜜桃视频 | 国产资源一区 | 国产在线播放91 | 亚洲热在线 | 国产精品一卡二卡在线观看 | 日韩美女视频在线观看 | 久色电影| 99热免费在线| 国产在线网址 | 国产伦精品一区二区三区四区 | 黄色a∨| 超碰日日夜夜 | 亚洲人在线观看视频 | 视频一区日韩 | 天堂а√在线最新版中文在线 | 无码免费一区二区三区 | 精品国产黄色片 | 国产99久久久国产精品成人免费 | 日本精品久久久久久久 | 欧美日韩图片 | 超碰在线97观看 | 黄色片在线免费看 | 欧美成a| 国产区91 | 99精品一区二区三区无码吞精 | 成人小视频免费在线观看 | 特级淫片aaaaaaa级附近的 | 99精品视频免费看 | 欧美日本国产 | 狠狠躁日日躁夜夜躁av | 一区二区三区在线播放 |