BZOJ3442: 学习小组

题解:

裸费用流,不过略有点坑。。。

因为只要求参加的人数多,并不要求参加的小组总和多。

所以我们可以不把k流量流满,又因为是最小费用最大流,所以我们连i-t容量为k-1的边即可。

代码;

  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 200000+5
 26 
 27 #define maxm 200000+5
 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 for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
 44 
 45 #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
 46 
 47 #define mod 1000000007
 48 
 49 using namespace std;
 50 
 51 inline int read()
 52 
 53 {
 54 
 55     int x=0,f=1;char ch=getchar();
 56 
 57     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 58 
 59     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 60 
 61     return x*f;
 62 
 63 }
 64 int n,m,k,mincost,tot=1,a[maxn],s,t,head[maxn],d[maxn],from[2*maxm];
 65 
 66 bool v[maxn];
 67 
 68 queue<int>q;
 69 
 70 struct edge{int from,go,next,v,c;}e[2*maxm];
 71 
 72 void add(int x,int y,int v,int c)
 73 
 74 {
 75 
 76     e[++tot]=(edge){x,y,head[x],v,c};head[x]=tot;
 77 
 78     e[++tot]=(edge){y,x,head[y],0,-c};head[y]=tot;
 79 
 80 }
 81 
 82 bool spfa()
 83 
 84 {
 85 
 86     for (int i=s;i<=t;i++){v[i]=0;d[i]=inf;}
 87 
 88     q.push(s);d[s]=0;v[s]=1;
 89 
 90     while(!q.empty())
 91 
 92     {
 93 
 94         int x=q.front();q.pop();v[x]=0;
 95 
 96         for (int i=head[x],y;i;i=e[i].next)
 97 
 98          if(e[i].v&&d[x]+e[i].c<d[y=e[i].go])
 99 
100          {
101 
102             d[y]=d[x]+e[i].c;from[y]=i;
103 
104             if(!v[y]){v[y]=1;q.push(y);}
105 
106          }
107 
108     }
109 
110     return d[t]!=inf;
111 
112 }
113 
114 void mcf()
115 
116 {
117 
118     mincost=0;
119 
120     while(spfa())
121 
122     {
123 
124         int tmp=inf;
125 
126         for(int i=from[t];i;i=from[e[i].from]) tmp=min(tmp,e[i].v);
127 
128         mincost+=d[t]*tmp;
129 
130         for(int i=from[t];i;i=from[e[i].from]){e[i].v-=tmp;e[i^1].v+=tmp;}
131 
132     }
133 
134 }
135 
136 int main()
137 
138 {
139 
140     freopen("input.txt","r",stdin);
141 
142     freopen("output.txt","w",stdout);
143 
144     n=read();m=read();k=read();s=0;t=n+m+1;
145     for1(i,m)
146     {
147         int x=read();
148         for1(j,n)add(n+i,t,1,(2*j-1)*x);
149     }
150     for1(i,m)a[i]=read();
151     for1(i,n)for1(j,m)
152     {
153         char ch=getchar();
154         while(ch!='0'&&ch!='1')ch=getchar();
155         if(ch=='1')add(i,n+j,1,-a[j]);
156     }
157     for1(i,n)add(s,i,k,0);
158     if(k-1)for1(i,n)add(i,t,k-1,0);
159     mcf();
160     cout<<mincost<<endl;
161 
162     return 0;
163 
164 }  
View Code

3442: 学习小组

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 115  Solved: 50
[Submit][Status]

Description

【背景】
坑校准备鼓励学生参加学习小组。
【描述】
    共有n个学生,m个学习小组,每个学生有一定的喜好,只愿意参加其中的一些学习小组,但是校领导为学生考虑,规定一个学生最多参加k个学习小组。财务处的 大叔就没那么好了,他想尽量多收钱,因为每个学生参加学习小组都要交一定的手续费,不同的学习小组有不同的手续费。然而,事与愿违,校领导又决定对学习小 组组织者进行奖励,若有a个学生参加第i个学习小组,那么给这个学习小组组织者奖励Ci*a^2元。在参与学生(而不是每个学习小组的人数总和)尽量多的 情况下,求财务处最少要支出多少钱(若为负数,则输出负数)(支出=总奖励费-总手续费)。

Input

输 入有若干行,第一行有三个用空格隔开的正整数n、m、k。接下来的一行有m个正整数,表示每个Ci。第三行有m个正整数,表示参加每个学习小组需要交的手 续费Fi。再接下来有一个n行m列的矩阵,表若第i行j列的数字是1,则表示第i个学生愿意参加第j个学习小组,若为0,则为不愿意。

Output

 
输出只有一个整数,为最小的支出。

Sample Input


3 3 1
1 2 3
3 2 1
111
111
111

Sample Output


-2
【样例解释】
参与学生最多为3,每个学生参加一个学习小组,若有两个学生参加第一个学习小组,一个学生参加第二个学习小组(一定要有人参加第二个学习小组),支出为-2,可以证明没有更优的方案了。
【数据范围与约定】
100%的数据,0<n≤100,0<m≤90,0<k≤m,0<Ci≤10,0<Fi≤100。

HINT

Source

posted @ 2015-01-21 18:23  ZYF-ZYF  Views(265)  Comments(0Edit  收藏  举报