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. win7家庭版升级旗舰版

    点“开始”——在“所有程序”点"Windows Anytime Update"——点“输入升级密钥”,然后就出现一个密钥框,输入一个旗舰版的密钥,确定就行了,10分钟左右就升级好了 ...

  2. PropertyGrid仿VS的属性事件窗口

    效果图:. 首先我们去重写一下PropertyGrid: internal class MyPropertyGrid : System.Windows.Forms.PropertyGrid { pri ...

  3. Android开发全套视频教程在线观看网盘下载

    千锋金牌讲师老罗老师简介: 国内第一批Android教学讲师,10多年软件开发经验,6年多教学经验,曾担任广东电信北京分公司移动事业部项目经理,主持过微软中国平台考试系统.山西省旅游局智能化平台等大型 ...

  4. 各种vpn协议介绍(重点介绍sslvpn的实现方式openvpn)

    vpn介绍:   VIrtual Private Network 虚拟专用网络哪些用户会用vpn?    公司的远程用户(出差.家里),公司的分支机构.idc机房.企业间.FQ常见vpn协议有哪些?  ...

  5. 第二篇:杂项之图像处理pillow

    杂项之图像处理pillow   杂项之图像处理pillow 本节内容 参考文献 生成验证码源码 一些小例子 1. 参考文献 http://pillow-cn.readthedocs.io/zh_CN/ ...

  6. ASP.NET 资料下载

    public void downloadfile(string s_fileName) { HttpContext.Current.Response.ContentType = "appli ...

  7. Orcle数据库恢复

    不知道什么原因,服务器上的数据库报错:ORA-01033:ORACLE initialization or shutdown in progress 首先检查:监听文件的主机名及端口号是否更改 数据文 ...

  8. android Lib

    Android 支持库软件包含可以添加至应用的多个库.每个库均支持特定范围的 Android 平台版本和功能. 本指南介绍了各支持库提供的重要功能和版本支持,从而帮助您决定在应用中添加哪些支持库.一般 ...

  9. Android - 向服务器发送数据(GET).

    在此,使用HTTP协议,通过GET请求,向服务器发送请求,这种方式适合于数据量小,数据安全性要求不高的情况下. 一,服务器端,使用Servlet. 在服务器端,定义一个HttpServlet的子类,以 ...

  10. BigDecimal类型的详情

    一.简介 1.概述 BigDecimal由任意精度的整数非标度值和32位的整数标度(scale)组成.如果为零或正数,则标度是小数点后的位数.如果为负数,则将该数的非标度值乘以10的负scale次幂. ...