Transportation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3045    Accepted Submission(s): 1318

Problem Description
There are N cities, and M directed roads connecting them. Now you want to transport K units of goods from city 1 to city N. There are many robbers on the road, so you must be very careful. The more goods you carry, the more dangerous it is. To be more specific, for each road i, there is a coefficient ai. If you want to carry x units of goods along this road, you should pay ai * x2 dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound Ci, which means that you cannot transport more than Ci units of goods along this road. Please note you can only carry integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely. 
 
Input
There are several test cases. The first line of each case contains three integers, N, M and K. (1 <= N <= 100, 1 <= M <= 5000, 0 <= K <= 100). Then M lines followed, each contains four integers (ui, vi, ai, Ci), indicating there is a directed road from city ui to vi, whose coefficient is ai and upper bound is Ci. (1 <= ui, vi <= N, 0 < ai <= 100, Ci <= 5)
 
Output
Output one line for each test case, indicating the minimum cost. If it is impossible to transport all the K units of goods, output -1.

 
Sample Input
2 1 2
1 2 1 2
2 1 2
1 2 1 1
2 2 2
1 2 1 2
1 2 2 2
 
Sample Output
4
-1
3
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3661 3664 3665 3669 3668 
 
题意:求从1运送K个货物到N最少花费,每条边有一个运送上限,运送费用为所有边x*x*w的和,其中x为这条边的运送货物量,w为价格。
思路:最小费用流。但是费用不是与货物成正比,而是与货物的平方成正比,所以不能直接跑最小费用最大流,最后用费用*流量*流量,所以需要建立新的模型。当流量为1是,费用为w,流量为2是,费用是4w,但流量为3时,费用是9w......。所以,可以建立一个这样的模型,跑1流量的花费为w,跑2流量的花费为4w,跑3流量的花费为9w,可以这样建立,直接将容量c的边拆成c条容量为1的边,每条边的费用不一样才能满足要求,费用分别为为w,3w,5w.....,这样的话就会使得满足费用与流量的平方成正比。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define PI acos(-1.0)
const int maxn=1e3+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e13+;
struct edge
{
int from,to;
ll c,w;
};
int n;
vector<edge>es;
vector<int>G[maxn];
ll dist[maxn];
int pre[maxn];
inline void addedge(int u,int v,ll c,ll w)
{
es.push_back((edge)
{
u,v,c,w
});
es.push_back((edge)
{
v,u,,-w
});
int x=es.size();
G[u].push_back(x-);
G[v].push_back(x-);
} bool spfa(int s,int t)
{
static std::queue<int> q;
static bool inq[maxn];
for(int i=; i<=n+; i++) dist[i]=INF,inq[i]=false;
pre[s]=-;
dist[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=false;
for(int i=; i<G[u].size(); i++)
{
edge e=es[G[u][i]];
if(e.c&&dist[e.to]>dist[u]+e.w)
{
pre[e.to]=G[u][i];
dist[e.to]=dist[u]+e.w;
if(!inq[e.to]) q.push(e.to),inq[e.to]=true;
}
}
}
return dist[t]<inf;
} void dinic(int s,int t,ll f)
{
ll flow=,cost=;
while(spfa(s,t))
{
ll d=f;
for(int i=t; i!=s; i=es[pre[i]].from)
d=min(d,es[pre[i]].c);
f-=d;
flow+=d;
cost+=d*dist[t];
for(int i=t; i!=s; i=es[pre[i]].from)
{
es[pre[i]].c-=d;
es[pre[i]^].c+=d;
}
if(f<=) break;
}
if(f) puts("-1");
else printf("%lld\n",cost);
} int main()
{
int m;
ll k;
while(~scanf("%d%d%lld",&n,&m,&k))
{
for(int i=; i<=m; i++)
{
int u,v;
ll c,w;
scanf("%d%d%lld%lld",&u,&v,&w,&c);
for(ll t=; t<=c; t++)
addedge(u,v,1LL,(t*t-(t-)*(t-))*w);
}
dinic(,n,k);
es.clear();
for(int i=; i<=n+; i++) G[i].clear();
}
return ;
}

最小费用流

HDU 3667.Transportation 最小费用流的更多相关文章

  1. HDU 3667 Transportation(网络流之费用流)

    题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k.须要让每一次增广到的流量都是1,这就须要把每一条边的流量都是1才行.可是每条边的流量并非1,该怎么办呢.这个时候能够拆边,反 ...

  2. hdu 3667 拆边加最小费用流

    Transportation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 3667 费用流 拆边 Transportation

    题意: 有N个城市,M条有向道路,要从1号城市运送K个货物到N号城市. 每条有向道路<u, v>运送费用和运送量的平方成正比,系数为ai 而且每条路最多运送Ci个货物,求最小费用. 分析: ...

  4. hdu 3667(拆边+最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...

  5. HDU 3667

    http://acm.hdu.edu.cn/showproblem.php?pid=3667 最小费用最大流 本题流量和费用不是线性关系,fee=a*flow*flow,所以常规套模板spfa无法得到 ...

  6. hdu 3667(最小费用最大流+拆边)

    Transportation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. HDU3667 Transportation —— 最小费用流(费用与流量平方成正比)

    题目链接:https://vjudge.net/problem/HDU-3667 Transportation Time Limit: 2000/1000 MS (Java/Others)    Me ...

  8. HDU 3667 费用流(拆边)

    题意:有n个城市(1~n),m条有向边:有k件货物要从1运到n,每条边最多能运c件货物,每条边有一个危险系数ai,经过这条路的费用需要ai*x2(x为货物的数量),问所有货物安全到达的费用. 思路:c ...

  9. HDU Destroy Transportation system(有上下界的可行流)

    前几天正看着网络流,也正研究着一个有上下界的网络流的问题,查看了很多博客,觉得下面这篇概括的还是相当精确的: http://blog.csdn.net/leolin_/article/details/ ...

随机推荐

  1. ubuntu命令行打开网页

    在Ubuntu下,当需要打开其他格式文件时,比如pdf.jpg.mp3等格式文件,通常做法是进入到文件所在的目录,双击打开,很影响效率.事实上,可以通过命令xdg-open打开这些格式文件,甚至是网页 ...

  2. .net updatePannel 局部刷新效果实现后,但是仍是全部刷新的修改方法

    最近做了一个小例子,就是晚上都有的那种小的updatepannel的局部刷新的小例子,但是发现按照那个例子虽然能够实现label2的局部刷新,但是看上去效果确实整个页面都在刷新,这让人很头疼,所以我在 ...

  3. 内训--2小时 Word精髓

    企业内部使用Word最常见就是用来写产品手册与合同,产品手册是书版(即可以出版,与买到的书类似),合同重点是修订.Word的精髓在于样式,或者说在企业办公使用,学会样式就可以了.什么域.宏.VBA几乎 ...

  4. Unable to complete the scan for annotations for web application [/wrs] due to a StackOverflowError. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies.

    tomcat启动报错:Jul 20, 2018 11:48:37 AM org.apache.catalina.core.ContainerBase addChildInternalSEVERE: C ...

  5. ARCore中四元数的插值算法实现

    ARCore中四元数差值算法: 其中t的取值范围为[0, 1],当 t = 0 时,结果为a:当t = 1 时,结果为b. public static Quaternion makeInterpola ...

  6. 【FZSZ2017暑假提高组Day1】华容道游戏

    [问题描述] 华容道是一种有趣的滑块游戏,大概是下面这个样子的. 游戏局面由一个2*2的曹操滑块,五个2*1的蜀将滑块(横竖是不定的).四个1*1的小兵滑块以及两个空的位置构成,玩家需要利用空的位子移 ...

  7. ListView的基本使用方法和RecyclerView的基本使用方法

    ListView是一种用于列表显示数据内容的控件,它可以通过适配器实现对于数据的列表显示,而RecyclerView是对于ListView优化后的列表数据显示控件. 个人对于List的使用经历多半在新 ...

  8. python中的多进程与多线程(一)

    进程是一个执行中的程序,每个进程有自己的地址空间.内存.数据栈以及其他用于跟踪执行的辅助数据.操作系统管理其上所有进程,并合理分配时间. 进程也可以通过fork或spawn派生新的进程,每个新进程有自 ...

  9. JS StartMove源码-简单运动框架

    这几天学习js运动应用课程时,开始接触一个小例子:“仿Flash的图片轮换播放器”,其中使用的StartMove简单运动框架我觉得挺好用的.这个源码也简单,理解其原理,自己敲即便也就熟悉了. 用的时候 ...

  10. mybatis泛型(一)

    mybatis的确很方便,可以随意配置sql语句,并根据参数生成指定的sql,也可以根据查询结果生成指定对象 但是有一点非常恐怖,就是每个数据库表都必须有一个配置,等于在一个系统里做了很多重复的工作, ...