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 ...
随机推荐
- Python中参数多个值的表示法
今天在写Python脚本时,调用了数据管理-制图综合-融合工具,在ArcGIS里操作的参数设置如下: 如果融合字段只有一个那好办,如果融合字段有多个我该怎么表达,查看帮助文档中的示例代码明白了: 所以 ...
- Genymotion 下载安装常见错误一条龙
Genymotion 安卓模拟器确实比安卓原生的模拟器快,但是除了快就找不到其他优点了... 曾经尝试在VM虚拟机内的Ubuntu系统里面再运行Genymotion的,主要原因是要FQ去下载一些东西, ...
- 心电图html js控件
https://github.com/joakimkemeny/jke.d3.ecg/tree/master/demo/js
- PHP MySQL 连接数据库
PHP连接MySQL的小实例 <?php /*时间:2014-09-14 *作者:葛崇 *功能:PHP连接MySQL小实例 * */ /* SQL 脚本.直接贴到命令行运行. DROP ...
- 解决 vue 的缩进问题 及 vue 的 sass 调用 mixin 函数
1.解决 vue 的缩进问题 配置 eslint , 只要要eslint 对应的值为 0,则 eslint 将不会对其进行检测 (.eslintrc.js -- rules ) A. 不检测 缩进 ...
- position sticky 定位
1.兼容性 https://caniuse.com/#search=sticky chrome.ios和firefox兼容性良好. 2.使用场景 sticky:粘性.粘性布局. 在屏幕范围内时,元素不 ...
- 开源 免费 java CMS - FreeCMS1.9 移动APP管理 网站配置
项目地址:http://www.freeteam.cn/ 网站配置 管理员能够在这里设置当前管理网站是否同意移动app訪问,是否默认移动APP网站.首页的布局,首页数据最多载入页数. 从左側管理菜单点 ...
- 解决Eclipse的Servers视图中无法添加Tomcat6/Tomcat7
如何解决 . 关闭Eclipse . 打开WorkSpace所在的位置. {workspace-directory}/.metadata/.plugins/org.eclipse.core.runti ...
- plsql 表数据中文显示乱码(配置环境变量)
plsql 表数据中文显示乱码(配置环境变量) CreateTime--2018年4月23日19:32:37 Author:Marydon 1.情景再现 2.解决方案 配置环境变量 变量名:NLS ...
- Oracle常见的异常处理
总结了在操作数据库的时候常常遇见的Oracle异常以及处理方法. 代码 提示 备注 一般处理方法 ORA-01861 文字与格式字符串不匹配- ORA-00904 invalid column nam ...