POJ 1275 差分约束
一堆化不等式,,,,,,
懒得写了。。。
请看mars_ch的blog…..
还是她给我找的错……….
//By SiriusRen
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int first[25],next[666],v[666],w[666],tot,ans;
int n,cases,have[25],need[25],jy,inq[25],vis[25],dis[25];
void add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
bool spfa(){
queue<int>q;q.push(0);
while(!q.empty()){
int t=q.front();q.pop();
vis[t]++,inq[t]=0;
for(int i=first[t];~i;i=next[i])
if(dis[v[i]]<dis[t]+w[i]){
dis[v[i]]=dis[t]+w[i];
if(!inq[v[i]])inq[v[i]]=1,q.push(v[i]);
if(vis[v[i]]>24)return 0;
}
}
if(dis[24]==ans)return 1;return 0;
}
int main(){
// freopen("data.txt","r",stdin);
scanf("%d",&cases);
while(cases--){
memset(have,0,sizeof(have));
for(int i=1;i<=24;i++)scanf("%d",&need[i]);
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&jy),have[jy+1]++;
for(ans=0;ans<=n;ans++){
memset(dis,0xcf,sizeof(dis));memset(vis,0,sizeof(vis));
tot=0,memset(first,-1,sizeof(first)),memset(inq,0,sizeof(inq));
dis[0]=0;
for(int i=1;i<=24;i++)add(i-1,i,0),add(i,i-1,-have[i]);
for(int i=9;i<=24;i++)add(i-8,i,need[i]);
add(0,24,ans);
for(int i=1;i<=8;i++)add(i+16,i,need[i]-ans);
if(spfa()){printf("%d\n",ans);goto end;}
}
puts("No Solution");
end:;
}
}