poj1275 Cashier Employment
ZZQ大神用的网络流
我写了差分约束
题解传送门:http://www.cnblogs.com/jackge/archive/2013/04/13/3017790.html
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<set> 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 14 #define re(i,l,r) for(int i=(l);i<=(r);i++) 15 #define Clear(a,b) memset(a,b,sizeof(a)) 16 #define inout(x) printf("%d",(x)) 17 #define douin(x) scanf("%lf",&x) 18 #define strin(x) scanf("%s",(x)) 19 #define LLin(x) scanf("%lld",&x) 20 #define op operator 21 #define CSC main 22 typedef unsigned long long ULL; 23 typedef const int cint; 24 typedef long long LL; 25 using namespace std; 26 cint inf=2147483647; 27 void inin(int &ret) 28 { 29 ret=0;int f=0;char ch=getchar(); 30 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 31 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 32 ret=f?-ret:ret; 33 } 34 int t,w[25],r[25],n,head[33],next[2020],zhi[2020],v[2020],ed; 35 void add(int a,int b,int c) 36 { 37 next[++ed]=head[a],head[a]=ed,zhi[ed]=b,v[ed]=c; 38 } 39 queue<int>h;bool bo[33];int shu[33],dis[33],temp; 40 bool spfa(int sum) 41 { 42 re(i,0,29)dis[i]=inf,shu[i]=0,bo[i]=0; 43 dis[24]=0;shu[24]=1; 44 while(!h.empty())h.pop(); 45 h.push(24); 46 while(!h.empty()) 47 { 48 int x=h.front();h.pop();bo[x]=0; 49 if(shu[x]>25)return 0; 50 for(int i=head[x];i;i=next[i]) 51 if(dis[zhi[i]]>dis[x]+v[i]) 52 { 53 dis[zhi[i]]=dis[x]+v[i]; 54 if(!bo[zhi[i]]) 55 { 56 h.push(zhi[i]); 57 bo[zhi[i]]=1; 58 shu[zhi[i]]++; 59 } 60 } 61 } 62 return dis[0]=-sum,1; 63 } 64 void build(int sum) 65 { 66 Clear(head,0);ed=0; 67 re(i,1,24) 68 { 69 add(i-1,i,r[i]); 70 add(i,i-1,0); 71 if(i>=8)add(i,i-8,-w[i]); 72 } 73 re(i,1,7)add(i,i+16,sum-w[i]); 74 add(24,0,-sum); 75 } 76 int CSC() 77 { 78 inin(t); 79 while(t--) 80 { 81 re(i,1,24)inin(w[i]); 82 Clear(r,0); 83 inin(n);int rr=n; 84 while(n--) 85 { 86 int x;inin(x); 87 r[x+1]++; 88 }int l=0; 89 int ans=inf; 90 while(l<=rr) 91 { 92 int mid=(l+rr)>>1; 93 build(mid); 94 if(spfa(mid)) 95 { 96 ans=mid; 97 rr=mid-1; 98 }else l=mid+1; 99 } 100 if(ans==inf)printf("No Solution\n"); 101 else printf("%d\n",ans); 102 } 103 return 0; 104 }