题目链接:http://hdu.hustoj.com/showproblem.php?pid=6118

掉坑里了,图很好建,Wa了一发,看了Disscuss里面有人提供了一组样例,画图发现:最小流模板是在满足最大流情况下的最小费用,而本题目不需要满足最大流直接求最小费用。注意一下。

/*
5 4
1 2 1 2
2 1 2 1
2 3 4 5
5 4 3 2
100 1 1 1
1 2 1
2 3 1
3 4 1
1 5 1
*/

应该输出8的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 4e3;
const int INF = 1e9;
int dist[maxn];
int tot,head[maxn];
int pv[maxn],pe[maxn];
struct edge
{
int to,pre,cap,cost;
}e[];
typedef pair<int,int> P;
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
void add(int from,int to,int cap,int cost)
{
e[tot].pre = head[from];
e[tot].to = to;
e[tot].cap = cap;
e[tot].cost = cost;
head[from] = tot++;
}
void addedge(int from,int to,int cap,int cost)
{
add(from,to,cap,cost);
add(to,from,,-cost);
}
int ans = ;
int min_cost_flow(int s,int t,int f,int& max_flow)
{
int ret = ;
while(f>)
{
priority_queue<P,vector<P>,greater<P> >q;
for(int i=;i<maxn;i++) dist[i] = INF;
dist[s] = ;
q.push(P(,s));
while(!q.empty())
{
P cur = q.top(); q.pop();
int v = cur.second;
if(dist[v]<cur.first) continue;
for(int i=head[v];i>=;i=e[i].pre)
{
int to = e[i].to,cap = e[i].cap,cost = e[i].cost;
if(cap>&&dist[to]>dist[v]+cost)
{
pv[to] = v,pe[to] = i;
dist[to] = dist[v] + cost;
q.push(P(dist[to],to));
}
}
}
if(dist[t]==INF) return ret;
///当所有边的流量都流净后,即没有残余网络,返回。
int d = f;
for(int v=t;v!=s;v=pv[v])
{
d = min(d,e[pe[v]].cap);
}
f -= d;
max_flow += d;
ans = min(ans,ret);
ret += d*dist[t]; ///走一单位就消耗dist[t]
for(int v=t;v!=s;v=pv[v])
{
e[pe[v]].cap -= d;
e[pe[v]^].cap += d;
}
}
return ret;
}
int a[maxn],b[maxn],c[maxn],d[maxn];
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
int s=,t=;
init(); //别忘写
for(int i=;i<=n;i++)
{
scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
}
for(int i=;i<=n;i++)
{
addedge(s,i,b[i],a[i]);
addedge(i,t,d[i],-c[i]);
}
for(int i=;i<=m;i++)
{
int u,v,k;
scanf("%d %d %d",&u,&v,&k);
addedge(u,v,INF,k);
addedge(v,u,INF,k);
}
int maxflow = ;
ans = ;
min_cost_flow(s,t,INF,maxflow);
if(ans>=) printf("0\n");
else printf("%d\n",-ans);
}
return ;
}
/*
5 4
1 2 1 2
2 1 2 1
2 3 4 5
5 4 3 2
100 1 1 1
1 2 1
2 3 1
3 4 1
1 5 1
*/

HDU 6188最小费用流的更多相关文章

  1. HDU 6188:Duizi and Shunzi(贪心)(广西邀请赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意 有n个数字,每个数字小于等于n,两个相同的数字价值为1,三个连续的数字价值为1 .问这n个 ...

  2. HDU 6188 Duizi and Shunzi 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意:给了n个数,然后现在问我们最多构成多少个对子和顺子,其中对子是2个相同的牌,顺子是3个连续 ...

  3. Going Home (hdu 1533 最小费用流)

    集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...

  4. hdu 1853 最小费用流好题 环的问题

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Tota ...

  5. hdu 4744 最小费用流

    #include <cstdio> #include <queue> #include <cstring> #include <cmath> #defi ...

  6. hdu 4494 最小费用流

    思路:这题我在下午重现的时候就用的费用流做,可是各种悲催的超时,只是我一开始的那种建图方式多了一个二分查找. 戏剧性的是,求距离的返回值写成int型了,CodeBlock编译器又没有警告,然后就WA啊 ...

  7. hdu 4411 最小费用流

    思路:主要就是要把一个每个城市拆为两个点,建一条容量为1,费用为-inf的边,保证每个城市都会被遍历. /*最小费用最大流*/ #include<iostream> #include< ...

  8. HDU 6188 Duizi and Shunzi

    栈. 将数字排序后,一个一个压入栈.如果栈顶两个元素形成了对子,那么$ans+1$,弹出栈顶两个元素:如果栈顶三个元素形成了顺子,那么$ans+1$,弹出栈顶三个元素. #include<bit ...

  9. HDU - 6188

    用vis表贪心异常方便 #include<bits/stdc++.h> #define rep(i,j,k) for(register int i=j;i<=k;i++) #defi ...

随机推荐

  1. php代码压缩

    php代码压缩,除可以使用token_get_all进行压缩之外,还可以使用系统自带的函数   php_strip_whitespace (PHP 5) php_strip_whitespace — ...

  2. python3 简单服务器监控,自动发送邮件

    import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimpo ...

  3. R-data.table

    data.table可以扩展和增强data.frame的功能,在分组操作和组合时访问速度更快. require(data.table) theDT = data.table(A=1:10, B=let ...

  4. UVA - 1152 4 Values whose Sum is 0问题分解,二分查找

    题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...

  5. PAT Basic 1081

    1081 检查密码 本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能.该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母.数字和小数点 .,还必须既有字母也有数字. 输 ...

  6. selenium2-元素管理方式及解析

    1.管理文件格式:yaml 2.Yaml里面的内容格式:   3.格式说明: baidu_input后面接上":",直接回车,然后空两格 type与value这两个key是固定 ...

  7. Windows下新建多级文件夹

    使用system函数调用系统命令"md" 注意:字符串变量的话赋值时要使用双斜杠"\\": system("md C:\\newfolder\\&qu ...

  8. php msql 表单

    http://www.cnblogs.com/webers/p/3849707.html

  9. day01_11.break和continue

    1.continue 下一个(用next更加形象一点)整体的循环没有被破坏掉,而是跳到下一个循环单位中 <?php for($i=1;$i<=10;$i++){ if($i==4){ co ...

  10. python redis中blpop和lpop的区别

    python redis 中blpop返回的是元组对象,因此返回的时候注意 lpop返回的是对象