【HDU 4807】Lunch Time 最小费用最大流
题意
在一个有向图当中,现在每一条边带有一个容量,现在有K个人在起点,需要到终点去吃饭,询问这K个人最后一个人到达食堂的最小时间是多少
贴一篇题解:http://blog.csdn.net/u013761036/article/details/38268335
代码
#include <bits/stdc++.h>
#define MAXN 5005
#define MAXM 100010
#define inf 0x7f7f7f7f
using namespace std;
struct Edge{
int to, nxt, cap, cost;
}edge[MAXM];
int head[MAXN], cnt;
int dis[MAXN], last[MAXN], vis[MAXN];
int flow, cost, ans;
void init() {
memset(head, -1, sizeof(head));
cnt = 0;
}
void add_edge(int u, int v, int cap, int cost) {
edge[cnt].to = v;
edge[cnt].cap = cap;
edge[cnt].cost = cost;
edge[cnt].nxt = head[u];
head[u] = cnt++;
}
bool spfa(int s, int t) {
memset(last, -1, sizeof(last));
memset(dis, 127, sizeof(dis));
memset(vis, 0,sizeof(vis));
dis[s] = 0; vis[s] = 1; last[s] = -1;
queue<int> que; que.push(s);
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = 0;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
if(edge[i].cap && dis[v] > dis[u] + edge[i].cost) {
dis[v] = dis[u] + edge[i].cost;
last[v] = i;
if(!vis[v]) {
que.push(v);
vis[v] = 1;
}
}
}
}
return last[t] != -1;
}
void mcmf(int s, int t, int k) {
flow = 0, cost = 0; ans = inf;
int last_time, tot = k;
while(spfa(s, t)) {
int Min = inf;
for(int i = last[t]; ~i; i = last[edge[i ^ 1].to]) Min = min(Min, edge[i].cap);
for(int i = last[t]; ~i; i = last[edge[i ^ 1].to]) {
cost += edge[i].cost * Min;
edge[i].cap -= Min; edge[i ^ 1].cap += Min;
}
tot -= flow * (dis[t] - last_time) + Min;
flow += Min; last_time = dis[t]; tot = max(tot, 0);
ans = min(ans, dis[t] + (int)ceil(1.0 * tot / flow));
if(tot < 1) break;
}
}
int n, m, k, a, b, c;
int main() {
while(scanf("%d%d%d", &n, &m, &k) != EOF) {
init();
for(int i = 1; i <= m; ++i) {
scanf("%d%d%d", &a, &b, &c); a++; b++;
add_edge(a, b, c, 1); add_edge(b, a, 0, -1);
}
if(k == 0) {printf("0\n"); continue;}
mcmf(1, n, k);
if(ans == inf) printf("No solution\n"); else printf("%d\n", ans);
}
return 0;
}
【HDU 4807】Lunch Time 最小费用最大流的更多相关文章
- HDU 5988.Coding Contest 最小费用最大流
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 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 3667(拆边+最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- HDU–5988-Coding Contest(最小费用最大流变形)
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 1533 Going Home 最小费用最大流 入门题
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- POJ 2195 & HDU 1533 Going Home(最小费用最大流)
这就是一道最小费用最大流问题 最大流就体现到每一个'm'都能找到一个'H',但是要在这个基础上面加一个费用,按照题意费用就是(横坐标之差的绝对值加上纵坐标之差的绝对值) 然后最小费用最大流模板就是再用 ...
- hdu 1853 Cyclic Tour 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...
随机推荐
- 【Excle数据透视表】如何隐藏数据透视表中行字段的”+/-”按钮
如下图:新建的数据透视表中有存在"+/-"符号,导致数据透视图不太美观,那么怎么处理呢? 解决方案 单击"显示"组中的"+/-"按钮显示或隐 ...
- centos7 安装rocketmq(quick start)
Quick Start This quick start guide is a detailed instruction of setting up RocketMQ messaging system ...
- AWS上的游戏服务:Lumberyard + Amazon GameLift + Twitch
开发一款世界级的游戏是一个非常困难,耗时和昂贵的过程.如今的游戏玩家要求越来越苛刻,他们希望既能够通过各种不同的终端设备来进行游戏 ,又要求游戏具有社交的功能. 因为此类游戏的开发期和推广期都非常长. ...
- 使用Nginx的proxy_cache缓存功能取代Squid
Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...
- nodejs初学-----helloworld
近期紧锣密鼓的学习了下nodejs(之前在学php.算入门了吧,可是时间关系,还没写文章,兴许要搞安卓和大数据,总之比較忙哈,计划上php要排到后面了,还请广大小伙伴不要着急) 先抄一句:Node.j ...
- uwsgi报错:listen queue of socket ...
Linux默认的socket链接为128,uwsgi默人的链接为100 需要修改系统默认的配置参数, 然后修改uwsgi配置:listen参数:1024
- keras----resnet-vgg-xception-inception
来源: https://www.pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras/ classi ...
- ubuntu16.04--在标题栏显示网速
有时感觉网络失去响应,就通过Ubuntu 14.04自带的系统监视器程序来查看当前网速,但是这样很不方便,遂打算让网速显示在标题栏,那样就随时可直观的看到.一番搜索尝试后,成功实现!同时也实现了CPU ...
- Ubuntu Server 安装 NodeJS
准备命令: $ sudo apt-get install python $ sudo apt-get install build-essential $ sudo apt-get install gc ...
- 资源重复 uac.res resource kept(转)
一般按照网上流传的方法制作UAC.RES放到DELPHI程序里面来就可以出现盾牌.但是某些DELPHI的项目在添加了UAC.RES后编译会报错,例如: [DCC Error] E2161 Warnin ...