★★☆   输入文件:maxflowd.in   输出文件:maxflowd.out   简单对比
时间限制:1 s   内存限制:128 MB

【问题描述】
    一个工厂每天生产若干商品,需运输到销售部门进行销售。从产地到销地要经过某些城镇,有不同的路线可以行走,每条两城镇间的公路都有一定的流量限制。公路设有收费站,每通过一辆车,要交纳过路费。请你计算,在不考虑其它车辆使用公路的前提下,如何使产地运输到销地的商品最多,并使费用最少。
【输入格式】
输入文件有若干行
第一行,一个整数n,表示共有n个城市(2<=n<=100),产地是1号城市,销地是n号城市
第二行,一个整数,表示起点城市
第三行,一个整数,表示终点城市
下面有n行,每行有2n个数字。第p行第2q−1,2q列的数字表示城镇p与城镇q之间有无公路连接。数字为0表示无,大于0表示有公路,且这两个数字分别表示该公路流量和每车费用。
【输出格式】
输出文件有一行
第一行,1个整数n,表示最小费用为n。
【输入输出样例】
输入文件名: maxflowd.in
6
1
6
0 0 1 3 5 10 0 0 0 0 0 0 
0 0 0 0 0 0 5 7 0 0 0 0
0 0 0 0 0 0 0 0 2 8 0 0
0 0 0 0 1 3 0 0 0 0 3 5
0 0 2 4 0 0 0 0 0 0 2 6
0 0 0 0 0 0 0 0 0 0 0 0
输出文件名:maxflowd.out
63
 
 
裸费用流
#include <cstdio>
#include <queue>
#define inf 0x7fffffff
#define N 105 using namespace std;
bool vis[N];
int n,s,t,fa[N],dis[N],flow[N],cnt=,head[N];
struct Edge
{
int next,to,flow,cost;
Edge (int next=,int to=,int flow=,int cost=) : next(next),to(to),flow(flow),cost(cost) {}
}edge[N*N];
inline void ins(int u,int v,int w,int l)
{
edge[++cnt]=Edge(head[u],v,w,l);
head[u]=cnt;
}
bool spfa(int s,int t)
{
for(int i=s;i<=t;++i) vis[i]=,flow[i]=inf,dis[i]=inf;
dis[s]=;
fa[s]=;
queue<int>q;
q.push(s);
for(int now;!q.empty();)
{
now=q.front();
q.pop();
vis[now]=;
for(int i=head[now];i;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[now]+edge[i].cost&&edge[i].flow)
{
dis[v]=dis[now]+edge[i].cost;
flow[v]=min(flow[now],edge[i].flow);
fa[v]=i;
if(!vis[v])
{
q.push(v);
vis[v]=;
}
}
}
}
return dis[t]!=inf;
}
int dinic(int s,int t)
{
int ans=;
for(;spfa(s,t);)
{
int x=flow[t];
for(int i=t;i!=s&&i;i=edge[fa[i]^].to)
{
edge[fa[i]].flow-=x;
edge[fa[i]^].flow+=x;
}
ans+=dis[t]*x;
}
return ans;
}
int main()
{
freopen("maxflowd.in","r",stdin);freopen("maxflowd.out","w",stdout);
scanf("%d%d%d",&n,&s,&t);
for(int i=;i<=n;++i)
for(int a,b,j=;j<=n;++j)
{
scanf("%d%d",&a,&b);
if(a&&b)
{
ins(i,j,a,b);
ins(j,i,,-b);
}
}
printf("%d\n",dinic(s,t));
return ;
}

最新文章

  1. codevs 2924 数独挑战
  2. Nginx的nginx.conf配置文件中文注释说明
  3. MYSQL #1064错误
  4. 2016HUAS_ACM暑假集训2L - Points on Cycle(圆上的点)
  5. Android 正则表达式
  6. Html A标签中 href 和 onclick 同时使用的问题 优先级别
  7. 怎样在一个页面使多个setInterval函数正常执行
  8. nginx https 配置
  9. stm32l053r8 nucelo板的串口实验
  10. linux下连接windows的远程桌面
  11. Access Logging Tomcat
  12. springBoot和Mybatis输出sql日志
  13. .Net Framework 4.x 程序到底运行在哪个 CLR 版本之上(ZT)
  14. C# WebApi 获取客户端ip地址
  15. 5.Django|模型层--多表关系
  16. eclipse中启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误
  17. 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter
  18. 【 js 性能优化】【源码学习】underscore throttle 与 debounce 节流
  19. lua-resty-gearman模块
  20. js数组遍历some、foreach、map、filter、every、lastIndexOf、indexOf对比

热门文章

  1. JavaScript代码放在HTML代码不同位置的差别
  2. UVa 1335 Beijing Guards (二分+贪心)
  3. POJ - 2955 Brackets括号匹配(区间dp)
  4. HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)
  5. js 实现发布订阅模式
  6. css需要注意的地方
  7. Codevs 1444 “破锣摇滚”乐队
  8. [Xcode 实际操作]六、媒体与动画-(5)使用CoreImage框架给图片添加马赛克效果
  9. map练习小例题
  10. idea 添加yuicompressor压缩js/css