UVALive-5095 Transportation (最小费用流+拆边)
题目大意:有n个点,m条单向边。要运k单位货物从1到n,但是每条道路上都有一个参数ai,表示经这条路运送x个单位货物需要花费ai*x*x个单位的钱。求最小费用。
题目分析:拆边。例如:u到v的容量为5,则拆成容量均为1,单位费用分别为1,3,5,7,9的5条边。求流恰好能满足运输需求时的最小费用即可。
代码如下:
# include<iostream>
# include<cstdio>
# include<cmath>
# include<string>
# include<vector>
# include<list>
# include<set>
# include<map>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; # define LL long long
# define REP(i,s,n) for(int i=s;i<n;++i)
# define CL(a,b) memset(a,b,sizeof(a))
# define CLL(a,b,n) fill(a,a+n,b) const double inf=1e30;
const int INF=1<<30;
const int N=5005; int k; struct Edge
{
int fr,to,cap,fw,cost;
Edge(int fr,int to,int cap,int fw,int cost){
this->fr=fr;
this->to=to;
this->cap=cap;
this->fw=fw;
this->cost=cost;
}
};
struct MCMF
{
vector<Edge>edges;
vector<int>G[N];
int s,t,n;
int inq[N];
int p[N];
int a[N];
int d[N]; void init(int n,int s,int t)
{
this->n=n;
this->s=s,this->t=t;
for(int i=0;i<n;++i) G[i].clear();
edges.clear();
} void addEdge(int u,int v,int cap,int cost)
{
edges.push_back(Edge(u,v,cap,0,cost));
edges.push_back(Edge(v,u,0,0,-cost));
int m=edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
} bool bellmanFord(int &flow,int &cost)
{
fill(d,d+n,INF);
memset(inq,0,sizeof(inq));
d[s]=0,inq[s]=1,p[s]=0,a[s]=INF; queue<int>q;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
inq[x]=0;
for(int i=0;i<G[x].size();++i){
Edge &e=edges[G[x][i]];
if(e.cap>e.fw&&d[e.to]>d[x]+e.cost){
d[e.to]=d[x]+e.cost;
p[e.to]=G[x][i];
a[e.to]=min(a[x],e.cap-e.fw);
if(!inq[e.to]){
inq[e.to]=1;
q.push(e.to);
}
}
}
}
if(d[t]==INF) return false;
flow+=a[t];
cost+=d[t]*a[t];
for(int u=t;u!=s;u=edges[p[u]].fr){
edges[p[u]].fw+=a[t];
edges[p[u]^1].fw-=a[t];
}
return true;
} void minCost(int &flow,int &cost)
{
flow=cost=0;
while(bellmanFord(flow,cost))
if(flow>=k) break;
}
};
MCMF cf; int n,m; int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
cf.init(n+1,1,n);
int a,b,c,d;
while(m--)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
int cnt=1;
while(d--)
{
cf.addEdge(a,b,1,cnt*c);
cnt+=2;
}
}
int flow,cost;
cf.minCost(flow,cost);
if(flow>=k) printf("%d\n",cost);
else printf("-1\n");
}
return 0;
}
UVALive-5095 Transportation (最小费用流+拆边)的更多相关文章
- 【 UVALive - 5095】Transportation(费用流)
Description There are N cities, and M directed roads connecting them. Now you want to transport K un ...
- HDU 3667.Transportation 最小费用流
Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU3667 Transportation —— 最小费用流(费用与流量平方成正比)
题目链接:https://vjudge.net/problem/HDU-3667 Transportation Time Limit: 2000/1000 MS (Java/Others) Me ...
- ZOJ3231 Apple Transportation(最小费用流)
题目给你一棵苹果树,然后每个结点上有一定的苹果树,你要将苹果运输达到某个状态,使得均方差最小. 将苹果x个从a->b的花费是x*w,w是边权. 当时比赛的时候想的就是,最后达到的状态一定是sum ...
- hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流
/** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由 ...
- hdu 3667 拆边加最小费用流
Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- UVaLive 3353 Optimal Bus Route Design (最小费用流)
题意:给定一个 n 个点的有向带权图,让你找若干个圈,使得每个结点恰好属于一个圈,并且总长度尽量小. 析:一开始想的是先缩点,先用DP,来求... 题解给的是最小费用流或者是最佳完全匹配,其实都是一样 ...
- HDU 3667 费用流 拆边 Transportation
题意: 有N个城市,M条有向道路,要从1号城市运送K个货物到N号城市. 每条有向道路<u, v>运送费用和运送量的平方成正比,系数为ai 而且每条路最多运送Ci个货物,求最小费用. 分析: ...
- UVA1486 Transportation 费用流 拆边。
#include <iostream> #include <cstdio> #include <cmath> #include <queue> #inc ...
随机推荐
- 牛客网多校赛第七场J--Sudoku Subrectangle
链接:https://www.nowcoder.com/acm/contest/145/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...
- Asp.NET 与 WebApi 共享Session
首先新建一个.net framework 4.5.2的 web应用程序,选择WebApi基架,包括MVC与Webapi 1.在global.asax中启用Session 2.在HomeControll ...
- Oracle性能优化之oracle里表、索引、列的统计信息
一.表的统计信息 表的统计信息用于描述表的详细信息,包括记录数(num_rows).表块的数量(blocks).平均行长度(avg_row_len)等典型维度.这些维度可以通过数据字典表DBA_TAB ...
- Monte Carlo methods
Monte Carlo methods https://zh.wikipedia.org/wiki/蒙地卡羅方法 通常蒙地卡羅方法可以粗略地分成两类:一类是所求解的问题本身具有内在的随机性,借助计算机 ...
- Can you solve this equation?---hdu2199(二分)
http://acm.hdu.edu.cn/showproblem.php?pid=2199 给出y的值求x: 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 = Y x是0到100的 ...
- postman app支持浏览器上的cookie
1. 安装postman app 注意要安装postman application(一个应用软件),而不是chrome 插件,打开下面的这个开关 2. chrom浏览器 给chrom浏览器安装pos ...
- CSS之Flex 布局:语法篇
网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如 ...
- stm8s 时钟库函数选择内部RC初始化
//本文选择16M内部RC震荡.分频为1 即系统时钟为16M void CLK_HSICmd(FunctionalState NewState) { /* Check the parameters * ...
- CentOS7安装mysql-python模块
# sudo pip install mysql-python 此时会提示找不到mysql-config文件,我们安装一下mysql-community-devel # sudo yum instal ...
- python3 requests 进行接口测试、爬虫使用总结
Requests 是第三方模块,如果要使用的话需要导入.Requests也可以说是urllib模块的升级版,使用上更方便. 这是使用urllib的例子. import urllib.request i ...