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 ...
随机推荐
- 将浮点数保持几位小数,尾数舍入的Format函数
select format(hours,2) from pos.daywork
- 遭遇java.lang.NoClassDefFoundError: org/apache/tomcat/PeriodicEventListener
前天还正常的程序,今天忽然无法启动了,MyEclipse的Console提醒我如下错误: 严重: Error deploying web application directory rttsbizja ...
- 用10046 跟踪exp
之前写过一个blog,Oracle expdp为什么比exp快,原理是什么,是从官方文档中获知的,如今通过10046来分析exp的过程. C:\Users\Administrator>exp L ...
- hdu4490 Mad Veterinarian(bfs)
Mad Veterinarian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Hibernate生成实体类-手工写法(一)
BaseDao package com.pb.dao; import java.sql.Connection; import java.sql.DriverManager; import java.s ...
- 修复windows Management Instrumentation(WMI)
第一步.重建repository文件夹下所有文件,打开“开始”“运行”输入一下命令并回车:1.停止 WMI 服务, net stop winmgmt2.删除repository文件夹下所有文件, %w ...
- OpenERP7.0中日期的问题
在OpenERP7.0中载入中文语言后,输入某些单据,一些细化到时间的字段输入时会报错,说是日期格式不正确. 解决办法是进入[设置]-[翻译]-[语言]菜单,把时间格式中的值 %I:%M:%S %p ...
- [C#]记录程序耗时的方法【转发】
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // H ...
- Flash:DisplayObject的矩阵旋转(移动/修改注册点,修改旋转点)
简单来说,原理就是利用matrix运算,先把旋转点移到原点位置,旋转变换后再恢复到原来的位置 var a:Sprite = new Sprite(); a.graphics.beginFill(0); ...
- Eclipse 如何导入MyEclipse的项目
Eclipse 如何导入MyEclipse的项目 CreateTime--2018年3月8日09:53:55 Author:Marydon 1.Eclipse导入MyEclipse的项目方法,跟导 ...