标签:
裸最大权封闭子图
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(int i=l;i<=r;i++) 3 #define dec(i,l,r) for(int i=l;i>=r;i--) 4 #define link(x) for(edge *j=h[x];j;j=j->next) 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define inf 1e9 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define NM 60000 10 #define nm 400000 11 using namespace std; 12 int read(){ 13 int x=0,f=1;char ch=getchar(); 14 while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();} 15 while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar(); 16 return x*f; 17 } 18 struct edge{ 19 int t,v; 20 edge *next,*rev; 21 }e[nm],*h[NM],*o=e,*p[NM],*tmp[NM]; 22 void _add(int x,int y,int v){ 23 o++;o->t=y;o->v=v;o->next=h[x];h[x]=o; 24 } 25 void add(int x,int y,int v){ 26 _add(x,y,v);_add(y,x,0); 27 h[x]->rev=h[y];h[y]->rev=h[x]; 28 } 29 int n,m,_x,_y,_t,_s,d[NM],cnt[NM]; 30 int maxflow(){ 31 int flow=0;edge *j; 32 inc(i,0,n)tmp[i]=h[i]; 33 cnt[0]=n+1; 34 for(int x=0,s=inf;d[x]<n+1;){ 35 for(j=tmp[x];j;j=j->next) 36 if(j->v&&d[x]==d[j->t]+1)break; 37 if(j){ 38 s=min(s,j->v);tmp[x]=p[j->t]=j; 39 if((x=j->t)==n){ 40 for(;x;x=p[x]->rev->t) 41 p[x]->v-=s,p[x]->rev->v+=s; 42 flow+=s;s=inf; 43 } 44 }else{ 45 if(!--cnt[d[x]])break; 46 d[x]=n+1; 47 link(x) 48 if(j->v&&d[x]>d[j->t]+1){ 49 d[x]=d[j->t]+1; 50 tmp[x]=j; 51 } 52 cnt[d[x]]++; 53 if(x)x=p[x]->rev->t; 54 } 55 } 56 return flow; 57 } 58 int main(){ 59 freopen("data.in","r",stdin); 60 n=read();m=read(); 61 inc(i,1,n)add(0,i,read()); 62 inc(i,1,m){ 63 _x=read();_y=read();_t=read();_s+=_t; 64 add(_x,i+n,inf);add(_y,i+n,inf);add(i+n,n+m+1,_t); 65 } 66 n=n+m+1; 67 printf("%d\n",_s-maxflow()); 68 return 0; 69 }
标签:
原文地址:http://www.cnblogs.com/onlyRP/p/5171157.html