hdu 4494 Teamwork 最小费用最大流
Teamwork
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4494
Description
Some locations in city A has been destroyed in the fierce battle. So the government decides to send some workers to repair these locations. There are m kinds of workers that were trained for different skills. Each location need some number of some kinds of workers and has a schedule that at what time can the repair begins, and the time cost of repair. Any job cannot begin until all the workers required arrived.
For example, location 1 needs 2 workers of type 1 and 3 workers of type 2, and the beginning time and time cost is 100 minute and 90 minute correspondingly, then 5 workers that satisfy the requirement should arrive before 100 minute, start working at 100 minute and get the job done at 190 minute. Notice that two different types of workers cannot replace each other, so with 3 workers of type 1 and only 2 workers of type 2, this job cannot be done.
Workers can go from one location to another after their jobs are done. You can take the Euclidean distance between locations as the time workers need to travel between them. Each worker should be sent from a depot initially at 0 minute. Now your task is to determine the minimum number of workers needed to be sent from depot so that all the jobs can be done.
Input
There are multiple test cases, the integer on the first line T (T<25) indicates the number of test cases.
Each test case begins with two integers n (<=150), the number of location(including the depot) and m(<=5), the number of different skills.
The next line gives two integers x 0, y 0 indicates the coordinate of depot.
Then follows n - 1 lines begins with 4 integer numbers: x i, y i, b i(b i>0), p i(p i>0), (x i, y i) gives the coordinate of the i-th location, bi gives the beginning time and pi gives the time cost. The rest of the line gives m non-negative integers v 1, v 2, ..., v m, of which the i-th number indicates the the number of workers of type i needed (for all v i, 0<=v i<10, each location at least requires one worker).
All integers are less than 1000000 (10 6).
Output
For each test cases output one line, the minimum workers to be sent. It is guaranteed that there's always a feasible solution that all the jobs can be done.
Sample Input
2 4 1 0 0 0 1 1 1 3 1 1 3 3 4 1 0 10 1 5 4 1 0 0 0 1 1 1 3 1 1 3 3 4 1 0 3 1 5
Sample Output
5 9
HINT
题意
有n个工地,工地的位置在xi,yi,工地必须在bi时间开工,要求持续修建ei时间
每个工地需要m种人,每种人需要vk个
工地做完了的,可以派去其他工地
问你最少需要多少个工人
题解:
最小费用最大流
拆点,建边,u,v,容量,费用
addedge(0,2*i-1,p[i].v[TTT],0);
addedge(2*i-1,3*n,p[i].v[TTT],1);
addedge(2*i,3*n,p[i].v[TTT],0);
向能够到达的其他点
addedge(2*i-1,2*j,p[i].v[TTT],0);
虽然这道题是DAG,但是跑背包会TLE。。。
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200500
#define mod 1001
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//*************************************************************************************
struct Node
{
int x,y,b,e;
int v[];
int V[];
};
Node p[];
const int MAXN = ;
const int MAXM = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to, next, cap, flow, cost;
int x, y;
} edge[MAXM],HH[MAXN],MM[MAXN];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N, M;
char map[MAXN][MAXN];
void init()
{
N = MAXN;
tol = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)//左端点,右端点,容量,花费
{
edge[tol]. to = v;
edge[tol]. cap = cap;
edge[tol]. cost = cost;
edge[tol]. flow = ;
edge[tol]. next = head[u];
head[u] = tol++;
edge[tol]. to = u;
edge[tol]. cap = ;
edge[tol]. cost = -cost;
edge[tol]. flow = ;
edge[tol]. next = head[v];
head[v] = tol++;
}
bool spfa(int s, int t)
{
queue<int>q;
for(int i = ; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i]. next)
{
int v = edge[i]. to;
if(edge[i]. cap > edge[i]. flow &&
dis[v] > dis[u] + edge[i]. cost )
{
dis[v] = dis[u] + edge[i]. cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -) return false;
else return true;
}
//返回的是最大流, cost存的是最小费用
int minCostMaxflow(int s, int t, int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i != -; i = pre[edge[i^]. to])
{
if(Min > edge[i]. cap - edge[i]. flow)
Min = edge[i]. cap - edge[i]. flow;
}
for(int i = pre[t]; i != -; i = pre[edge[i^]. to])
{
edge[i]. flow += Min;
edge[i^]. flow -= Min;
cost += edge[i]. cost * Min;
}
flow += Min;
}
return flow;
}
double DDis(Node A,Node B)
{
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
int n=read(),m=read();
int x;
for(int i=;i<;i++)
scanf("%d",&x);
n--;
for(int i=;i<=n;i++)
{
scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].b,&p[i].e);
p[i].e+=p[i].b;
for(int j=;j<=m;j++)
{
scanf("%d",&p[i].v[j]);
}
}
int Ans = ;
for(int TTT=;TTT<=m;TTT++)
{
init();
for(int i=;i<=n;i++)
{
addedge(,*i-,p[i].v[TTT],);
addedge(*i-,*n,p[i].v[TTT],);
addedge(*i,*n,p[i].v[TTT],);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j)continue;
if(1.0*p[i].e+1.0*DDis(p[i],p[j])<=1.0*p[j].b)
{
addedge(*i-,*j,p[i].v[TTT],);
}
}
}
int s=,t=*n;
int ans1 = ,ans2 = ;
ans1 = minCostMaxflow(s,t,ans2);
Ans+=ans2;
}
printf("%d\n",Ans);
}
}
hdu 4494 Teamwork 最小费用最大流的更多相关文章
- hdu 2686 Matrix 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...
- HDU 4862 JUMP 最小费用最大流
2014 多校的B题,由于我不怎么搞图论,当时碰到这个题目,我怎么想都没往网络流方面弄,不过网络流真的是个好东西,对于状态多变,无法用动规或者数据结构来很好表示的时候,非常有用 这个题目要求每个点一定 ...
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...
- hdu 1533 Going Home 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...
- HDU 5988.Coding Contest 最小费用最大流
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 3667(拆边+最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...
- hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
随机推荐
- ActionBarSherlock的学习笔记(二) ------------ 创建ActionBarSherlock
将ActionBarSherlock 作为库项目添加到当前的项目中去,然后创建ActionBar,并自定义ActionBar的标题栏 例子如下: import android.os.Bundle; i ...
- TCP/IP详解学习笔记(14)-TCP连接的未来和性能(未写完)
在TCP刚出世的时候,其主要工作环境是以太网和SLIP之类的低速网络.随着高速网络的出现,让TCP协议的吞吐量更大,效率更高的要求就愈来愈迫切.为此,TCP增加了三个重要机制来对应现在的变化,他们是 ...
- MBR与GRUB简介
在坛子里找到一篇关于grub和mbr工作原理的文章,以前一直都是一头雾水,今天转这文章学习下..哈.. 能正常工作的grub应该包 括一下文件:stage1.stage2.*stage1_5.menu ...
- windows主线程等待子线程退出卡死问题
在windows下调用_beginthread创建子线程并获得子线程id(函数返回值),如果子线程很快退出,在主线程中调用WaitForSingleObject等待该线程id退出,会导致主线程卡死.需 ...
- HDU 1387 Team Queue
Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【转】Ofbiz学习经验谈
不可否认,OFBiz这个开源的系统功能是非常强大的,涉及到的东西太多了,其实对我们现在而言,最有用的只有这么几个:实体引擎.服务引擎.WebTools.用户权限管理.最先要提醒各位的是,在配置一个OF ...
- GRID控件删除之前确认
<asp:TemplateField HeaderText="删除新闻" ShowHeader="False"><ItemTemplate&g ...
- Python面向对象2
方法 公共方法 私有方法 类方法 静态方法 #!usr/bin/python #coding:utf8 class Milo(): name = 'csvt' def fun1(self): prin ...
- Matlab中imshow()函数的使用
imread() 返回的图像类型是uint8类型, 这时用imshow显示图像的时候, imshow会认为输入矩阵的范围在0-255, 如果imshow的参数为double类型的,那么imshow认为 ...
- VB调用控制面板
控制面板 模块: control.exe 命令: rundll32.exe shell32.dll,Control_RunDLL 结果: 显示控制面板窗口. 例子: Dim x x = Shell(& ...