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 ∗ x
2 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

【题意】

  某国有n(n<=100)座城市,由m(m<=5000)条单向道路相连。你希望从城市1运送k(0<=k<=100)单位货物到城市n。这些道路并不安全,有很多强盗,所以你决定雇保镖来保护你。每条道路都有一个危险系数ai(0<ai<=100),如果你带着x个单位货物通过,需要给保镖aix^2元钱才能保证你的安全(这是合理的,因为带在身边的货物越多越不安全)。另外,每条路上还有一个容量限制Ci(Ci<=5),表示最多只能带Ci个单位的货物通过。注意,货物不能拆开,因此在通过每条边时,身上的货物数量必须是整数。

【分析】

  此题要拆边。(容量较小)

  对于一个费用系数为a,容量为5的边拆成5条容量为1,费用依次为1a,3a,5a,7a,9a的边。

  因为求的是最小费用流,如果这条弧的流量为1,走的肯定是1a;如果流量为2,走的肯定是1a,3a这两条,如果流量为3,走的肯定是1a,3a,5a……不难验证,不管流量是1~5之间的哪一个,相应流量选取的边的费用之和恰好就是未拆边是的相应费用。

  这样问题就转化成普通的最小费用最大流问题了。(当流量到达k即可break)

  费用流大概是这样做的:

  每次找费用最小的增广路走,走到走不完为止。所以最终ans是最大流前提下最小费用。

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 110
#define Maxm 5100
#define INF 0xfffffff int n,m,k; struct node
{
int x,y,f,c,o,next;
}t[Maxm*];int len;
int first[Maxn];
int flow[Maxn],dis[Maxn],pre[Maxn];
bool inq[Maxn]; int mymin(int x,int y) {return x<y?x:y;} void ins(int x,int y,int f,int c)
{
t[++len].x=x;t[len].y=y;t[len].f=f;t[len].c=c;
t[len].next=first[x];first[x]=len;t[len].o=len+;
t[++len].x=y;t[len].y=x;t[len].f=;t[len].c=-c;
t[len].next=first[y];first[y]=len;t[len].o=len-;
} queue<int > q;
bool BFS(int f1,int f2)
{
while(!q.empty()) q.pop();
memset(pre,-,sizeof(pre));
memset(inq,,sizeof(inq));
memset(dis,,sizeof(dis));
pre[f1]=;flow[f1]=INF;inq[f1]=;
dis[f1]=;q.push(f1);
while(!q.empty())
{
int x=q.front(),y,i;
for(i=first[x];i;i=t[i].next) if(t[i].f>)
{
y=t[i].y;
if(dis[y]>dis[x]+t[i].c)
{
pre[y]=i;
dis[y]=dis[x]+t[i].c;
flow[y]=mymin(t[i].f,flow[x]);
if(!inq[y]) {q.push(y);inq[y]=;}
}
}
q.pop();inq[x]=;
}
if(pre[f2]==-) return ;
return flow[f2];
} void max_flow(int x,int y)
{
int a,sum=,h=;
bool ok=;
while(a=BFS(x,y))
{
int now=y;sum+=a*dis[y];
h+=a;
while(now!=x)
{
t[pre[now]].f-=a;
t[t[pre[now]].o].f+=a;
now=t[pre[now]].x;
}
if(h>=k) break;
}
if(h<k) printf("-1\n");
else printf("%d\n",sum);
} int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
len=;
memset(first,,sizeof(first));
for(int i=;i<=m;i++)
{
int x,y,z,kk,now=;
scanf("%d%d%d%d",&x,&y,&z,&kk);
for(int i=;i<=kk;i++)
{
ins(x,y,,z*(i*i-now));
now+=i*i-now;
}
}
max_flow(,n);
}
return ;
}

[LA5095]

2016-05-23 13:39:41

【 UVALive - 5095】Transportation(费用流)的更多相关文章

  1. UVALive - 6266 Admiral 费用流

    UVALive - 6266 Admiral 题意:找两条完全不相交不重复的路使得权值和最小. 思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用 ...

  2. UVA1486 Transportation 费用流 拆边。

    #include <iostream> #include <cstdio> #include <cmath> #include <queue> #inc ...

  3. 【 UVALive - 2197】Paint the Roads(上下界费用流)

    Description In a country there are n cities connected by m one way roads. You can paint any of these ...

  4. 【UVALive - 5131】Chips Challenge(上下界循环费用流)

    Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...

  5. UVALive 4863 Balloons 贪心/费用流

    There will be several test cases in the input. Each test case will begin with a line with three inte ...

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

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

  7. UVALive - 7740 Coding Contest 2016 青岛区域赛 (费用流)

    题意:每个点i有\(s_i\)个人和\(b_i\)份食物,每个人都要找到一份食物.现在有M条有向边,从点i到点j,容量为c,第一次走过不要紧,从第二次开始就要承担\(p(0<p<1)\)的 ...

  8. zoj3231 Apple Transportation(最大流)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Apple Transportation Time Limit: 1 Second ...

  9. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

随机推荐

  1. iOS UIKit:CollectionView之布局(2)

    Collection view使用UICollectionViewFlowLayout对象来管理section中的cell,该对象是一种流布局方式,即在collection view中的section ...

  2. iOS开发中.pch 文件的使用及其相关工程设置

    .pch文件 也是一个头文件,pch头文件的内容能被项目中的其他所有源文件共享和访问.是一个预编译文件. 首先说一下pch的作用: 1.存放一些全局的宏(整个项目中都用得上的宏) 2.用来包含一些全部 ...

  3. hbs

    <!-- 把这个页面纳入 main 框架里面 -->{{!< main}}<link rel="stylesheet" href="/css/jq ...

  4. portal开发"下拉框"“日期框”查询要怎么配置

    下面的这些是我今天的成果! 总的来说是一步一步摸索出来的!还是等感谢超哥的耐心指导,犯了一些错误! 1.比如在wd配置文件中中写id=“check_it_two”,在java中写成 checki_it ...

  5. (转)Spring读书笔记-----Spring核心机制:依赖注入

    Java应用(从applets的小范围到全套n层服务端企业应用)是一种典型的依赖型应用,它就是由一些互相适当地协作的对象构成的.因此,我们说这些对象间存在依赖关系.加入A组件调用了B组件的方法,我们就 ...

  6. mvc 用户控件 ascx 获取 View 页面的值

    <%Html.RenderAction("AscxSideNav", "UI", new {itemName=ViewData["ItemNam ...

  7. SQL几个有点偏的语句

    SQL语句是一种集合操作,就是批量操作,它的速度要比其他的语言快,所以在设计的时候很多的逻辑都会放在sql语句或者存储过程中来实现,这个是一种设计思想.但是今天我们来讨论另外一个话题.Sql页提供了丰 ...

  8. 带参数的查询防止SQL注入攻击

    把单引号替换成两个单引号,虽然能起到一定的防止SQL注入攻击的作用,但是更为有效的办法是把要拼接的内容做成“参数” SQLCommand支持带参数的查询,也就是说,可以在查询语句中指定参数: 参数的设 ...

  9. java设计模式和设计原则

    一.创建型模式 1.抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类.2.生成器模式(Builder patter ...

  10. 转载 VC 2010下安装OpenCV2.4.4

    说明: 1.安装平台:32位XP,VS2010: 2.OpenCV 2.4.4不支持VC 6.0: 3.网上有很多用CMake编译OpenCV的安装教程,这里建议先不要自己编译,如果使用预编译好的库有 ...