Jam's store

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 134    Accepted Submission(s): 49
Problem Description
Jam didn't study well,then he go to repair the computer in a store,there are
M
staffs and N
guests, given each guests have to spend Tij
time to repair the computer by the j
staffs.



Now ask the total of time the guest at least to wait.

The staff wiil do the next work after he has done the current work
 
Input
The first line is T(1≤T≤100)
means T
Case



For each case



The first line is M
and N(1≤M,N≤20)
means the number of staffs and guests



Now given a Matrix with N∗M
each number means the i
guests to the j
staff time (1≤Tij≤1000)
 
Output
Output one line about the time at least they need to wait
 
Sample Input
1
4 3
4 4 1 5
8 2 5 6
4 5 10 5
 
Sample Output
7
Hint
the first guest choose the third staff
the second guest choose the second staff
the third gurst choose the third staff
the total of time is 4+2+1=7
一道比较裸的最大流,刚开始想得有点简单,建图的时候应该是需要分层的,m个服务人员n个顾客,完全可能出现所有的顾客都去一个服务人员那边的情况,所以应该分成n层,对顾客顾客排序,一个顾客的编号可以使1--n,他后边可能有k个人,所以后边的人是需要等k*time,这就是边权,然后就是建立超级源点跟超级汇点,跑一边最大流
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 5000+10
#define MAXM 800000+10
#define INF 0x3f3f3f3f
int head[MAXN],cnt;
struct node
{
int u,v,cap,flow,cost,next;
}edge[MAXM];
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w,int c)
{
node E={u,v,w,0,c,head[u]};
edge[cnt]=E;
head[u]=cnt++;
node E1={v,u,0,0,-c,head[v]};
edge[cnt]=E1;
head[v]=cnt++;
}
bool BFS(int s,int t)
{
queue<int>q;
memset(dis,INF,sizeof(dis));
memset(pre,-1,sizeof(pre));
memset(vis,false,sizeof(vis));
dis[s]=0;vis[s]=true;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.v]>dis[E.u]+E.cost&&E.cap>E.flow)
{
dis[E.v]=dis[E.u]+E.cost;
pre[E.v]=i;
if(!vis[E.v])
{
vis[E.v]=true;
q.push(E.v);
}
}
}
}
return pre[t]!=-1;
}
void MCMF(int s,int t,int &cost,int &flow)
{
cost=flow=0;
while(BFS(s,t))
{
int Min=INF;
for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])
{
node E=edge[i];
Min=min(Min,E.cap-E.flow);
}
for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
cost+=Min*edge[i].cost;
}
flow+=Min;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&m,&n);
int S=0,T=n+n*m+1;
init();
for(int i=1;i<=n;i++)
{
add(S,i,1,0);
for(int j=1;j<=m;j++)
{
int time;
scanf("%d",&time);
for(int k=1;k<=n;k++)
add(i,j*n+k,1,k*time);
}
}
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
add(i*n+j,T,1,0);
int flow,cost;
MCMF(S,T,cost,flow);
printf("%d\n",cost);
}
return 0;
}

hdoj--5619--Jam's store(最小费用最大流)的更多相关文章

  1. hdoj 1533 Going Home 【最小费用最大流】【KM入门题】

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  3. [板子]最小费用最大流(Dijkstra增广)

    最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...

  4. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  5. ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)

    将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

  8. 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)

    3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 821  Solved: 502[Submit][Status ...

  9. hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***

    题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙,          每个逮捕队伍在每个城市可以选 ...

  10. UVa11082 Matrix Decompressing(最小费用最大流)

    题目大概有一个n*m的矩阵,已知各行所有数的和的前缀和和各列所有数的和的前缀和,且矩阵各个数都在1到20的范围内,求该矩阵的一个可能的情况. POJ2396的弱化版本吧..建图的关键在于: 把行.列看 ...

随机推荐

  1. 重绘DataGridView的DataGridViewCheckBoxCell控件

    最近项目中要用到在DataGridView单元格里面放置一个带有文本的 DataGridViewCheckBoxCell控件但原有 的是不支持的然后我就想着重写个 DataGridViewCheckB ...

  2. JavaScript学习书签

    JavaScript常用正则表达式 闭包 JavaScipt DOM 变量提升

  3. log4j最全教程

    (转自http://www.codeceo.com/article/log4j-usage.html) 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方 ...

  4. python_文件io

    # -*- coding:UTF-8 -*-#从键盘读入raw_input([prompt]) #函数从标准输入读取一个行,并返回一个字符串(去掉结尾的换行符)#input([prompt]) 函数和 ...

  5. dva相关文档

    https://dvajs.com/guide/getting-started.html#%E5%AE%9A%E4%B9%89-model-------dva.js https://dvajs.com ...

  6. 【特 性】Attribute

    1 AttributeUsage [AttributeUsageAttribute(AttributeTargets.All, AllowMultiple = true, Inherited = tr ...

  7. Vmware在NAT模式下网络配置详解

    Vmware在NAT模式下网络配置详解 Linux中的网络配置对于接触Linux不久的小白菜来说,还是小有难度的,可能是不熟悉这种与windows系列迥然不同的命令行操作,也可能是由于对Linux的结 ...

  8. jquery源码分析(五)——Deferred 延迟对象

    javascript的异步编程 为什么要使用异步编程? JS是单线程语言,就简单性而言,把每一件事情(包括GUI事件和渲染)都放在一个线程里来处理是一个很好的程序模型,因为这样就无需再考虑线程同步这些 ...

  9. [Poj3261] [Bzoj1717] [后缀数组论文例题,USACO 2006 December Gold] Milk Patterns [后缀数组可重叠的k次最长重复子串]

    和上一题(POJ1743,上一篇博客)相似,只是二分的判断条件是:是否存在一段后缀的个数不小于k #include <iostream> #include <algorithm> ...

  10. springmvc 时间返回格式化

    如果是@ResponseBody,可以通过@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")设置返回的样式: 如果是不是@ResponseBody ...