bzoj3436小K的农场
生活随笔
收集整理的這篇文章主要介紹了
bzoj3436小K的农场
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
bzoj3436小K的農場
題意:
n個數,知道m條關系:a-b≥c、a-b≤c或a==b。問是否存在滿足所有關系的情況。n≤10000,m≤10000。
題解:
差分約束。因為只要求是否滿足,因此最短路最長路都可以。不過要注意如果是用spfa的bfs寫法,每個點都必須作為源點判一次負環,因為圖可能不連通。正因為如此,雖說加了SLF的bfs寫法spfa能卡過,但比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
轉載于:https://www.cnblogs.com/YuanZiming/p/5982007.html
總結
以上是生活随笔為你收集整理的bzoj3436小K的农场的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 家政/保洁 平台设计
- 下一篇: 数学编辑器——AxMath