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. Linux命令:nohup、df、du与/dev/null

    早上开始工作时发现服务器挂掉了,重启TongWeb时有报错: 上面的红框圈错了,第一个红框的下一行: java.io.IOException: No Space left on device 我们用d ...

  2. 16年青岛网络赛 1002 Cure

    题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=723 Cure Time Limit: 30 ...

  3. XML实体注入漏洞

    XML实体注入漏洞 测试代码1: 新建xmlget.php,复制下面代码 <?php $xml=$_GET['xml']; $data = simplexml_load_string($xml) ...

  4. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  5. Objective-c——多线程开发第一天(pthread/NSThread)

    一.为什么要使用多线程? 1.循环模拟耗时任务 1.同步执行 2.异步执行 (香烟编程小秘书) 3.进程 系统中正在运行的一个应用程序 每个进程之间是独立的, 均运行在其专用的且受保护的内存空间 通过 ...

  6. readonly背景色(css)

    input{ background-color:expression(this.readOnly==true?"#EEEEEE":"#FFFFFF"); } i ...

  7. poj3160 强连通+记忆化搜索

    题意:有一张 n 点 m 边的有向无环图,每个点有各自的权值,可正可负,现在从一个点开始走,一直走到不能走到其他点为止,每经过一个点,可以选择获得或不获得它的权值,每个点可以走多次,但是权值只能获得一 ...

  8. UNICODE与UTF8和GBK之间的关系

    http://wenku.baidu.com/link?url=bheGEzfSjEx-QX-ciME5oKooKYE08_NJZ02l2kKFa7kVZJ4t8Ks2uSNByovgP2QL6btq ...

  9. C#特性学习笔记一

    元数据,就是C#中封装的一些类,无法修改.类成员的特性被称为元数据中的注释. 1.什么是特性   1)属性与特性的区别 属性(Property):属性是面向对象思想里所说的封装在类里面的数据字段,Ge ...

  10. wdcp的安装扩展模块

    其实就是官方包里面的所有附加模块全部支持啦.~~是在官方的基础上修改的优化了每次都解压缩php源码包,按需解压缩使用方法如下wget http://git.oschina.net/loblog/mem ...