hdu 4494 最小费用流
思路:这题我在下午重现的时候就用的费用流做,可是各种悲催的超时,只是我一开始的那种建图方式多了一个二分查找。
戏剧性的是,求距离的返回值写成int型了,CodeBlock编译器又没有警告,然后就WA啊WA,AC率一下就被拉低了。
当然,对每种工人分别建图是不变的,因为每种工人互不影响。
后来想到了一个较好的建图方式,将每个点拆成3个点,i,i+n,i+2*n。
1号点就是仓库,也就是超级源点,3*n+1号点为超级汇点。
由1号点想每个i建一条流量为ty[i][j],费用为1的边。表示每次增加流量,人数就增加。
由i向i+2*n建一条流量为ty[i][j],费用为0的边。
由i+2*n向汇点建一条流量为ty[i][j],费用为0的边。
由1向每个i+n建一条流量为ty[i][j],费用为0的边。
对i号点,寻找开始时间sta[k]满足b[i]+p[i]+d[i][j]的点k,然后由i+n向k+2*n建一条流量为ty[i][j],费用为0的边。
这样,如果i号点的工人能到k号点工作,那么k号点的k+2*n到3*n+1的流量就会减少ty[i][j]。
- #include<iostream>
- #include<cstring>
- #include<cstring>
- #include<cmath>
- #include<cstdio>
- #define inf 100000000
- using namespace std;
- const int Maxn = ;
- struct Edge{
- int v;
- int val;
- int cost;
- int next;
- }edge[Maxn*];
- struct Point{
- double x,y;
- }p[Maxn];
- int head[Maxn],n,m,k;
- int e;
- int dis[Maxn],pre[Maxn], pos[Maxn],sta[Maxn],en[Maxn],ty[Maxn][],flow;
- int que[Maxn*];
- double d[Maxn][Maxn];
- bool vis[Maxn];
- void add(int u, int v, int val, int cost)
- {
- edge[e].v = v;
- edge[e].val = val;
- edge[e].cost = cost;
- edge[e].next = head[u];
- head[u] = e++;
- edge[e].v = u;
- edge[e].val = ;
- edge[e].cost = -cost;
- edge[e].next = head[v];
- head[v] = e++;
- }
- double DIS(Point a,Point b)
- {
- return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
- }
- void init()
- {
- memset(head,-,sizeof(head));
- e=;
- }
- bool spfa(int s, int t)
- {
- int i;
- memset(pre, -, sizeof(pre));
- memset(vis, , sizeof(vis));
- int Head, tail;
- Head = tail = ;
- for(i = ; i < Maxn; i++)
- dis[i] = inf;
- que[tail++] = s;
- pre[s] = s;
- dis[s] = ;
- vis[s] = ;
- while(Head != tail)
- {
- int now = que[Head++];
- vis[now] = ;
- for(i=head[now]; i != -; i = edge[i].next)
- {
- int adj = edge[i].v;
- if(edge[i].val > && dis[now] + edge[i].cost < dis[adj])
- {
- dis[adj] = dis[now] + edge[i].cost;
- pre[adj] = now;
- pos[adj] = i;
- if(!vis[adj])
- {
- vis[adj] = ;
- que[tail++] = adj;
- }
- }
- }
- }
- return pre[t] != -;
- }
- int MinCostFlow(int s, int t)
- {
- int i;
- int cost = ;
- flow = ;
- while(spfa(s, t))
- {
- int f = ;
- for(i = t; i != s; i = pre[i])
- if (edge[pos[i]].val < f)
- f = edge[pos[i]].val;
- flow += f;
- cost += dis[t] * f;
- for(i = t; i != s; i = pre[i])
- {
- edge[pos[i]].val -= f;
- edge[pos[i] ^ ].val += f;
- }
- }
- return cost;
- }
- void build(int type)
- {
- int i,j;
- init();
- for(i=;i<=n;i++){
- add(,i,ty[i][type],);
- add(i,i+*n,ty[i][type],);
- add(,i+n,ty[i][type],);
- add(i+*n,*n+,ty[i][type],);
- for(j=;j<=n;j++){
- if(sta[i]+en[i]+d[i][j]<=sta[j]){
- add(i+n,j+*n,ty[i][type],);
- }
- }
- }
- }
- int solve()
- {
- int i,j,u,v;
- int ans=;
- for(i=;i<=m;i++){
- build(i);
- ans+=MinCostFlow(,*n+);
- }
- return ans;
- }
- int main()
- {
- int i,j,u,v,c,t;
- scanf("%d",&t);
- while(t--)
- {
- init();
- scanf("%d%d",&n,&m);
- scanf("%lf%lf",&p[].x,&p[].y);
- for(i=;i<=n;i++){
- scanf("%lf%lf%d%d",&p[i].x,&p[i].y,&sta[i],&en[i]);
- for(j=;j<=m;j++){
- scanf("%d",&ty[i][j]);
- }
- }
- for(i=;i<=n;i++){
- for(j=i+;j<=n;j++){
- d[i][j]=d[j][i]=DIS(p[i],p[j]);
- }
- }
- printf("%d\n",solve());
- }
- return ;
- }
hdu 4494 最小费用流的更多相关文章
- hdu 4494 Teamwork 最小费用最大流
Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...
- Going Home (hdu 1533 最小费用流)
集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...
- hdu 1853 最小费用流好题 环的问题
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others) Tota ...
- HDU 6188最小费用流
题目链接:http://hdu.hustoj.com/showproblem.php?pid=6118 掉坑里了,图很好建,Wa了一发,看了Disscuss里面有人提供了一组样例,画图发现:最小流模板 ...
- hdu 4744 最小费用流
#include <cstdio> #include <queue> #include <cstring> #include <cmath> #defi ...
- hdu 4411 最小费用流
思路:主要就是要把一个每个城市拆为两个点,建一条容量为1,费用为-inf的边,保证每个城市都会被遍历. /*最小费用最大流*/ #include<iostream> #include< ...
- hdu 4494 Teamwork (可行流的最小流)
去年通话邀请赛的B题,当时居然过的那么少...明明是一道非常裸的可行流最小流麽..仅仅要对每种人分别求一下可行最小流加起来就能够了.建图是对每一个点拆点,容量上下届都设为v[i],然后每一个点间能连边 ...
- HDU 3667.Transportation 最小费用流
Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 4067 hdoj 4067 Random Maze 最小费用流
给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...
随机推荐
- git强制覆盖本地文件
git fetch --all git reset --hard origin/master
- Linux 基本权限(一)
1. 权限概念 root@hang:/home# ll 总用量 20#文件权限 链接数量 文件所有者 所属用户组 容量大小B 创建(修改)时间 文件名 drwxr-xr-x root root 11月 ...
- android自定义相册 支持低端机不内存溢出
1 之前在网上看的自定义相册很多时候在低端机都会内存溢出开始上代码把 首先我们要拿到图片的所有路径 cursor = context.getContentResolver().query( Media ...
- Cocos2d-x 3.0 动作
http://blog.csdn.net/lnb333666/article/details/16858635 //运行一个action动作对象 runAction("action对象&qu ...
- android仿win8 metro磁贴布局
代码下载 //更新代码, 这里是更新后的代码 //////////////////////// 1,含一个图片无限滚动的控件,自己实现的 2.可新增删除每个磁贴 3.来个图片吧 ////* ...
- [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects
Each time you use the Async Pipe, you create a new subscription to the stream in the template. This ...
- [Node.js] Broswerify -- 1
Browserify is a tool that brings node.js style development to the browser. The thing you can see on ...
- 使用贝赛尔曲线画扇形、圆形、弧线、多边形,实现App下载时的动画效果demo
// // MyView.swift // TestUIBezierPath // // Created by iCodeWoods on 16/5/8. // Copyright © 2016年 i ...
- oc-27-@property的参数
//01加强-10 @property .4前 ) @property + 手动实现 ) @property int age; + @synthesize age;//get和set方法的声明和实现都 ...
- 源码分析:静态分析 C 程序函数调用关系图
http://www.tinylab.org/callgraph-draw-the-calltree-of-c-functions/