Lunch Time(费用流变型题,以时间为费用)
Lunch Time
http://acm.hdu.edu.cn/showproblem.php?pid=4807
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 782 Accepted Submission(s): 183
The first line of each test case contains three integer N(2 <= N <= 2500), M(0 <= M <= 5000), K(0 <= K <= 109). Then follows M lines, each line has three numbers ai, bi, ci(0 <= ci <= 20), means there is an edge from vertex ai to bi with the capacity ci.
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std; const int INF=0x3f3f3f3f;
const int N=;
const int M=;
int top;
int dist[N],pre[N];
bool vis[N];
int c[N];
int maxflow; struct Vertex{
int first;
}V[N];
struct Edge{
int v,next;
int cap,flow,cost;
}E[M]; void init(int n){
for(int i=;i<=n;i++){
V[i].first=-;
}
top=;
maxflow=;
} void add_edge(int u,int v,int c,int cost){
E[top].v=v;
E[top].cap=c;
E[top].flow=;
E[top].cost=cost;
E[top].next=V[u].first;
V[u].first=top++;
} void add(int u,int v,int c,int cost){
add_edge(u,v,c,cost);
add_edge(v,u,,-cost);
} bool SPFA(int s,int t,int n){
int i,u,v;
queue<int>qu;
for(i=;i<=n;i++){
dist[i]=INF;
vis[i]=false;
c[i]=;
pre[i]=-;
}
vis[s]=true;
c[s]++;
dist[s]=;
qu.push(s);
while(!qu.empty()){
u=qu.front();
qu.pop();
vis[u]=false;
for(i=V[u].first;~i;i=E[i].next){
v=E[i].v;
if(E[i].cap>E[i].flow&&dist[v]>dist[u]+E[i].cost){
dist[v]=dist[u]+E[i].cost;
pre[v]=i;
if(!vis[v]){
c[v]++;
qu.push(v);
vis[v]=true;
if(c[v]>n){
return false;
}
}
}
}
}
if(dist[t]==INF){
return false;
}
return true;
} int MCMF(int s,int t,int n,int k){
if(k==) return ;////////////*******************////
int d;
int i,mincost;
mincost=;
int ans=INF;
int sum_peo=k,now_peo=,list_time=;
while(SPFA(s,t,n)){
d=INF;
for(i=pre[t];~i;i=pre[E[i^].v]){
d=min(d,E[i].cap-E[i].flow);
}
maxflow+=d;
for(i=pre[t];~i;i=pre[E[i^].v]){
E[i].flow+=d;
E[i^].flow-=d;
}
mincost+=dist[t]*d;
sum_peo-=(dist[t]-list_time)*now_peo+d;
list_time=dist[t],now_peo+=d;
int now=dist[t]+(int)ceil((1.0*(sum_peo<?:sum_peo))/now_peo);
if(ans>now) ans=now;
if(sum_peo<) break;
}
return ans;
} int main(){
int n,m,k;
int v,u,w,c;
int s,t;
while(~scanf("%d %d %d",&n,&m,&k)){
s=,t=n-;
init(n);
for(int i=;i<=m;i++){
scanf("%d %d %d",&v,&u,&c);
add(v,u,c,);
}
int ans=MCMF(s,t,n,k);
if(ans==INF) printf("No solution\n");
else printf("%d\n",ans);
}
}
/*
题意:
给你一个有向图,每条边上都有每一时刻的最大流量,有k个人在点0,
他们要去点n-1,问你最晚到达的那个人最快要多久。
思路:
这道题目用该是借助费用流的找新路径去枚举,可以说是费用流变形吧,
首先我们一定要明白一点,就是时间的影响,单纯的最大流如果在时间的基础上考虑没什么意义,
而费用流呢,我们想象一下,如果把时间设成费用那么我们就可以吧流量和时间结合起来了,
在费用流的过程中我们要知道,他每一次都是根据最短路去找新的路径的,
也就是说路径在费用上来说是递增的(流量不一定),
那么我们就可以根据这个特点来枚举一那些路径来过人,
要明白,如果起点到终点有两条边,一条很近,一条很远,
有可能大家都走近的路径(宁可排队走),也不走远的(所以直接最大流是错了),
那么我们就可以枚举路径了,路径可以直接用费用流每次的路径,因为时间递增的,
对于每一次我们能过的人是多少呢?这个地方很关键,对于每一条路径来说,
如果当前的路径a距离是10,流量是15,那么当时间大于10的时候,每过一个时间单位,路径a都可以再过15个人,
所以当前的时间段的总人数是之前的总人数+(当前长度-上一个长度)* 上一个的总流量 + 当前流量
那么如果现在当前这一部之前的所有路径当路径要花费的时间是多少呢
now = 当前长度+剩余的路径长度/当前总流量 向上取整
这样比较now和ans更新答案就行了,
还有一个地方要明确,就是当总人数超过全图的最大流的时候答案就是
费用流中最长的路径 + 总人数/全图最大流 向上取整,
这个地方不用特判,上面的想法里面包括在这里,说了只是为了便于理解。 */
Lunch Time(费用流变型题,以时间为费用)的更多相关文章
- Coding Contest(费用流变形题,double)
Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...
- POJ 3686 The Windy's(思维+费用流好题)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5362 Accepted: 2249 Descr ...
- HDU 3376 && 2686 方格取数 最大和 费用流裸题
题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...
- Going Home POJ - 2195 费用流板子题
On a grid map there are n little men and n houses. In each unit time, every little man can move one ...
- bzoj3442: 学习小组(费用流好题)
3442: 学习小组 题目:传送门 题解: 超级好题啊大佬们的神题!建图肥肠灵性!感觉自己是星际玩家... 首先呢st直接向每个人连边,容量为min(k,喜欢的小组个数),费用为0 然后每个人再向ed ...
- CFGYM 2013-2014 CT S01E03 D题 费用流模版题
题意: n行, a房间的气球,b房间的气球 i行需要的气球,与a房的距离,b房的距离 求最小距离 #include <stdio.h> #include <string.h> ...
- 【BZOJ-2055】80人环游世界 上下界费用流 (无源无汇最小费用最大流)
2055: 80人环游世界 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 321 Solved: 201[Submit][Status][Discus ...
- 费用流+SPFA ||【模板】最小费用最大流
题面:[模板]最小费用最大流 代码: #include<cstdio> #include<cstring> #include<iostream> #include& ...
- 【费用流】【Next Array】费用流模板(spfa版)
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...
随机推荐
- [转]关闭WIN7“程序兼容性助理”
转载自 http://www.flighty.cn/html/tutorial/20140717_244.html WIN7程序兼容性助理其实是一个非常鸡肋的功能,对于我们基本上可以说没有什么用处,反 ...
- SQL 字段查找
select [name] from sysobjects where [id] in (select [id] from syscolumns where [name]='a1') SQL 2005 ...
- Linux性能分析 vmstat输出
vmstat输出 1.linux系统下vmstat输出 vmstat的输出分为以下几种模式: (1).VM MODE (普通选项) (2).DISK MODE(-d选项) (3).DI ...
- [UE4]机器人自动寻路
要让机器人能够自动寻路,需要画出自动寻路的范围,可以使用“Nav Mesh Bounds Volume”组件来自定寻路范围 通过“Delay”节点可以实现让AI执行Move To以后停顿1秒,然后继 ...
- jpgraph中文使用手册之文本和字体控制教程
摘要:在之前的php jpgraph安装配置教程中已介绍过jpgraph字体的安装与配置方法,jpgraph类库中字体和文本的使用是非常重要的,jpgraph既可以控 制文本的旋转.对齐方式.字体大小 ...
- Asterisk重要App
elastix82*CLI> core show application SoftHangup -= Info about application 'SoftHangup' =- [Synop ...
- C++多线程同步之事件(Event)
原文链接:http://blog.csdn.net/olansefengye1/article/details/53291074 一.事件(Event)原理解析 1.线程同步Event,主要用于线程间 ...
- hint之qb_name
http://www.thinkindata.com/?p=34 该hint用于子查询(query_block) 很多的情况下,如果子查询共用相同的别名(alias), 可以通过设定不同的qb_n ...
- vue&webpack多页面配置
前言 最近由于项目需求,选择使用vue框架,webpack打包直接使用的vue-cli,因为需要多页面而vue-cli只有单页面,所以就决定修改vue-cli的配置文件来满足开发需求. html-we ...
- tensorflow data's save and load
note: if you'll load data,the data shape should be similar with saved data's shape. -- 中式英语,天下无敌 ...