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 ...
随机推荐
- Git之删除仓库
Github删除已有仓库步骤 在仓库页面点击设置 在新打开网页删除 输入仓库名点击删除即可
- IIS中User-mode caching引起的Cache-Control不为public的问题
在IIS的Output caching中如果启用了User-mode caching将引起Cache-Control为no-cache,从而造成页面不能被浏览器或代理服务器缓存. web.config ...
- laydate设置起始时间,laydate设置开始时间和结束时间
//设置开始时间 var startDate = laydate.render({ elem: '#start_date',//开始时间选择控件id min:'2018-6-1', type: 'da ...
- 从零开始写JavaWeb框架(第一章节)
买了本<从零开始写JavaWeb框架> 因为是第一次用IDEA,期间遇到很多问题,比如:怎么在IDEA中配置tomcat: 在IDEA界面的右上角点击: 点击+,选择Maven 到了如下界 ...
- Selenium IDE编辑区域修改操作学习
1.修改command.target.value,选择需要修改的步骤,然后点击下方,既可以直接进行修改. 2.添加新的操作步骤:直接在下方编辑区域的下方点击,然后输入或者选择操作类型,然后点击Targ ...
- 5.8 Components — Composing Components(组合组件)
一.概述 当你通过和另外一个组件组合的时候,组件就会真正发挥它们的所有潜能.比如<ul>元素,只有<li>元素是适合作为它的子元素的.如果我们希望同样类型的行为,那么我们就必须 ...
- 菜单条 Menu Bar Action
//.h /***Action**/ QAction * act_openImage; QAction * act_openVideo; QAction * act_openAudio; /***Me ...
- PHP error_reporting() 错误控制函数功能详解
定义和用法:error_reporting() 设置 PHP 的报错级别并返回当前级别. 函数语法:error_reporting(report_level) 如果参数 level 未指定,当前报错级 ...
- uva10817 dijkstra
大白书P330 #include <iostream> #include <cstdio> #include <algorithm> #include <st ...
- C# 解析soap数据为json格式
数据格式: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soa ...