UVA 10594-Date Flow(无向图的最小费用网络流+题目给的数据有误)

题意:给一个有N个点的无向图,要求从1向N传送一定的数据,每条边的容量是一定的,如果能做到,输出最小的费用,否则输出Impossible.

解析:由于是无向图,所以每个有连接的两个点要建4条边,分别是edge(from,to,cap,0,cost),edge(to,from,0,0,-cost),edge(to,from,cap,0,cost),edge(from,to,0,0,-cost)

设置一个起点0,0与1连一条有向边,容量为题目给出的D,这样限制了最大的流量,如果最后的流量不等于D,则是Impossible.否则输出最小费。

 

代码如下:

#include<cstdio>
#include<cstring>
#include<string> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #include<iterator> #include<utility> #include<sstream> #include<iostream> #include<cmath> #include<stack> using namespace std; const double eps=0.00000001; typedef long long LL; const LL INF=1ll<<60; const int maxn=105; const int maxm=5005; const int skip=10010; int N,M; LL D,K; int u[maxm],v[maxm]; LL waste[maxm]; struct edge { int from,to; LL cap; LL flow,cost; edge(int from=0,int to=0,LL cap=0,LL flow=0,LL cost=0) :from(from),to(to),cap(cap),flow(flow),cost(cost){} }save[4*maxm]; int edge_num; vector<int> G[maxn]; void addedge(int from,int to,LL cap,LL cost) { int a=edge_num++; // 增加4条边,编号分别是a,b,sa,sb,skip的目的是为了把一条无向边分隔开,
int b=edge_num++;
// 因为如果其中一条有向边的flow改变了,另一条边也要改变
    int sa=a+skip;
    int sb=b+skip; save[a]=edge(from,to,cap,0,cost); save[b]=edge(to,from,0,0,-cost); save[sa]=edge(to,from,cap,0,cost); save[sb]=edge(from,to,0,0,-cost); G[from].push_back(a); G[from].push_back(sb); // 建立临接表 G[to].push_back(b); G[to].push_back(sa); } LL add[maxn],C[maxn]; // add[]是增加的流量 int fa[maxn]; // 保存父亲边编号 bool inq[maxn]; // 入队标记 inline void init(int be) // 初始化 { memset(add,0,sizeof(add)); memset(inq,false,sizeof(inq)); add[be]=INF; fa[be]=0; inq[be]=true; for(int i=0;i<=N;i++) C[i]=INF; C[be]=0; } void MFMC(int be,int en) { LL cnt=0; LL ret=0; // cnt是最大流量,ret是最小费用 while(true) { init(be); queue<int> que; que.push(be); while(!que.empty()) { int from=que.front(); que.pop(); inq[from]=false; // spfa跑最小费,入队标记改为false for(int i=0;i<G[from].size();i++) { int edge_id=G[from][i]; edge& e=save[edge_id]; int to=e.to; LL cap=e.cap,flow=e.flow,cost=e.cost; if(cap>flow&&C[to]>C[from]+cost) //更新 { C[to]=C[from]+cost; fa[to]=edge_id; // 保存编号 add[to]=min

转载于:https://www.cnblogs.com/wust-ouyangli/p/4741668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值