标签:
具体参考http://blog.csdn.net/power721/article/details/6665750
TODO
1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #define N 8005 5 #define M 1000005 6 #define inf 999999999 7 #define min(a,b) ((a)<(b)?(a):(b)) 8 using namespace std; 9 10 int n,m,s,t,num; 11 int adj[N],dis[N],q[N]; 12 vector<int> st[1005],ed[1005]; 13 struct edge{ 14 int v,w,pre; 15 }e[M]; 16 void insert(int u,int v,int w){ 17 e[num]=(edge){v,w,adj[u]}; 18 adj[u]=num++; 19 e[num]=(edge){u,0,adj[v]}; 20 adj[v]=num++; 21 } 22 23 int bfs(){ 24 int i,x,v,head=0,tail=0; 25 memset(dis,0,sizeof(dis)); 26 dis[s]=1; 27 q[tail++]=s; 28 while(head<tail){ 29 x=q[head++]; 30 for(i=adj[x];~i;i=e[i].pre) 31 if(e[i].w&&!dis[v=e[i].v]){ 32 dis[v]=dis[x]+1; 33 if(v==t) 34 return 1; 35 q[tail++]=v; 36 } 37 } 38 return 0; 39 } 40 int dfs(int x,int limit){ 41 if(x==t) 42 return limit; 43 int i,v,tmp,cost=0; 44 for(i=adj[x];~i&&cost<limit;i=e[i].pre) 45 if(e[i].w&&dis[x]==dis[v=e[i].v]-1){ 46 tmp=dfs(v,min(limit-cost,e[i].w)); 47 if(tmp){ 48 e[i].w-=tmp; 49 e[i^1].w+=tmp; 50 cost+=tmp; 51 } 52 else 53 dis[v]=-1; 54 } 55 return cost; 56 } 57 int Dinic(){ 58 int ans=0; 59 while(bfs()) 60 ans+=dfs(s,inf); 61 return ans; 62 } 63 64 int main(){ 65 while(scanf("%d%d",&n,&m),n+m){ 66 int i,j,k,u,v,c,w[5005]={0},sum=0; 67 num=0; 68 memset(adj,-1,sizeof(adj)); 69 s=0; 70 t=1; 71 for(i=1;i<=n;i++){ 72 st[i].clear(); 73 ed[i].clear(); 74 } 75 for(i=1;i<=m;i++){ 76 scanf("%d",w+i); 77 sum+=w[i]; 78 insert(i+1,t,w[i]); 79 } 80 scanf("%d",&k); 81 for(i=1;i<=k;i++){ 82 scanf("%d%d%d%d",&u,&v,&c,&w[0]); 83 insert(s,i+m+1,w[0]); 84 insert(i+m+1,c+1,inf); 85 st[u].push_back(c); 86 ed[v].push_back(c); 87 } 88 for(i=1;i<=n;i++){ 89 for(j=0;j<ed[i].size();j++){ 90 for(k=0;k<st[i].size();k++){ 91 insert(st[i][k]+1,ed[i][j]+1,inf); 92 //printf("%d %d %d\n",i,ed[i][j],st[i][k]); 93 } 94 } 95 } 96 printf("%d\n",sum-Dinic()); 97 } 98 }
标签:
原文地址:http://www.cnblogs.com/wushuaiyi/p/4234987.html