hdu 4807(网络流 + 贪心)
2015-08-08 18:29
477 查看
题意:
n个点(0 到 n-1), m条边,每条边花费都是1,容量不同, 有k个人在0点,问,最少需要多少时间所有人能走到点n-1
解决:
建图,跑费用流的过程中贪心一下。
策略如下:
因为跑费用流的时候,每次增广路径的花费是递增的,假设按顺序找出了3条增广路径,w1, w2, w3,花费递增,容量未知,如果最优方案一定是,全部从w1走,或者尽量从w1和w2走,或者是3条路同时走。例子1,假设有一条增广路径,花费1,流量1, 另一条增广路径,花费10,容量1000,只有5个人,解决方案当然是所有人都从花费少的路径走。答案是5;例子2, 两条路径花费1,容量1,花费5容量10,有20个人,那么结果应该是6。
有一个很重要的性质,假设这条路的花费是x,流量是y,那么x秒之后,每秒钟这条路都能通过y人。、
所以我们每找到一条增广路的时候,就假设只用这条路以及之前的路。维护一个最小值,即为答案。
View Code
n个点(0 到 n-1), m条边,每条边花费都是1,容量不同, 有k个人在0点,问,最少需要多少时间所有人能走到点n-1
解决:
建图,跑费用流的过程中贪心一下。
策略如下:
因为跑费用流的时候,每次增广路径的花费是递增的,假设按顺序找出了3条增广路径,w1, w2, w3,花费递增,容量未知,如果最优方案一定是,全部从w1走,或者尽量从w1和w2走,或者是3条路同时走。例子1,假设有一条增广路径,花费1,流量1, 另一条增广路径,花费10,容量1000,只有5个人,解决方案当然是所有人都从花费少的路径走。答案是5;例子2, 两条路径花费1,容量1,花费5容量10,有20个人,那么结果应该是6。
有一个很重要的性质,假设这条路的花费是x,流量是y,那么x秒之后,每秒钟这条路都能通过y人。、
所以我们每找到一条增广路的时候,就假设只用这条路以及之前的路。维护一个最小值,即为答案。
#include <bits/stdc++.h> const int MAXN = 2600; const int MAXM = 100000; const int INF = 0x3f3f3f3f; struct Edge { int u, v, cap, cost, next; Edge() {} Edge(int _u, int _v, int _cap, int _cost, int _next) { u = _u; v = _v; cap = _cap; cost = _cost; next = _next; } }edge[MAXM]; int tot; int head[MAXN]; int n, m, k; int dist[MAXN]; int pre[MAXN]; bool in_que[MAXN]; bool spfa(int src, int sink) { for (int i = 1; i <= n; ++ i) { dist[i] = INF; } memset(pre, 0, sizeof pre); memset(in_que, false, sizeof in_que); std::queue<int> que; que.push(src); dist[src] = 0; in_que[src] = true; while (que.empty() == false) { int u = que.front(); que.pop(); in_que[u] = false; for (int i = head[u]; i; i = edge[i].next) { int v = edge[i].v; int cost = edge[i].cost; if (edge[i].cap > 0 && dist[v] > dist[u] + cost) { pre[v] = i; dist[v] = dist[u] + cost; if (in_que[v] == false) { que.push(v); in_que[v] = true; } } } } if (pre[sink] == 0) { return false; } return true; } // fisrt : total cost // srcond : min flow int mcmf(int src, int sink) { int res = INF; int pass_per_second = 0; int now_time = 0; int last_time = 0; while (spfa(src, sink) == true) { int min_flow = INF; for (int i = pre[sink]; i; i = pre[edge[i].u]) { // find the min flow in augmenting path min_flow = std::min(min_flow, edge[i].cap); } for (int i = pre[sink]; i; i = pre[edge[i].u]) { // update the cap in augmenting path edge[i].cap -= min_flow; edge[i^1].cap += min_flow; // printf("u = %d, v = %d, cost = %d, min_flow = %d\n", edge[i].u, edge[i].v, edge[i].cost, min_flow); //min_cost += edge[i].cost * min_flow; } k -= ( (dist[sink] -last_time)*pass_per_second + min_flow ); last_time = dist[sink]; pass_per_second += min_flow; int tmp = k; int ttt = last_time + ( (int)ceil(1.0*tmp/(pass_per_second) )); res = std::min(res, ttt); //printf("ttt = %d\n", ttt); //printf("min_flow = %d, last_time = %d, pass_per_second = %d, k = %d\n", min_flow, last_time, pass_per_second, k); if (k <= 0) { break; } } return res; } void addEdge(int u, int v, int cap, int cost) { edge[++tot] = Edge(u, v, cap, cost, head[u]); head[u] = tot; } void init() { tot = 1; memset(head, 0, sizeof head); } int main() { while (~scanf("%d%d%d", &n, &m, &k)) { init(); for (int i = 1, u, v, cap, cost; i <= m; ++ i) { // cost is the cost per flow in this edge scanf("%d%d%d", &u, &v, &cap); addEdge(u+1, v+1, cap, 1); addEdge(v+1, u+1, 0, -1); } if (k == 0) { puts("0"); continue; } int res = mcmf(1, n); if (res == INF) { puts("No solution"); } else { printf("%d\n", res); } } return 0; }
View Code
相关文章推荐
- tinyhttpd简介
- 申请https证书需要注意的4大问题
- HTTP2协议之HPACK--之头部压缩规范介绍
- https原理及tomcat配置https方法
- 企业中web服务http网站的搭建和配置
- iOS开发网络篇—NSURLConnection基本使用(一)
- iOS开发网络篇—数据安全
- iOS开发网络篇—GET请求和POST请求
- iOS开发网络篇—HTTP协议
- iOS开发网络篇—网络编程基础(二)
- matlab画神经网络的结构 neural network
- 【python网络编程】使用rsa加密算法模块模拟登录新浪微博
- 解决ubuntu下无线网络不可用
- hdu 4050 2011北京赛区网络赛K 概率dp ***
- 用C++实现网络编程---抓取网络数据包的实现方法
- iOS开发网络篇—网络编程基础(一)
- 记我的WiFi音箱之一个网络在线播放及本地播放音乐器(四)
- http协议学习和总结系列
- hdu 4049 2011北京赛区网络赛J 状压dp ***
- HTTP协议详解