ZOJ3362 Beer Problem(最小费用任意流)

题目大概说有n个城市,由m条无向边相连,每条边每天最多运送cap桶酒且其运送一桶的花费是cost。现在从1号城市开始出发运酒,供应到2到n号城市,这些城市的收购单价是price,问最大的盈利是多少。

。。。顺路AC

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<algorithm>
 5 using namespace std;
 6 #define INF (1<<30)
 7 #define MAXN 111
 8 #define MAXM 111*222
 9 struct Edge{
10     int u,v,cap,cost,next;
11 }edge[MAXM];
12 int head[MAXN];
13 int NV,NE,vs,vt;
14 
15 void addEdge(int u,int v,int cap,int cost){
16     edge[NE].u=u; edge[NE].v=v; edge[NE].cap=cap; edge[NE].cost=cost;
17     edge[NE].next=head[u]; head[u]=NE++;
18     edge[NE].u=v; edge[NE].v=u; edge[NE].cap=0; edge[NE].cost=-cost;
19     edge[NE].next=head[v]; head[v]=NE++;
20 }
21 bool vis[MAXN];
22 int d[MAXN],pre[MAXN];
23 bool SPFA(){
24     for(int i=0;i<NV;++i){
25         vis[i]=0;
26         d[i]=INF;
27     }
28     vis[vs]=1;
29     d[vs]=0;
30     queue<int> que;
31     que.push(vs);
32     while(!que.empty()){
33         int u=que.front(); que.pop();
34         for(int i=head[u]; i!=-1; i=edge[i].next){
35             int v=edge[i].v;
36             if(edge[i].cap && d[v]>d[u]+edge[i].cost){
37                 d[v]=d[u]+edge[i].cost;
38                 pre[v]=i;
39                 if(!vis[v]){
40                     vis[v]=1;
41                     que.push(v);
42                 }
43             }
44         }
45         vis[u]=0;
46     }
47     return d[vt]!=INF;
48 }
49 int MCMF(){
50     int res=0;
51     while(SPFA()){
52         int flow=INF,cost=0;
53         for(int u=vt; u!=vs; u=edge[pre[u]].u){
54             flow=min(flow,edge[pre[u]].cap);
55         }
56         for(int u=vt; u!=vs; u=edge[pre[u]].u){
57             edge[pre[u]].cap-=flow;
58             edge[pre[u]^1].cap+=flow;
59             cost+=flow*edge[pre[u]].cost;
60         }
61         if(cost>=0) break;
62         res+=cost;
63     }
64     return res;
65 }
66 int main(){
67     int n,m,a,b,c,d;
68     while(~scanf("%d%d",&n,&m)){
69         vs=1; vt=n+1; NV=vt+1; NE=0;
70         memset(head,-1,sizeof(head));
71         for(int i=2; i<=n; ++i){
72             scanf("%d",&a);
73             addEdge(i,vt,INF,-a);
74         }
75         while(m--){
76             scanf("%d%d%d%d",&a,&b,&c,&d);
77             addEdge(a,b,c,d);
78             addEdge(b,a,c,d);
79         }
80         printf("%d\n",-MCMF());
81     }
82     return 0;
83 }

 

posted @ 2016-04-08 18:27  WABoss  阅读(330)  评论(0编辑  收藏  举报