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 最小费用最大流的更多相关文章

  1. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

  2. HDU 4862 JUMP 最小费用最大流

    2014 多校的B题,由于我不怎么搞图论,当时碰到这个题目,我怎么想都没往网络流方面弄,不过网络流真的是个好东西,对于状态多变,无法用动规或者数据结构来很好表示的时候,非常有用 这个题目要求每个点一定 ...

  3. POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  4. 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 ...

  5. HDU 5988.Coding Contest 最小费用最大流

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. hdu 3667(拆边+最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...

  7. hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  9. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

随机推荐

  1. 《C++ Primer 4th》读书笔记 第8章-标准IO库

    原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936457.html

  2. mysql 自旋锁

    自旋(spin)是一种通过不间断地测试来查看一个资源是否变为可用状态的等待操作,用于仅需要等待很短的时间等待所需资源的场景.使用自旋这种“空闲循环(busy-loop)”来完成资源等待的方式要比通过上 ...

  3. .NET之美——1.1 C#中的泛型

    1.1 C#中的泛型 .Net 1.1版本最受诟病的一个缺陷就是没有提供对泛型的支持.通过使用泛型,我们可以极大地提高代码的重用度,同时还可以获得强类型的支持,避免了隐式的装箱.拆箱,在一定程度上提升 ...

  4. POJ 1160 Post Office

    题意:有n个村庄,要在其中m个村庄里建邮局,每个村庄去邮局的代价为当前村庄到最近的一个有邮局村庄的路程,问总最小代价是多少. 解法:dp.dp[i][j]表示在前j个村庄建立i个邮局后的代价,则状态转 ...

  5. Calling C++ code from C# z

    http://blogs.msdn.com/b/borisj/archive/2006/09/28/769708.aspx I apologize for the long delay for thi ...

  6. JavaScript中的*top、*left、*width、*Height详解

    来源:http://www.ido321.com/911.html html代码 1: <body> 2: <div class="father" id=&quo ...

  7. Python相关书籍推荐

    Python基础教程(第2版 修订版) 作      者 [挪] Magnus Lie Hetland 著:司维,曾军崴,谭颖华 译 出 版 社 人民邮电出版社 出版时间 2014-06-01 版   ...

  8. bzoj 3997 [TJOI2015]组合数学(DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3997 [题意] 给定一个nm的长方形,每次只能使经过格子权值减1,每次只能向右向下,问 ...

  9. 团 大连网赛 1007 Friends and Enemies

    //大连网赛 1007 Friends and Enemies // 思路:思路很棒! // 转化成最大二分图 // 团:点集的子集是个完全图 // 那么朋友圈可以考虑成一个团,原题就转化成用团去覆盖 ...

  10. stl+模拟 CCF2016 4 路径解析

    // stl+模拟 CCF2016 4 路径解析 // 一开始题意理解错了.... #include <iostream> #include <string> #include ...