每个工厂拆成N个工厂,费用分别为1~N倍原费用。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
int ch;
while((ch=getchar())!=EOF) {
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
} const int maxn = ;
int N, M;
int cost[maxn][maxn]; void Input()
{
scanf("%d%d", &N, &M);
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
scanf("%d", &cost[i][j]);
} struct Edge
{
int from, to, cost, cap;
};
const int maxv = maxn * maxn + maxn;
vector<int> g[maxv];
vector<Edge> edge;
int n,s,t; void add(int from, int to, int cost, int cap)
{
edge.push_back((Edge){from, to, cost, cap});
g[from].push_back(edge.size() - );
edge.push_back((Edge){to, from, -cost, });
g[to].push_back(edge.size() - );
} void init()
{
for(int i = ; i <= n; i++)
g[i].clear();
edge.clear();
} void construct()
{
n = N + N * M + ;
s = n - ;
t = n;
init(); for(int i = ; i <= N; i++)
add(s, i, , );
for(int k = ; k <= N; k++)
{
for(int j = ; j <= M; j++)
{
int id = N + (k - ) * M + j;
add(id, t, , );
for(int i = ; i <= N; i++)
add(i, id, k * cost[i][j], );
}
}
} int d[maxv];
int inq[maxv];
int road[maxv];
int SPFA()
{
memset(d, INF, sizeof(d));
memset(inq, , sizeof(inq));
queue<int> q;
q.push(s);
d[s] = ;
road[s] = -; while(!q.empty())
{
int u = q.front(); q.pop();
inq[u] = false; for(int i = ; i < g[u].size(); i++)
{
Edge &e = edge[g[u][i]];
if(e.cap > && d[u] + e.cost < d[e.to])
{
d[e.to] = d[u] + e.cost;
road[e.to] = g[u][i];
if(!inq[e.to])
{
inq[e.to] = true;
q.push(e.to);
}
}
}
}
return d[t] != INF;
}
int MCMF()
{
int cost = ;
while(SPFA())
{
cost += d[t];
for(int e = road[t]; e != -; e = road[edge[e].from])
{
edge[e].cap -= ;
edge[e^].cap += ;
}
}
return cost;
}
int main()
{
debug();
int t;
scanf("%d", &t);
for(int ca = ; ca <= t; ca++)
{
Input();
construct();
printf("%.6f\n", MCMF() * 1.0 / N);
}
return ;
}

POJ 3686 The Windy's 最小费用最大流的更多相关文章

  1. POJ 2195 Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意 :  N*M的点阵中,有N个人,N个房子.让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之 ...

  2. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  3. POJ 2516 Minimum Cost (最小费用最大流)

    POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...

  4. POJ 2195 Going Home 【最小费用最大流】

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:2715 ...

  5. POJ 2135 Farm Tour(最小费用最大流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

  6. POJ - 2516 Minimum Cost(最小费用最大流)

    1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...

  7. POJ 2135 Farm Tour(最小费用最大流,变形)

    题意:给一个无向图,FJ要从1号点出发到达n号点,再返回到1号点,但是路一旦走过了就会销毁(即回去不能经过),每条路长度不同,那么完成这趟旅行要走多长的路?(注:会有重边,点号无序,无向图!) 思路: ...

  8. POJ 3686 The Windy's (费用流)

    [题目链接] http://poj.org/problem?id=3686 [题目大意] 每个工厂对于每种玩具的加工时间都是不同的, 并且在加工完一种玩具之后才能加工另一种,现在求加工完每种玩具的平均 ...

  9. POJ 3680:Intervals(最小费用最大流)***

    http://poj.org/problem?id=3680 题意:给出n个区间[Li,Ri],每个区间有一个权值wi,要使得每个点都不被超过k个区间覆盖(最多能被k个区间覆盖),如果选取了第i个区间 ...

随机推荐

  1. Correlation Filter in Visual Tracking系列一:Visual Object Tracking using Adaptive Correlation Filters 论文笔记

    Visual Object Tracking using Adaptive Correlation Filters 一文发表于2010的CVPR上,是笔者所知的第一篇将correlation filt ...

  2. Online Object Tracking: A Benchmark 翻译

    来自http://www.aichengxu.com/view/2426102 摘要 目标跟踪是计算机视觉大量应用中的重要组成部分之一.近年来,尽管在分享源码和数据集方面的努力已经取得了许多进展,开发 ...

  3. DevExpress中GridView上的右键菜单

    1. 先拖一个PopupMenu和BarManage控件,设置PopupMenu的Manager属性为BarManager. 2. 先选中GridView,不是GridControl,在属性窗口中,选 ...

  4. Linux Bash 使用$?来防止一些误删操作

    在shell脚本中如果不加任何检查机制,很有可能导致误删的出现,例如: cd $remove_directory rm * 这段代码的目的是删除目标目录下所有的文件.但是如果目标目录由于输入错误等原因 ...

  5. git branch几个简单操作

    1.git branch  该命令会列出当先项目中的所有分支信息,其中以*开头的表示当前所在的分支.参数-r列出远程仓库中的分支,而-a则远程与本地仓库的全部分支. 2.git branch deve ...

  6. powerDesigner 报Unable to connect SQLState=08004 解决方法

    在使用PowerDesigner配置数据库连接(configure connections)的时候,点击Test connection之后弹出Unable to connect SQLState=08 ...

  7. 技术那么多,你想看看JSON Schema的测试吗?

    目录 1. 什么是JSON Schema? 2. 如何定义一个JSON Schema 3. 如何测试JSON Schema a) 使用JSON Schema validator GUI b) 在Jav ...

  8. 关掉apache2服务器日志文件

    磁盘空间突然满了,才发现是这个东西占空间.11个G的空间 修改:/etc/apache2/size-available/default <VirtualHost *:80>ServerNa ...

  9. PHP指定字段的多维数组排序方法

    PHP数组排序可以用array_multisort方法实现,但是如果是多维数组,并且我们要指定数组中的某个字段进行排序,那么这就需要我们自己写方法实现了. function sortArrByFiel ...

  10. makefile学习小结

    =============2016/08/15================ 上午完成makefile的试验,缩短了代码量,现在make强大,有缺省的变量,能自己推导关系,不需要gcc –MM -M ...