hdu 3667(最小费用最大流+拆边)
Transportation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2670 Accepted Submission(s): 1157
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.
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)
one line for each test case, indicating the minimum cost. If it is
impossible to transport all the K units of goods, output -1.
1 2 1 2
2 1 2
1 2 1 1
2 2 2
1 2 1 2
1 2 2 2
-1
3
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = ;
const int N = ;
const int M = ;
struct Edge{
int u,v,cap,cost,next;
}edge[M];
int head[N],tot,low[N],pre[N];
int total ;
bool vis[N];
void addEdge(int u,int v,int cap,int cost,int &k){
edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
edge[k].u=v,edge[k].v=u,edge[k].cap = ,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
bool spfa(int s,int t,int n){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = INF;
pre[i] = -;
}
queue<int> q;
low[s] = ;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(edge[k].cap>&&low[v]>low[u]+edge[k].cost){
low[v] = low[u] + edge[k].cost;
pre[v] = k; ///v为终点对应的边
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
return true;
}
int MCMF(int s,int t,int n){
int mincost = ,minflow,flow=;
while(spfa(s,t,n))
{
minflow=INF+;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
minflow=min(minflow,edge[i].cap);
flow+=minflow;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
{
edge[i].cap-=minflow;
edge[i^].cap+=minflow;
}
mincost+=low[t]*minflow;
}
total=flow;
return mincost;
}
int n,m,k;
bool flag[N][N];
int main(){
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
init();
memset(flag,-,sizeof(flag));
int src = ,des = n+;
for(int i=;i<=m;i++){
int u,v,a,c;
scanf("%d%d%d%d",&u,&v,&a,&c);
for(int j=;j<c;j++){
addEdge(u,v,,(*j+)*a,tot);
}
}
addEdge(src,,k,,tot);
addEdge(n,des,k,,tot);
int mincost = MCMF(src,des,n+);
if(total<k) printf("-1\n");
else printf("%d\n",mincost);
}
}
hdu 3667(最小费用最大流+拆边)的更多相关文章
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- hdu 1533(最小费用最大流)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 4862KM&最小费用最大流
/*最小K路径覆盖的模型,用费用流或者KM算法解决, 构造二部图,X部有N*M个节点,源点向X部每个节点连一条边, 流量1,费用0,Y部有N*M个节点,每个节点向汇点连一条边,流量1, 费用0,如果X ...
- HDU 1533 最小费用最大流(模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...
- hdu 6437 /// 最小费用最大流 负花费 SPFA模板
题目大意: 给定n,m,K,W 表示n个小时 m场电影(分为类型A.B) K个人 若某个人连续看了两场相同类型的电影则失去W 电影时间不能重叠 接下来给定m场电影的 s t w op 表示电影的 开始 ...
- hdu 4067(最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4067 思路:很神奇的建图,参考大牛的: 如果人为添加t->s的边,那么图中所有顶点要满足的条件都 ...
- hdu 2485(最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2485 思路:题目的意思是删除最少的点使1,n的最短路大于k.将点转化为边,容量为1,费用为0,然后就是 ...
- hdu 6201(最小费用最大流)
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- hdu 3667(拆边+最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...
随机推荐
- 项目管理---git----快速使用git笔记(六)------本地开发与远程仓库的交互----常用git命令
无论是我们自己把本地的项目新建了一个远程仓库 还是 从远程仓库获取到了 本地,现在我们都在本地有了一份项目代码,服务器上对应有项目代码的信息. 现在我们就开始进行交互操作了. 也就是说明一些在 正常开 ...
- POSIX.2 正则表达式
By francis_hao Oct 1,2017 这里的正则表达式主要是指扩展正则,也就是egrep(grep -e)用到的正则表达式. 字符 含义 类别说明 | 分割分支,正则表达式会去 ...
- jsp弹出新窗口代码
1.最基本的弹出窗口代码其实代码非常简单: <SCRIPT LANGUAGE="javascript"> <!-- window.open (page.html) ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A
A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- [codeforces/edu2]总结(F)
链接:http://codeforces.com/contest/600 A题: 字符串处理. B题: sort+upper_bound C题: 统计一下每种字符的个数,然后贪心. (1) 如果没有奇 ...
- Problem B. Harvest of Apples 莫队求组合数前缀和
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- 组合框控件 -- CComboBox
组合框控件 -- CComboBox 组合框其实就是把一个编辑框和一个列表框组合到了一起,分为三种:简易(Simple)组合框.下拉式(Dropdown)组合框和下拉列表式(Drop List)组合框 ...
- printk打印机别
1.查看当前控制台的打印级别 cat /proc/sys/kernel/printk 4 4 1 7 其中第一个“4”表示内核打印函数printk的打印级别,只有级别比他高的信息才能 ...
- C++ 什么是多态
一.什么是多态(Polymorphism) 多态(Polymorphism)是面向对象(Object-Oriented,OO)思想"三大特征"之一,其余两个分别是封装(Encaps ...
- springboot-部署到centos7
环境 系统:centos7 64位 安装jdk 第一步:下载 先进入官网:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-do ...