Beta Round #9 (酱油杯noi考后欢乐赛)最大伤害

题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFnoi%E8%80%83%E5%90%8E%E6%AC%A2%E4%B9%90%E8%B5%9B%29/%E6%9C%80%E5%A4%A7%E4%BC%A4%E5%AE%B3

题解:发现我最小割还是不怎么会T_T

          而且此题题意有些不明。。。

          看到出题人说就是选出一大坨,然后能加的都加。卧槽,这不是最大权闭合子图吗?

         最大权闭合子图中原来有边的现在连边为inf,表示i与j必须同割,那我们连边为x,就代表i,j不同割需要扣除多少。

         然后求出最小割,用总权值减去最小割,好象是对的?然后就A掉了。。。

代码:

  1 #include<cstdio>
  2 
  3 #include<cstdlib>
  4 
  5 #include<cmath>
  6 
  7 #include<cstring>
  8 
  9 #include<algorithm>
 10 
 11 #include<iostream>
 12 
 13 #include<vector>
 14 
 15 #include<map>
 16 
 17 #include<set>
 18 
 19 #include<queue>
 20 
 21 #include<string>
 22 
 23 #define inf 1000000000
 24 
 25 #define maxn 2000+5
 26 
 27 #define maxm 1000000
 28 
 29 #define eps 1e-10
 30 
 31 #define ll long long
 32 
 33 #define pa pair<int,int>
 34 
 35 #define for0(i,n) for(int i=0;i<=(n);i++)
 36 
 37 #define for1(i,n) for(int i=1;i<=(n);i++)
 38 
 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 40 
 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 42 
 43 #define mod 1000000007
 44 
 45 using namespace std;
 46 
 47 inline int read()
 48 
 49 {
 50 
 51     int x=0,f=1;char ch=getchar();
 52 
 53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 54 
 55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 56 
 57     return x*f;
 58 
 59 }
 60 int  n,m,s,t,sum,maxflow,tot=1,a[maxn],b[maxn][maxn],head[maxn],cur[maxn],h[maxn],q[maxn];
 61 
 62 struct edge{int go,next,v;}e[maxm];
 63 
 64 void ins(int x,int y,int z){e[++tot].go=y;e[tot].v=z;e[tot].next=head[x];head[x]=tot;}
 65 
 66 void insert(int x,int y,int z){ins(x,y,z);ins(y,x,0);}
 67 
 68 bool bfs()
 69 
 70 {
 71 
 72     for(int i=s;i<=t;i++)h[i]=-1;
 73 
 74     int l=0,r=1;q[1]=s;h[s]=0;
 75 
 76     while(l<r)
 77 
 78     {
 79 
 80         int x=q[++l];
 81 
 82         for(int i=head[x];i;i=e[i].next)
 83 
 84          if(e[i].v&&h[e[i].go]==-1)
 85 
 86          {
 87 
 88             h[e[i].go]=h[x]+1;q[++r]=e[i].go;
 89 
 90          }
 91 
 92     }
 93 
 94     return h[t]!=-1;
 95 
 96 }
 97 
 98 int dfs(int x,int f)
 99 
100 {
101 
102     if(x==t) return f;
103 
104     int tmp,used=0;
105 
106     for(int i=cur[x];i;i=e[i].next)
107 
108      if(e[i].v&&h[e[i].go]==h[x]+1)
109 
110     {
111 
112         tmp=dfs(e[i].go,min(e[i].v,f-used));
113 
114         e[i].v-=tmp;if(e[i].v)cur[x]=i;
115 
116         e[i^1].v+=tmp;used+=tmp;
117 
118         if(used==f)return f;       
119 
120     }
121 
122     if(!used) h[x]=-1;
123 
124     return used;
125 
126 }
127 
128 void dinic()
129 
130 {
131 
132     maxflow=0;
133 
134     while(bfs())
135 
136     {
137 
138         for (int i=s;i<=t;i++)cur[i]=head[i];maxflow+=dfs(s,inf);
139 
140     }
141 
142 }
143 
144 int main()
145 
146 {
147 
148     freopen("input.txt","r",stdin);
149 
150     freopen("output.txt","w",stdout);
151 
152     n=read();m=read();s=0;t=n+m+1;
153     for1(i,n+m)a[i]=read()*2;
154     for1(i,n)for1(j,n)b[i][j]=read();
155     for1(i,n)for1(j,m)b[i][j+n]=b[j+n][i]=read();
156     for1(i,m)for1(j,m)b[i+n][j+n]=read();
157     for1(i,n+m)
158     {
159         int t1=0,t2=0;
160         if(a[i]>0)t1+=a[i],sum+=a[i];else t2+=abs(a[i]);
161         for1(j,n+m)if(b[i][j])
162         {
163             t1+=b[i][j];
164             if(b[i][j]>0)insert(i,j,b[i][j]),sum+=b[i][j];
165         }
166         if(t1)insert(s,i,t1);
167         if(t2)insert(i,t,t2);
168     }
169     dinic();
170     printf("%d
",(sum-maxflow)/2);
171 
172     return 0;
173 
174 } 
View Code
原文地址:https://www.cnblogs.com/zyfzyf/p/4114785.html