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

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

生活随笔

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

编程问答

bzoj3436小K的农场

發(fā)布時(shí)間:2024/3/24 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bzoj3436小K的农场 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

bzoj3436小K的農(nóng)場(chǎng)

題意:

n個(gè)數(shù),知道m(xù)條關(guān)系:a-b≥c、a-b≤c或a==b。問(wèn)是否存在滿足所有關(guān)系的情況。n≤10000,m≤10000。

題解:

差分約束。因?yàn)橹灰笫欠駶M足,因此最短路最長(zhǎng)路都可以。不過(guò)要注意如果是用spfa的bfs寫法,每個(gè)點(diǎn)都必須作為源點(diǎn)判一次負(fù)環(huán),因?yàn)閳D可能不連通。正因?yàn)槿绱?#xff0c;雖說(shuō)加了SLF的bfs寫法spfa能卡過(guò),但比dfs寫法慢不只10倍。

代碼:

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 #define ll long long 6 #define inc(i,j,k) for(int i=j;i<=k;i++) 7 #define maxn 10010 8 #define INF 0x3fffffff 9 using namespace std; 10 11 inline int read(){ 12 char ch=getchar(); int f=1,x=0; 13 while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} 14 while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); 15 return f*x; 16 } 17 18 struct e{int t,w,n;}; e es[maxn*40]; int g[maxn],ess; 19 void pe(int f,int t,int w){es[++ess]=(e){t,w,g[f]}; g[f]=ess;} 20 int n,m,cnt[maxn],d[maxn]; bool inq[maxn]; deque<int>q; 21 ll spfa(int s){ 22 q.clear(); memset(inq,0,sizeof(inq)); memset(cnt,0,sizeof(cnt)); 23 q.push_back(s); inq[s]=1; d[s]=0; cnt[s]=1; 24 while(!q.empty()){ 25 int x=q.front(); q.pop_front(); inq[x]=0; 26 for(int i=g[x];i;i=es[i].n)if(d[es[i].t]>d[x]+es[i].w){ 27 d[es[i].t]=d[x]+es[i].w; 28 if(!inq[es[i].t]){ 29 if(!q.empty()&&d[es[i].t]<d[q.front()])q.push_front(es[i].t);else q.push_back(es[i].t); 30 inq[es[i].t]=1; cnt[es[i].t]++; if(cnt[es[i].t]>=n)return 0; 31 } 32 } 33 } 34 return 1; 35 } 36 int main(){ 37 n=read(); m=read(); 38 inc(i,1,m){ 39 int opt=read(),a,b,c; 40 if(opt==1)a=read(),b=read(),c=read(),pe(a,b,-c); 41 if(opt==2)a=read(),b=read(),c=read(),pe(b,a,c); 42 if(opt==3)a=read(),b=read(),pe(b,a,0),pe(a,b,0); 43 } 44 inc(i,1,n)if(!spfa(i)){puts("No"); return 0;} puts("Yes"); return 0; 45 }

?

20161018

轉(zhuǎn)載于:https://www.cnblogs.com/YuanZiming/p/5982007.html

總結(jié)

以上是生活随笔為你收集整理的bzoj3436小K的农场的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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