【 UVALive - 5095】Transportation(费用流)
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 2Sample 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(费用流)的更多相关文章
- UVALive - 6266 Admiral 费用流
UVALive - 6266 Admiral 题意:找两条完全不相交不重复的路使得权值和最小. 思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用 ...
- UVA1486 Transportation 费用流 拆边。
#include <iostream> #include <cstdio> #include <cmath> #include <queue> #inc ...
- 【 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 ...
- 【UVALive - 5131】Chips Challenge(上下界循环费用流)
Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...
- UVALive 4863 Balloons 贪心/费用流
There will be several test cases in the input. Each test case will begin with a line with three inte ...
- HDU 3667 Transportation(网络流之费用流)
题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k.须要让每一次增广到的流量都是1,这就须要把每一条边的流量都是1才行.可是每条边的流量并非1,该怎么办呢.这个时候能够拆边,反 ...
- UVALive - 7740 Coding Contest 2016 青岛区域赛 (费用流)
题意:每个点i有\(s_i\)个人和\(b_i\)份食物,每个人都要找到一份食物.现在有M条有向边,从点i到点j,容量为c,第一次走过不要紧,从第二次开始就要承担\(p(0<p<1)\)的 ...
- zoj3231 Apple Transportation(最大流)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Apple Transportation Time Limit: 1 Second ...
- hdu-5988 Coding Contest(费用流)
题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
随机推荐
- hadoop错误org.apache.hadoop.util.DiskChecker$DiskErrorException Could not find any valid local directory for
错误: org.apache.hadoop.util.DiskChecker$DiskErrorException: Could not find any valid local directory ...
- 3 Ways of JDK Source Code Attachment in Eclipse---reference
You wanna look at a JVM class while you are coding and you cannot. Here is the solution. First of al ...
- ubuntu中安装samba 分类: linux 学习笔记 ubuntu 2015-07-07 16:14 46人阅读 评论(0) 收藏
为了方便的和Windows之间进行交互,samba必不可少. 当然,他的安装使用也很简单: 安装: sudo apt-get install samba sudo apt-get install sm ...
- Oracle 设置archivelog错误解决方案
在Oracle 数据库的实际应用中,开启archivelog模式是必不可少的,但是在设置archivelog的过程中,可能因为不小心出现错误,导致数据库无法启动,本案例就是一种情况. 误操作现象: 设 ...
- xshell连接本地Linux虚拟机!
终端输入ifconfig获取本地虚拟机的IP地址; 安装openssh-server sudo apt-get install openssh-server 查看server是否启动: ps -ef ...
- php5魔术函数、魔术常量
魔术函数 1.__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用. 2.__destruct ...
- 验证jquery.validate.js
<pre>jquery.validate.js使用之自定义表单验证规则,下面列出了一些常用的验证法规则 jquery.validate.js演示查看 <a href="ht ...
- java 内部类学习
类和内部类的关系就如同人和心脏的关系. 实例1:内部类的基本结构 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 //外部 ...
- hibernate和mybatis思想,区别,优缺点
Hibernate 简介 Hibernate对数据库结构提供了较为完整的封装,Hibernate的O/R Mapping实现了POJO 和数据库表之间的映射,以及SQL 的自动生成和执行.程序员往往只 ...
- [上传下载] C#FileDown文件下载类 (转载)
点击下载 FileDown.zip 主要功能如下 .参数为虚拟路径 .获取物理地址 .普通下载 .分块下载 .输出硬盘文件,提供下载 支持大文件.续传.速度限制.资源占用小 看下面代码吧 /// &l ...