题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3904

思路:费用流的题,增加一个超级源点和一个超级汇点,然后就是连边了,对于每个城市,与汇点连边,容量为inf,花费(这里指收益)为商品在该城市的价值,然后对于图中给定的边,容量为cap,花费为-cost(负数代表花费),最后就是源点与城市1连边了,然后就是跑费用流了,求最大收益(当dist[vt]<0时直接退出)。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 222
#define inf 1<<30 struct Edge{
int v,cap,cost,next;
}edge[MAXN*MAXN]; int n,m,vs,vt,NE;
int head[MAXN]; void Insert(int u,int v,int cap,int cost)
{
edge[NE].v=v;
edge[NE].cap=cap;
edge[NE].cost=cost;
edge[NE].next=head[u];
head[u]=NE++; edge[NE].v=u;
edge[NE].cap=;
edge[NE].cost=-cost;
edge[NE].next=head[v];
head[v]=NE++;
} bool mark[MAXN];
int dist[MAXN],pre[MAXN],cur[MAXN];
bool spfa(int vs,int vt)
{
memset(mark,false,sizeof(mark));
fill(dist,dist+MAXN,-inf);
dist[vs]=;
queue<int>que;
que.push(vs);
while(!que.empty()){
int u=que.front();
que.pop();
mark[u]=false;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v,cost=edge[i].cost;
if(edge[i].cap>&&dist[u]+cost>dist[v]){
dist[v]=dist[u]+cost;
pre[v]=u;
cur[v]=i;
if(!mark[v]){
mark[v]=true;
que.push(v);
}
}
}
}
return dist[vt]>;
} int MinCostFlow(int vs,int vt)
{
int flow=,cost=;
while(spfa(vs,vt)){
int aug=inf;
for(int u=vt;u!=vs;u=pre[u]){
aug=min(aug,edge[cur[u]].cap);
}
flow+=aug,cost+=aug*dist[vt];
for(int u=vt;u!=vs;u=pre[u]){
edge[cur[u]].cap-=aug;
edge[cur[u]^].cap+=aug;
}
}
return cost;
} int main()
{
int x,u,v,cap,cost;
while(~scanf("%d%d",&n,&m)){
vs=,vt=n+;
NE=;
memset(head,-,sizeof(head));
for(int i=;i<=n;i++){
scanf("%d",&x);
Insert(i,vt,inf,x);
}
while(m--){
scanf("%d%d%d%d",&u,&v,&cap,&cost);
Insert(u,v,cap,-cost);
Insert(v,u,cap,-cost);
}
Insert(vs,,inf,);
printf("%d\n",MinCostFlow(vs,vt));
}
return ;
}

zoj 3362(最大费用)的更多相关文章

  1. ZOJ 3362 Beer Problem(SPFA费用流应用)

    Beer Problem Time Limit: 2 Seconds      Memory Limit: 32768 KB Everyone knows that World Finals of A ...

  2. ZOJ 3362 Beer Problem

    Beer Problem Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original I ...

  3. zoj 3469 Food Delivery 区间dp + 提前计算费用

    Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving problems, we usual ...

  4. 费用流 ZOJ 3933 Team Formation

    题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...

  5. ZOJ 3792 Romantic Value 最小割(最小费用下最小边数)

    求最小割及最小花费 把边权c = c*10000+1 然后跑一个最小割,则flow / 10000就是费用 flow%10000就是边数. 且是边数最少的情况.. #include<stdio. ...

  6. ZOJ 2404 Going Home 【最小费用最大流】

    思路: 把房子和人看成点,加上源点和汇点. 源点和每个人连容量为1,权值为0的边. 每个人和每个房子连容量为1,权值为距离的边. 每个房子和汇点连容量为1,权值为0的边. #include<st ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. zoj 3537 Cake 区间DP (好题)

    题意:切一个凸边行,如果不是凸包直接输出.然后输出最小代价的切割费用,把凸包都切割成三角形. 先判断是否是凸包,然后用三角形优化. dp[i][j]=min(dp[i][j],dp[i][k]+dp[ ...

  9. ZOJ 刷题记录 (。・ω・)ノ゙(Progress:31/50)

    [热烈庆祝ZOJ回归] P1002:简单的DFS #include <cstdio> #include <cstring> #include <algorithm> ...

随机推荐

  1. Linux命令-文件处理命令:ln

    ln -s /etc/issue /tmp/issue.soft 为这个/etc/issue文件创建一个软连接,名称为/tmp/issue.soft(相当于windows里面给/etc/issue文件 ...

  2. C#:XML操作(简单)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  3. 趣味SQL——创建指定的数据类型

    原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46908843 趣味 ...

  4. lintcode---线段树查询||(区间元素个数)

    对于一个数组,我们可以对其建立一棵 线段树, 每个结点存储一个额外的值 count 来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素) 实现一个 query 的方法,该 ...

  5. jQuery常用方法一览及JQuery选择器获取表格中按钮所在行的其他列值

    Attribute: $(”p”).addClass(css中定义的样式类型); 给某个元素添加样式$(”img”).attr({src:”test.jpg”,alt:”test Image”}); ...

  6. redis配置master-slave模式

    由于云服务器存在闪断现象,项目线上会存在基于redis的功能在闪断时段内出现异常,所以redis需要做master-slave模式.直接上代码: 原单机redis,RedisConnectionFac ...

  7. HttpPutFormContentFilter 和 ContextLoaderListener 讲解

    1 ContextLoaderListener 继承自ContextLoader,并且实现ServletContextListener接口. 肯定得实现这个接口了,不然怎么作为Servlet的监听器呢 ...

  8. Java类载入器(一)——类载入器层次与模型

    类载入器   虚拟机设计团队把类载入阶段中的"通过一个类的全限定名来获取描写叙述此类的二进制字节流"这个动作放到Java虚拟机外部去实现.以便让应用程序自己决定怎样去获取所须要的类 ...

  9. 【DataStructure】Some useful methods about linkedList.

    /** * Method 1: Delete the input element x  * and meanwhile keep the length of array after deleted n ...

  10. 50篇经典珍藏 | Docker、Mesos、微服务、云原生技术干货

    概念篇 全方位探(tian)索(keng)Mesos各种存储处理方式 老肖有话说@Mesos User Group第四次约会 技术实践 | Mesos 全方位“烹饪”指南 回顾 JAVA 发展轨迹,看 ...