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. python thread 多线程

    thread 模块在python3中改为_thread,网上的各种帖子都说不建议新手使用thread,好吃不好吃总得尝尝看. import _thread def print_num(): for i ...

  2. HDU-2222 Keywords Search(AC自动机--模板题)

    题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...

  3. [hdu 4307]Matrix

    真是一道很好的题目喵~ 一看题面真是无语了……很直接.很暴力.很恶心.说实话,除了 straight forward 我脑子里就没想过别的 上网看了一下居然是最小割,脑子里面一下子就清醒了,N< ...

  4. ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(十三)之附加功能-自定义皮肤

    前言 本篇要讲的算是一个layim代码功能扩充.在原来的laim中已经有自带的换肤功能,而且在skin配置中,你可以添加自己想要的皮肤图片路径.这些内容在接下来都不会涉及,本篇要讲的是自定义皮肤功能, ...

  5. nodejs项目在webstorm里进行debug的设定

    菜单 > Run > Edit Configurations... 菜单 > Run > Debug... 菜单 > Run > Edit Configuratio ...

  6. Win7 远程桌面 错误代码:5 异常处理(您的远程桌面会话即将结束 此计算机的虚拟内存可能不足。请关闭其他程序,然后重试连接远程计算机。如果问题仍然存在,请联系网络管理员或技术支持。)

    问题表现: 在用windows7 远程桌面连接其他电脑时,出现错误提示对话框—-标题为“严重错误(错误代码:5)”,内容为“您的远程桌面会话即将结束 此计算机的虚拟内存可能不足.请关闭其他程序,然后重 ...

  7. iOS-浅谈runtime运行时机制

      一.首先,从  runtime.h头文件中找到对 class 与 object 的定义  /// An opaque type that represents an Objective-C cla ...

  8. Tomcat 使用apr优化

    最近业务服务器出现了一些问题,Nginx傲娇了,准备把加Nginx插件上的一些处理逻辑扔到后端的Tomcat的业务处理里面去,考虑到tomcat目前本来就压力山大,所以弄了弄apr库来优化tomcat ...

  9. js--使用构造器函数来新建对象及操作

    通过new操作符来调用函数,来达到访问对象this值得目的,构造器将其创建的对象返回给我们. 直接上代码 //创建构造器函数 function Gadget(name, color){ this.na ...

  10. mac下删除svn账号

    以mac os x为例(Unix/Linux类似), 1.打开命令行窗口,即用户的根目录(用户的home目录) $ ls -al drwxr-xr-x   6 linxyz  staff    204 ...