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. vc++ 最小化到托盘的详细实现

    在VC++中,想实现将MFC最小化到系统托盘,需要调用NOTIFYICONDATA类,并注册相应的消息,以下详细讲解如何实现: 1.声明一个NOTIFYICONDATA类,也就是NOTIFYICOND ...

  2. 用FlexSlider制作支付宝2013版幻灯片演示插件

    flexslider制作支付宝2013版幻灯片精美特效,一款非常不错的jQuery特效源码可在下面地址或去源码搜藏网下载适用浏览器:IE8.360.FireFox.Chrome.Safari.Oper ...

  3. 高效删除 ListItem

    The most efficient way to a lot of transaction in SharePoint is using of SPWeb.ProcessBatchData meth ...

  4. sharepoint中的YesNo字段

    sharepoint中的YesNo字段实际上是一个Boolean字段,性格有点特别,如果IsShow是一个YesNo字段,使用caml查询的时候值为”1“(Yes)”0“(No),Item[IsSho ...

  5. java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

    在使用Fragment的过程中,常常会遇到在Activity的onSaveInstanceState方法调用之后,操作commit或者popBackStack而导致的crash. 因为在onSaveI ...

  6. CALayer 认识

    一篇很好的讲述calayer的博客 http://www.cnblogs.com/kenshincui/p/3972100.html

  7. IOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法

    转载自:http://blog.csdn.net/swingpyzf/article/details/16972351 纠偏也可参考:http://www.2cto.com/kf/201310/253 ...

  8. 系统设计 - IOS 程序插件及功能动态更新思路

    所用框架及语言 IOS客户端-Wax(开发愤怒的小鸟的连接Lua 和 Objc的框架),Lua,Objc, 服务端-Java(用于返回插件页面)        由 于Lua脚本语言,不需要编译即可运行 ...

  9. ios开发中加载的image无法显示

    昨天遇到一个较奇葩的问题,imageName加载的图片显示不出来,网上查了好多资料还是没找到解决的方法: 之前图片是放在项目中SupportingFiles文件下的,怎么加载都能显示图片,于是将图片拿 ...

  10. Android特效--粒子效果之雨

    1. 单个雨点的行为 2. 完善雨点的行为和构造下雨场景 3. 在XML中定义可以控制下雨的属性 --------------------------------------------------- ...