分析:
一开始一直Wa,发现是建图建错了,必须得拆点。
S连i,流量为a[i],费用为0,i+n连T,流量同上,费用为0,之后i连i+1费用为0,流量为inf,之后S连n*2+i,流量为li,费用为0,之后枚举j从1到n,n*2+i连接j+n,费用为p[i],之后i连接i+d[i]+1,费用为q[i],流量为inf,之后跑费用流就可以了。
附上代码:
#include <cstdio> #include <cmath> #include <algorithm> #include <cstdlib> #include <cstring> #include <queue> #include <iostream> using namespace std; #define N 155 #define S 0 #define T 151 struct node { int to,next,val,flow,from; }e[N*200]; int head[N],cnt,dis[N],vis[N],from[N],ans,n,m,k; void add(int x,int y,int z,int v) { e[cnt].to=y,e[cnt].next=head[x],e[cnt].val=v; e[cnt].flow=z,e[cnt].from=x,head[x]=cnt++; } void insert(int x,int y,int z,int v) { add(x,y,z,v);add(y,x,0,-v); } int spfa() { memset(dis,0x3f,sizeof(dis));memset(from,-1,sizeof(from)); queue <int>q;q.push(S);dis[S]=0; while(!q.empty()) { int x=q.front();q.pop();vis[x]=0; for(int i=head[x];i!=-1;i=e[i].next) { int to1=e[i].to; if(e[i].flow&&dis[to1]>dis[x]+e[i].val) { from[to1]=i; dis[to1]=dis[x]+e[i].val; if(!vis[to1])vis[to1]=1,q.push(to1); } } } return dis[T]==0x3f3f3f3f?0:1; } void mcf() { int x,i=from[T]; while(i!=-1){x=min(e[i].flow,x);i=from[e[i].from];} i=from[T]; while(i!=-1){e[i].flow-=x,e[i^1].flow+=x,ans+=x*e[i].val,i=from[e[i].from];} return ; } int a[N]; int main() { int t; scanf("%d",&t); for(int cas=1;cas<=t;cas++) { printf("Case %d: ",cas); int v=0;ans=0; memset(head,-1,sizeof(head));cnt=0; scanf("%d%d%d",&n,&m,&k); for(int i=1;i<n;i++)insert(i,i+1,1<<30,0); for(int i=1;i<=n;i++) { int x; scanf("%d",&x);a[i]=x; insert(S,i,x,0); insert(i+n,T,x,0); } for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); insert(S,2*n+i,x,0); for(int j=1;j<=n;j++)insert(2*n+i,j+n,x,y); } for(int i=1;i<=k;i++) { int x,y; scanf("%d%d",&x,&y); for(int j=1;j+x+1<=n;j++) { insert(j,j+n+x+1,1<<30,y); } } while(spfa())mcf(); for(int i=head[T];i!=-1;i=e[i].next) { if(e[i^1].flow!=0) { v=1; puts("impossible"); break; } } if(!v)printf("%d ",ans); } return 0; }