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 ...
随机推荐
- Code Forces 652D Nested Segments(离散化+树状数组)
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 设计模式之——visitor模式
visitor模式,又叫访问者模式,把结构和数据分开,编写一个访问者,去访问数据结构中的元素,然后把对各元素的处理全部交给访问者类.这样,当需要增加新的处理时候,只需要编写新的 访问者类,让数据结构可 ...
- nodejs Async详解之三:集合操作
Async提供了很多针对集合的函数,可以简化我们对集合进行异步操作时的步骤.如下: forEach:对集合中每个元素进行异步操作 map:对集合中的每个元素通过异步操作得到另一个值,得到新的集合 fi ...
- [css]浮动造成的影响
浮动造成的影响: 子元素浮动,父元素无法被撑出高了. 如果要给父元素做通栏background? 如果两个box的子元素都浮动,且希望两个box分行显示? box1 box2 box3: float: ...
- 【Lua】面向对象编程(二)
多重继承: module(...,package.seeall) local function search(k,plist) ,#plist do local v=plist[i][k] if ...
- linux的浅谈io操作
系统默认设定 名称类型文件描述符操作标准输入standard input0<,<< 标准输出standard output1>,>> 标准错误输出standard ...
- Integration Services 变量
如果没有变量,你会发现在ssis里面啥都干不成,和人没有灵魂一样 对系统变量唯一可配置的选项是指定变量在更改值时是否引发事件. 待续
- 【深入理解JVM】:类加载器与双亲委派模型
类加载器 加载类的开放性 类加载器(ClassLoader)是Java语言的一项创新,也是Java流行的一个重要原因.在类加载的第一阶段“加载”过程中,需要通过一个类的全限定名来获取定义此类的二进制字 ...
- VS2010/MFC编程入门之六(对话框:创建对话框模板和修改对话框属性)
鸡啄米在上一讲中介绍了MFC的消息映射机制,属于原理方面的知识.对于VC++编程入门学习者来说可能有些抽象,鸡啄米会把消息映射的知识渗透到后面的教程中.本节开始为大家讲解偏应用的知识-创建对话框. 对 ...
- MFC中利用GDI+进行双缓冲作图的有关设置
这里只是在遇到实际问题的时候提出的一种解决方法,用以处理闪屏问题. 首先要做的是对GDI的一个设置问题: 在应用程序类中添加一个保护权限数据成员 class C...App: {... private ...