HDU3395 Special Fish(最大费用任意流)

题目要的并不是最大匹配下得到的最大的结果。

网上流行的做法是加边。其实,在连续增广的时候求得一个可行流的总费用为负就停止这样就行了。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<algorithm>
 5 using namespace std;
 6 #define INF (1<<30)
 7 #define MAXN 222
 8 #define MAXM 22222
 9 struct Edge{
10     int u,v,cap,cost,next;
11 }edge[MAXM];
12 int head[MAXN];
13 int NV,NE,vs,vt;
14
15 void addEdge(int u,int v,int cap,int cost){
16     edge[NE].u=u; edge[NE].v=v; edge[NE].cap=cap; edge[NE].cost=cost;
17     edge[NE].next=head[u]; head[u]=NE++;
18     edge[NE].u=v; edge[NE].v=u; edge[NE].cap=0; edge[NE].cost=-cost;
19     edge[NE].next=head[v]; head[v]=NE++;
20 }
21 bool vis[MAXN];
22 int d[MAXN],pre[MAXN];
23 bool SPFA(){
24     for(int i=0;i<NV;++i){
25         vis[i]=0;
26         d[i]=INF;
27     }
28     vis[vs]=1;
29     d[vs]=0;
30     queue<int> que;
31     que.push(vs);
32     while(!que.empty()){
33         int u=que.front(); que.pop();
34         for(int i=head[u]; i!=-1; i=edge[i].next){
35             int v=edge[i].v;
36             if(edge[i].cap && d[v]>d[u]+edge[i].cost){
37                 d[v]=d[u]+edge[i].cost;
38                 pre[v]=i;
39                 if(!vis[v]){
40                     vis[v]=1;
41                     que.push(v);
42                 }
43             }
44         }
45         vis[u]=0;
46     }
47     return d[vt]!=INF;
48 }
49 int MCMF(){
50     int res=0;
51     while(SPFA()){
52         int flow=INF,cost=0;
53         for(int u=vt; u!=vs; u=edge[pre[u]].u){
54             flow=min(flow,edge[pre[u]].cap);
55         }
56         for(int u=vt; u!=vs; u=edge[pre[u]].u){
57             edge[pre[u]].cap-=flow;
58             edge[pre[u]^1].cap+=flow;
59             cost+=flow*edge[pre[u]].cost;
60         }
61         if(cost>=0) break;
62         res+=cost;
63     }
64     return res;
65 }
66 int main(){
67     int n,a[111],b;
68     while(~scanf("%d",&n)&&n){
69         for(int i=1;i<=n;++i) scanf("%d",a+i);
70         vs=0; vt=n<<1|1; NV=vt+1; NE=0;
71         memset(head,-1,sizeof(head));
72         for(int i=1;i<=n;++i){
73             addEdge(vs,i,1,0);
74             addEdge(i+n,vt,1,0);
75         }
76         for(int i=1;i<=n;++i){
77             for(int j=1;j<=n;++j){
78                 scanf("%1d",&b);
79                 if(b) addEdge(i,j+n,1,-(a[i]^a[j]));
80             }
81         }
82         printf("%d\n",-MCMF());
83     }
84     return 0;
85 }
时间: 2024-12-26 13:59:35

HDU3395 Special Fish(最大费用任意流)的相关文章

HDU 3395 Special Fish(最大费用流)

Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1814    Accepted Submission(s): 678 Problem Description There is a kind of special fish in the East Lake where is closed to campus o

ZOJ3362 Beer Problem(最小费用任意流)

题目大概说有n个城市,由m条无向边相连,每条边每天最多运送cap桶酒且其运送一桶的花费是cost.现在从1号城市开始出发运酒,供应到2到n号城市,这些城市的收购单价是price,问最大的盈利是多少. ...顺路AC 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 #define INF (1<&

HDU 3395 Special Fish 最“大”费用最大流

求最大费用可以将边权取负以转化成求最小费用.然而此时依然不对,因为会优先寻找最大流,但是答案并不一定出现在满流的时候.所以要加一些边(下图中的红边)使其在答案出现时满流.设所有边的流量为1,花费如下图所示.显然最大花费是1001,而没有红边的情况下会得到3. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio>

hdoj 3395 Special Fish 【最大费用流】

Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1920    Accepted Submission(s): 717 Problem Description There is a kind of special fish in the East Lake where is closed to campus o

HDU 3395 Special Fish(拆点+最大费用最大流)

Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2367    Accepted Submission(s): 878 Problem Description There is a kind of special fish in the East Lake where is closed to campus o

HDU 3395 Special Fish(费用流)

题目地址:HDU 3395 刷了几道白书和CF上的非算法的智商题,感觉智商越来越接近负数了...还是先刷几道简单题缓缓.. 这题很简单,二分图模型,用费用流也可以,用KM也可以.不过需要注意的是这里是最大费用流,并不是最大费用最大流,区别在于是否是最大流,这题可以不是最大流,所以要当费用开始减少的时候停止继续流,来保证费用是最大的. 代码如下: #include <iostream> #include <cstdio> #include <string> #includ

【二分图匹配入门专题1】 N - Special Fish hdu3395 【km算法】【春风十里,都不如tle~~~】

There is a kind of special fish in the East Lake where is closed to campus of Wuhan University. It's hard to say which gender of those fish are, because every fish believes itself as a male, and it may attack one of some other fish who is believed to

C++之路进阶——最小费用最大流(支线剧情)

F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  hyxzc Logout 捐赠本站 Notice:由于本OJ建立在Linux平台下,而许多题的数据在Windows下制作,请注意输入.输出语句及数据类型及范围,避免无谓的RE出现. 3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 542  Solved: 332[Submit

hdu 1533 Going Home 最小费用最大流 入门题

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3125    Accepted Submission(s): 1590 Problem Description On a grid map there are n little men and n houses. In each unit time, every