每个工厂拆成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. mysql按照中文名称排序

    mysql按照中文名称排序   Sql代码  www.2cto.com   /*   Navicat MySQL Data Transfer      Source Server         : ...

  2. Storm Ack框架笔记

    Storm利用Acker Bolt节点跟踪消息,当Spout发送出去的消息以及这些消息所衍生出来的消息均被处理后,Spout将受到对应于该消息的Ack.实现要点: 1.Storm中每条发送出去的消息都 ...

  3. CentOS6.4 安装 erlang

    wget http://www.erlang.org/download/otp_src_17.0.tar.gz -P /usr/local/src/ wget http://www.erlang.or ...

  4. debian8(jessie)安装小记

    其实上周五就想写这篇博客了,一直忙着没时间,虽然也不知道自己这一个星期到底在忙什么.这次我是彻底告别windows了,安装的过程略为艰辛,因为之前习惯了deepin和ubuntu的傻瓜式安装,而deb ...

  5. python3实现简单爬虫功能

    本文参考虫师python2实现简单爬虫功能,并增加自己的感悟. #coding=utf-8 import re import urllib.request def getHtml(url): page ...

  6. 转一篇老外写的博文:Android automated testing (Robotium)

    Robotium的中文资料甚少,只得求助于老外,发现了一篇不错的文章:https://blog.codecentric.de/en/2011/03/android-automated-testing- ...

  7. UIUC同学Jia-Bin Huang收集的计算机视觉代码合集

    转自:http://blog.sina.com.cn/s/blog_631a4cc40100wrvz.html   UIUC的Jia-Bin Huang同学收集了很多计算机视觉方面的代码,链接如下: ...

  8. underscorejs

    Underscore是一个JavaScript实用库,提供了一整套函数式编程的实用功能,但是没有扩展任何JavaScript内置对象.它是这个问题的答案:“如果我在一个空白的HTML页面前坐下, 并希 ...

  9. ArrayList、HashTable、List、Dictionary的演化及如何选择使用

    在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...

  10. 初探 performance – 监控网页与程序性能

    使用 window.performance 提供了一组精确的数据,经过简单的计算就能得出一些网页性能数据. 配合上报一些客户端浏览器的设备类型等数据,就可以实现简单的统计啦! 额,先看下兼容性如何:h ...