【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 ...
随机推荐
- 将Cocos2d-x游戏打包成Android应用程序
1. 打开Eclipse(已经装好CDT.ADT和NDK),导入cocos2d-x的Android项目. 2. 导入后java的源码会出现编译错误,打开cocos2d-x引擎的根文件夹\cocos2d ...
- bootcamp安装win7的详细步骤 (光盘安装)
bootcamp安装win7的详细步骤 首先是要您确定以下内容(1)您的Mac系统下是一个盘符,也就是“macintosh hd”一个磁盘.如果不是的话,首先您需要做的是备份您分区下面的资料,让磁 ...
- 多通道 移位寄存器 verilog
// Quartus II Verilog Template // Basic 64-stage shift register with multiple taps module basic_shif ...
- ArrayList和Vector的区别?HashMap和HashTable的区别?StringBuilder、StringBuffer和String的区别?
ArrayList和Vector的区别?从两个方面 1.同步性:ArrayList是线程不安全的,是非同步的:Vector是线程安全的,是同步的.(Java中线程的同步也就满足了安全性) 2.数值增长 ...
- 【Python基础】之不同的文件在不同目录下导入指定模块的方法
如下图三个文件的目录路径 – project |– 1 | |– 2 | | |– 3 | | | |– owen.py ...
- 重新编译Nginx指导手册【修复静态编译Openssl的Nginx漏洞 】(转)
1. 概述 当前爆出了Openssl漏洞,会泄露隐私信息,涉及的机器较多,环境迥异,导致修复方案都有所不同.不少服务器使用的Nginx,是静态编译opensssl,直接将openssl编译到ng ...
- JAVA学习第五十二课 — IO流(六)File对象
File类 用来给文件或者目录封装成对象 方便对文件与目录的属性信息进行操作 File对象能够作为參数传递给流的构造函数 一.构造函数和分隔符 public static void FileDemo( ...
- 实现RTSP摄像机进行网页直播和微信直播的技术方案:EasyNVR自动更新方法
问题背景: 1.EasyNVR的用户越来越多,技术人员一一对应解答效率不高: 2.随着EasyNVR应用场景的不断增加,以及EasyNVR自身在技术上的不断优化,版本更新比较快: 3.由于开发人力有限 ...
- Asp.Net Mvc: 浅析TempData机制
一. Asp.Net Mvc中的TempData 在Asp.Net Mvc框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...
- Richard Stallman's computer
What hardware do you use? I am using a Lemote Yeelong, a netbook with a Loongson chip and a 9-inch d ...