HDU 3395 Special Fish 最“大”费用最大流
求最大费用能够将边权取负以转化成求最小费用。
然而此时依旧不正确。由于会优先寻找最大流。可是答案并不一定出如今满流的时候。所以要加一些边(下图中的红边)使其在答案出现时满流。
设全部边的流量为1,花费例如以下图所看到的。
显然最大花费是1001。而没有红边的情况下会得到3。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f
#define Mod 6000007 using namespace std; struct E
{
int u,v,Max,cost,next;
}edge[200100]; int head[310];
int cur[310]; int Top; void Link(int u,int v,int w,int cost)
{
edge[Top].u = u;
edge[Top].v = v;
edge[Top].cost = cost;
edge[Top].Max = w;
edge[Top].next = head[u];
head[u] = Top++;
} struct Q
{
int v;
// bool operator < (const Q &a) const {
// return a.cost < cost;
// }
}; void Updata(int site,int flow,int &cost)
{
for(int i = site;cur[i] != -1; i = edge[cur[i]^1].v)
{
edge[cur[i]].Max -= flow;
edge[cur[i]^1].Max += flow;
cost += edge[cur[i]].cost*flow;
}
} int dis[310];
int flow[310]; bool mark[310]; queue<Q> q; int Spfa(int S,int T,int n,int &cost)
{
while(q.empty() == false)
q.pop(); memset(mark,false,sizeof(bool)*(n+2));
memset(dis,INF,sizeof(int)*(n+2));
memset(flow,INF,sizeof(int)*(n+2));
dis[S] = 0; Q f,t; f.v = S;
dis[S] = 0;
cur[S] = -1;
mark[S] = true;
q.push(f); while(q.empty() == false)
{
f = q.front();
q.pop();
mark[f.v] = false;
for(int p = head[f.v]; p != -1; p = edge[p].next)
{
t.v = edge[p].v; if(edge[p].Max && dis[t.v] > dis[f.v] + edge[p].cost)
{
cur[t.v] = p;
dis[t.v] = dis[f.v] + edge[p].cost;
flow[t.v] = min(flow[f.v],edge[p].Max); if(mark[t.v] == false)
{
mark[t.v] = true;
q.push(t);
}
}
}
} if(dis[T] != INF)
{
Updata(T,flow[T],cost);
return flow[T];
} return 0;
} int Cal_MaxFlow_MinCost(int S,int T,int n)
{
int f = 0,cost = 0,temp; do
{
temp = Spfa(S,T,n,cost);
f += temp;
}while(temp); printf("%d\n",-cost); return cost;
} int val[110]; int main()
{
int n; int u,v,w,i,j,x; while(scanf("%d",&n) && n)
{
memset(head,-1,sizeof(int)*(2*n+3));
Top = 0;
for(i = 1;i <= n; ++i)
{
scanf("%d",&val[i]);
} int S = 1,T = 2*n+2; for(i = 1;i <= n; ++i)
{
Link(S,i+1,1,0);
Link(i+1,S,0,0);
Link(n+i+1,T,1,0);
Link(T,n+i+1,0,0);
Link(i+1,T,1,0);
Link(T,i+1,0,0);
} for(i = 1;i <= n; ++i)
{
for(j = 1;j <= n; ++j)
{
scanf("%1d",&x);
if(x)
{
Link(i+1,j+n+1,1,-(val[i]^val[j]));
Link(j+n+1,i+1,0,(val[i]^val[j]));
}
}
}
Cal_MaxFlow_MinCost(S,T,T);
}
return 0;
}
HDU 3395 Special Fish 最“大”费用最大流的更多相关文章
- HDU 3395 Special Fish(拆点+最大费用最大流)
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- [ACM] HDU 3395 Special Fish (最大重量二分图匹配,KM算法)
Special Fish Problem Description There is a kind of special fish in the East Lake where is closed to ...
- HDU3395 Special Fish(最大费用任意流)
题目要的并不是最大匹配下得到的最大的结果. 网上流行的做法是加边.其实,在连续增广的时候求得一个可行流的总费用为负就停止这样就行了. #include<cstdio> #include&l ...
- hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 最小/大费用最大流模板(codevs1914)
void addedge(int fr,int to,int cap,int cos){ sid[cnt].fr=fr;sid[cnt].des=to;sid[cnt].cap=cap;sid[cnt ...
- HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives a ...
- HDU 1853 Cyclic Tour(最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others) Tota ...
- 【HDU 4807】Lunch Time 最小费用最大流
题意 在一个有向图当中,现在每一条边带有一个容量,现在有K个人在起点,需要到终点去吃饭,询问这K个人最后一个人到达食堂的最小时间是多少 贴一篇题解:http://blog.csdn.net/u0137 ...
- hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- 使用sed进行文字替换
范式: sed -i "s/查找内容/替换后内容/g" `grep 查找内容 -rl 查找开始路径` 例子: #sed -i "s/abc/ABC/g" `gr ...
- Office PPT如何切换到返回幻灯片
1 如图所示,有"老师""同学""家人"三个板块,如果依次播放,将播放"老师1" "老师2" &qu ...
- DevExpress学习03——label控件的背景色问题
今天使用了DevExpress的labelControl,发现拖放上去,其背景色和主窗体的背景一样,非常不谐调,把BackColor设置为透明也不行(Windows中的Label可以). 没有办法,我 ...
- Socket 之 传值方式
一.传送 DataTable 和 DataSet 1.发送端需序列化数据为二进制序列化格式 public static byte[] GetBinaryFormatDataSet(DataSet ds ...
- Android 笔记-Fragment 与 Activity之间传递数据
Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...
- python之函数用法staticmethod
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法staticmethod #http://www.cnblogs.com/hongfe ...
- HDFS架构设计
原文:http://hadoop.apache.org/docs/r2.6.4/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html 介绍 HDFS是个分布式 ...
- [转载]windows7中没用boot.ini文件而是改用BCDEDIT来配置了
原文地址:windows7中没用boot.ini文件而是改用BCDEDIT来配置了作者:开心骝哥 本人安装的Win7,因为机上有两个系统,一个dos系统,一个win7系统,开机时总是会在系统选择的界面 ...
- SpringMVC中的Controller默认单例
众所周知,Servlet是单例的. 在struts中,Action是多例的,每一个请求都会new出来一个action来处理. 在Spring中,Controller默认是单例的,多个请求都会访问同一个 ...
- [转]MegCup2015初赛题
原文链接 门票题:数独有多少种对解线上没有1的填法? 这道"门票题"虽说只是"热身",但还是有一定难度的.共有245名选手通过各种方法拿到了门票.下面,我们就为 ...