题目链接: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. 〖Android〗/system/etc/recovery-resource.dat

    源代码中的解释:[platform_build/tools/releasetools/ota_from_target_files] # Recovery is generated as a patch ...

  2. 通过CSS3,实现元素覆盖效果

    在非常多站点中,我们都能够看到这种效果.当用户鼠标进入某一个元素后,下方就会有遮罩层上浮动画,如图: 今天我们就用hover伪类加上css3实现,没有使用不论什么JS <!DOCTYPE htm ...

  3. Linux常用命令整理(脑图)

  4. (一)struts2入门——登陆验证熟悉struts2部署

    0.了解 Struts(支柱.支干)是什么? Struts是流行和成熟的基于MVC设计模式的Web应用程序框架.使用它能帮助我们减少在运用MVC设计模型来开发Web应用时间. 为什么2.1.3之后用S ...

  5. 【LeetCode】162. Find Peak Element (3 solutions)

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  6. winform通过网络获取用户信息

    1.获取当前部署: public static NameValueCollection GetQueryStringParameters() { NameValueCollection nameVal ...

  7. Python isdecimal() 方法

    描述 Python isdecimal() 方法检查字符串是否只包含十进制字符.这种方法只存在于unicode对象. 注意:定义一个十进制字符串,只需要在字符串前添加 'u' 前缀即可. 语法 isd ...

  8. IntelliJ IDEA 学习(一):IntelliJ IDEA15 破解方法(已验证)

    新的破解方法: 1.进到文件夹中:C:\Windows\System32\drivers\etc ,找到hosts文件,用记事本编辑 2.如果没有找到hosts文件,可在查看设置中勾选“显示隐藏的项目 ...

  9. powerdesigner 画ER图

    ER图 即为 CDM图 - Conceptual Data Modal CDM可以转换成PDM.OOM.LDM等图,具体请详查 一般,CDM图示在概要设计阶段创建,PDM图示根据CDM图的基础上产生的 ...

  10. [Objective C]super dealloc 调用时机

    转自:http://dcm19872007.blog.163.com/blog/static/86519374201311953739818/ objective-c 语言中最头疼的事就是内存释放,申 ...