职务地址:

pid=4862">HDU4862

最小费用流做的还是太少。

建图想不出来。

直接引用官方题解的话吧。。。

最小K路径覆盖的模型。用费用流或者KM算法解决,构造二部图,X部有N*M个节点。源点向X部每一个节点连一条边,流量1,费用0,Y部有N*M个节点,每一个节点向汇点连一条边。流量1,费用0。假设X部的节点x能够在一步之内到达Y部的节点y,那么就连边x->y,费用为从x格子到y格子的花费能量减去得到的能量。流量1,再在X部添加一个新的节点,表示能够从随意节点出发K次,源点向其连边,费用0。流量K。这个点向Y部每一个点连边,费用0,流量1,最这个图跑最小费用最大流,假设满流就是存在解。反之不存在,最小费用的相反数就是能够获得的最大能量

代码例如以下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int head[500], source, sink, cnt, flow, cost;
int cur[500], d[500], vis[500], mp[20][20];
struct node
{
int u, v, cap, cost, next;
} edge[1000000];
void add(int u, int v, int cap, int cost)
{
edge[cnt].v=v;
edge[cnt].cap=cap;
edge[cnt].cost=cost;
edge[cnt].next=head[u];
head[u]=cnt++; edge[cnt].v=u;
edge[cnt].cap=0;
edge[cnt].cost=-cost;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int spfa()
{
memset(d,INF,sizeof(d));
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(source);
d[source]=0;
cur[source]=-1;
int minflow=INF, i;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(d[v]>d[u]+edge[i].cost&&edge[i].cap)
{
d[v]=d[u]+edge[i].cost;
minflow=min(minflow,edge[i].cap);
cur[v]=i;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(d[sink]==INF) return 0;
flow+=minflow;
cost-=minflow*d[sink];
for(i=cur[sink]; i!=-1; i=cur[edge[i^1].v])
{
edge[i].cap-=minflow;
edge[i^1].cap+=minflow;
}
return 1;
}
void mcmf(int sum)
{
while(spfa());
if(flow==sum)
printf("%d\n",cost);
else
printf("-1\n");
}
int main()
{
int t, n, m, i, j, k, h, num=0;
char s[30];
scanf("%d",&t);
while(t--)
{
num++;
scanf("%d%d%d",&n,&m,&k);
for(i=0; i<n; i++)
{
scanf("%s",s);
for(j=0; j<m; j++)
{
mp[i][j]=s[j]-'0';
}
}
source=0;
sink=2*n*m+2;
cnt=0;
memset(head,-1,sizeof(head));
flow=0;
cost=0;
for(i=1; i<=n*m; i++)
{
add(source,i,1,0);
add(i+n*m+1,sink,1,0);
add(n*m+1,i+n*m+1,1,0);
}
add(source,n*m+1,k,0);
int z;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
for(h=j+1; h<m; h++)
{
z=0;
if(mp[i][j]==mp[i][h])
z=mp[i][j];
add(i*m+j+1,n*m+1+i*m+h+1,1,h-j-1-z);
}
for(h=i+1; h<n; h++)
{
int z=0;
if(mp[i][j]==mp[h][j])
z=mp[i][j];
add(i*m+j+1,n*m+1+h*m+j+1,1,h-i-1-z);
}
}
}
printf("Case %d : ",num);
mcmf(n*m);
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

HDU 4862 Jump(更多的联合培训学校1)(最小费用最大流)的更多相关文章

  1. HDU 4864Task(更多的联合培训学校1)(贪婪)

    职务地址:pid=4864">HDU4864 这题又是一上来觉得是最小费用流,可是边太多.果然,敲完交上去后不断TLE.. 小优化了两次也没过. . . sad.. 后来看了题解才发现 ...

  2. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

  3. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  4. hdu 4494 Teamwork 最小费用最大流

    Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...

  5. HDU 5988.Coding Contest 最小费用最大流

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. hdu 3667(拆边+最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...

  7. hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  9. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

随机推荐

  1. [PReact] Reduce the Size of a React App in Two Lines with preact-compat

    Not every app is greenfield, and it would be a shame if existing React apps could not benefit from t ...

  2. [Angular2 Animation] Control Undefined Angular 2 States with void State

    Each trigger starts with an “undefined” state or a “void” state which doesn’t match any of your curr ...

  3. ios 获取button所在的cell对象, 注意:ios7 =&lt; System Version &lt; ios8 获取cell对象的差别

    ios7 =< System Version< ios8 : ios7 =< System Version < ios8  下 button.superview.supervi ...

  4. swift学习:自定义Log

    import UIKit /* 总结:1:let file = (#file as NSString).lastPathComponent,#file获取的是打印所在的文件 的全路径,转成NSStri ...

  5. php 微信支付企业付款

    1.所需参数 字段名 变量名 必填 示例值 类型 描述 公众账号appid mch_appid 是 wx8888888888888888 String 公众号的appId 商户号 mchid 是 19 ...

  6. 使用 Google Guava 美化你的 Java 代码:1~4 【转】

    文章转载自:http://my.oschina.net/leejun2005/blog/172328 1.使用Google Collections,Guava,static imports编写漂亮代码 ...

  7. Redis主从高可用缓存

    nopCommerce 3.9 大波浪系列 之 使用Redis主从高可用缓存   一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集 ...

  8. ITFriend创业阶段的服务器环境搭建手册

    创业阶段,进一步实践了Linux环境搭建和维护,下面是一些常用软件的搭建步骤和参考资料,仅供自己和诸位参考. 我个人还是比较倾向"一站式Web开发"的,自己想做点事,需要太多的技能 ...

  9. 【34.88%】【codeforces 569C】Primes or Palindromes?

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  10. Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别

    http://www.lai18.com/content/9755931.html Spring 容器(Spring 的上下文) https://my.oschina.net/jast90/blog/ ...