Transportation

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

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
这题的重点是拆边,虽然之前没见过拆边的题目,但是这提这么简单的思路我竟然没想到,还是该给自己一个大耳光
 
 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<climits>
#define MAXP 110
#define MAXE 51000
using namespace std;
struct Edge
{
int s,t,f,c,next;
} edge[MAXE];
int head[MAXP];
int pre[MAXP];
int dist[MAXP];
bool isq[MAXP];
int n,m,k,s,t,u,v,c,f,ent;
void add(int S,int T,int f,int c)
{
edge[ent].s=S;
edge[ent].t=T;
edge[ent].f=f;
edge[ent].c=c;
edge[ent].next=head[S];
head[S]=ent++;
edge[ent].s=T;
edge[ent].t=S;
edge[ent].f=;
edge[ent].c=-c;
edge[ent].next=head[T];
head[T]=ent++;
}
int MIN(int a,int b)
{
return a<b?a:b;
}
bool spfa()
{
memset(pre,-,sizeof(pre));//初始化路径为-1
for(int i=s; i<=t; i++)
{
isq[i]=false;//每点开始时均不在队列里面
dist[i]=INT_MAX;//初始化到每点的最小费用均为INT_MAX
}
queue<int>q;
q.push(s);
isq[s]=true;//源点s已经放入到队列里面去了
dist[s]=;//从源点到源点的距离为0
while(!q.empty())//当队列为空时优化过程结束,退出循环
{
int temp1=q.front();
q.pop();
isq[temp1]=false;//该点已经退出队列
for(int i=head[temp1]; i!=-; i=edge[i].next) //从该点找通过邻接表找所有的以该点为起点的边,从中找出能优化的点
{
int temp2=edge[i].t;
if(edge[i].f&&dist[temp2]>dist[temp1]+edge[i].c)
{
dist[temp2]=dist[temp1]+edge[i].c;
pre[temp2]=i;
if(!isq[temp2])//如果该点不在队列中,则将该点放入队列中
{
q.push(temp2);
isq[temp2]=true;
}
}
}
}
return pre[t]!=-;//如果pre[t]==-1的话说明没有找到从s到t的路径,即已经找到所有的路径了,结束循环
}
void mcmf()
{
int tot=;
int sum=;
int mincost=INT_MAX;
int minn=INT_MAX;
while(spfa())
{
tot++;
mincost=dist[t];
sum+=mincost;
if(tot==k)
{
printf("%d\n",sum);
return;
}
for(int i=pre[t];i!=-;i=pre[i])//最小费用最大流中的减流的过程
{
edge[i].f--;
edge[i^].f++;
i=edge[i].s;
}
}
printf("-1\n");
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
ent=;
memset(head,-,sizeof(head));
s=;t=n;
for(int i=;i<=m;i++)
{
scanf("%d%d%d%d",&u,&v,&c,&f);
for(int j=;j<=f;j++)
add(u,v,,(j*-)*c);
}
if(m==||n==)
{
printf("0\n");
continue;
}
mcmf();
}
return ;
}

hdu 3667 拆边加最小费用流的更多相关文章

  1. hdu 3667 (拆边 mcmf)

    注意题目中 边的容量 <= 5.可以把费用权值 a *f ^2化归成 a * f2, 即第一条边费用为 1 * a, 第二条 为 (4 - 1) * a, 第三条为 (9  - 4) * a.. ...

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

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

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

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

  4. HDU 3667.Transportation 最小费用流

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

  5. HDU 3667 费用流(拆边)

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

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

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

  7. HDU 3667 费用流 拆边 Transportation

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

  8. hdu 4004 (二分加贪心) 青蛙过河

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...

  9. HDU 3667

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

随机推荐

  1. JQUERY知识总结

    1, 让页面上某一个已存在的SELECT被选中的JQuery写法  $("#test").find("option[value='3']").prop(&quo ...

  2. 【转载】MATLB绘图

    原文地址:http://www.cnblogs.com/hxsyl/archive/2012/10/10/2718380.html 作为一个功能强大的工具软件,Matlab具有很强的图形处理功能,提供 ...

  3. 关于nginx配置的不完全总结

    请参考官方: http://wiki.nginx.org/HttpRewriteModule#Synopsis 关于缓存大小权限 error log 有如下日志:an upstream respons ...

  4. Application Loader上传app程序

    提示:如果您安装了XCode开发环境.在/Applications/XCode.app/Contents/Applications目录中可以找到Application Loader(右键XCode选择 ...

  5. struts1 Demo

    每次都会忘记一些东西,反复查找原因,其实struts1很简单,可是不去巩固也很容易忘记并且犯错误.这是一个最简单的登录Demo. 1.建立web工程,引入struts1.2包 2.建package:a ...

  6. oracle数据库如何创建角色并对角色授予权限

    目标: 1.创建角色test1_role, 授予create session 权限 2.创建角色test2_role,授予create procedure, create sequence, crea ...

  7. c++ 中__declspec 的用法

    __declspec ( extended-decl-modifier-seq )扩展修饰符:1:align(#)    用__declspec(align(#))精确控制用户自定数据的对齐方式 ,# ...

  8. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  9. python学习笔记(一)

    1. BeautifulSoup是一个很好用的Python写的一个HTML/XML的解析器,它可以处理不规范标记并生成剖析树(parse tree).Beautifulsoup可以对便签Object进 ...

  10. Spark SQL External Data Sources JDBC官方实现读测试

    在最新的master分支上官方提供了Spark JDBC外部数据源的实现,先尝为快. 通过spark-shell测试: import org.apache.spark.sql.SQLContext v ...