题目链接:http://lightoj.com/volume_showproblem.php?problem=1198

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std; const int maxn = ;
const int maxe = ;
const int INF = 0x3f3f3f; struct Edge{
int u,v,flow,cap,cost;
int next;
Edge(int u=,int v=,int flow=,int cap=,int cost=,int next=):
u(u), v(v), flow(flow), cap(cap), cost(cost), next(next) {}
}; struct MCMF{
Edge edges[maxe];
int head[maxn],cnt;
int d[maxn];
bool inq[maxn];
int pa[maxn];
int res[maxn]; void init(){
memset(head,-,sizeof(head));
cnt = ;
} void addedge(int u,int v,int cap,int cost){
edges[cnt] = Edge(u,v,,cap,cost,head[u]);
head[u] = cnt++;
edges[cnt] = Edge(v,u,,,-cost,head[v]);
head[v] = cnt++;
} bool SPFA(int s,int t,int& flow,int& cost){
memset(inq,,sizeof(inq));
memset(d,-0x3f,sizeof(d));
queue<int> Q;
Q.push(s); inq[s] = true; d[s] = ; pa[s] = s;
res[s] = INF; res[t] = ; while(!Q.empty()){
int u = Q.front(); Q.pop();
inq[u] = false;
for(int i=head[u];i!=-;i=edges[i].next){
Edge& e = edges[i];
if(e.cap>e.flow && d[e.v] < d[u] + e.cost){
d[e.v] = d[u] + e.cost;
res[e.v] = min(res[u],e.cap-e.flow);
pa[e.v] = i;
if(!inq[e.v]){
inq[e.v] = true;
Q.push(e.v);
}
}
}
}
if(!res[t]) return false;
flow += res[t];
cost += res[t]*d[t];
for(int i=t;i!=s;i=edges[pa[i]].u){
edges[pa[i]].flow += res[t];
edges[pa[i]^].flow -= res[t];
}
return true;
} int MaxCost(int s,int t){
int flow = , cost = ; while(SPFA(s,t,flow,cost)); printf("%d\n",cost);
}
}solver; int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
for(int cas=;cas<=T;cas++){
solver.init();
int N;
cin>>N;
int A[maxn],B[maxn];
int s = , t = *N+;
for(int i=;i<=N;i++) scanf("%d",&A[i]),solver.addedge(s,i,,);
for(int i=;i<=N;i++) scanf("%d",&B[i]),solver.addedge(i+N,t,,); for(int i=;i<=N;i++)
for(int j=;j<=N;j++){
if(A[i] > B[j]) solver.addedge(i,j+N,,);
else if(A[i] == B[j]) solver.addedge(i,j+N,,);
else solver.addedge(i,j+N,,);
}
printf("Case %d: ",cas);
solver.MaxCost(s,t);
}
}

lightoj 1198 最大权重匹配的更多相关文章

  1. lightoj 1011 最大权重匹配或最大费用流

    由于暂时不会KM算法,只能用最大费用流来做了. 题目链接:http://lightoj.com/volume_showproblem.php?problem=1011 #include <cst ...

  2. 浏览器+css基础+选择器+权重+匹配规则

    浏览器的组成: shell+内核 shell:用户能看得到的界面就叫shell 内核:渲染rendering引擎和js引擎 现在主流拥有自己开发内核的浏览器:opera现在属于360和昆仑万维 CSS ...

  3. postgresql全文检索语法

    第1章    全文检索语法 1.1 概述 查询引擎为文本数据类型提供~, ~*, LIKE和ILIKE操作符,并提供全文检索以识别自然语言文档,并通过相关性查询进行排序.查询引擎提供两种数据类型用于支 ...

  4. 基于 libmemcahce 的memcache 操作

    <?php echo '<pre>'; //测试的键值的数量 $count = 30; $mem = create_memcache(); //var_dump($mem->i ...

  5. Sphinx速成指南

    目录 1. Sphinx简介 1.1. 什么是全文检索 1.2. 介绍 1.3. Sphinx的特性 2. Sphinx安装(For MySQL) 2.1. Windows下安装 2.2. Linux ...

  6. spring cloud微服务实践七

    在spring cloud 2.x以后,由于zuul一直停滞在1.x版本,所以spring官方就自己开发了一个项目 Spring Cloud Gateway.作为spring cloud微服务的网关组 ...

  7. Spring Cloud Gateway简单入门,强大的微服务网关

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 见名知义,Spring Cloud Gateway是用于微服务场景的网关组件,它是基于Spring WebFlu ...

  8. pytorch中网络特征图(feture map)、卷积核权重、卷积核最匹配样本、类别激活图(Class Activation Map/CAM)、网络结构的可视化方法

    目录 0,可视化的重要性: 1,特征图(feture map) 2,卷积核权重 3,卷积核最匹配样本 4,类别激活图(Class Activation Map/CAM) 5,网络结构的可视化 0,可视 ...

  9. LightOJ - 1356 Prime Independence (数论+二分图匹配)

    题意:有N个数的集合,其中选出若干个数组成一个子集,要求这个子集中的任意两个数a,b都不能通过a=k*b得到,其中k是一个素数.求这个子集最大的size. 分析:集合中任意两数的关系是二者之间是否之差 ...

随机推荐

  1. java web(jsp)-The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    在静态项目上新建 jsp文件的时候,报错:The superclass "javax.servlet.http.HttpServlet" was not found on the ...

  2. asp.net中ashx文件如何调用session

    如果你要保证数据的安全性,你可以在ashx中使用session验证.如:你的index.aspx中使用jquery回调ashx数据,那么在index.aspx page_load时session[&q ...

  3. POJ 2112.Optimal Milking (最大流)

    时间限制:2s 空间限制:30M 题意: 有K台挤奶机(编号1~K),C头奶牛(编号K+1~K+C),给出各点之间距离.现在要让C头奶牛到挤奶机去挤奶,每台挤奶机只能处理M头奶牛,求使所走路程最远的奶 ...

  4. linux如何开机以命令行形式启动?

    在管理员权限下,修改/etc/inittab文件即可.把id:5:initdefault:改为id:3:initdefault:就可以了. 如下图所示: 图1: . 图2:

  5. Excel等外部程序点击链接会带上IE信息的bug

    今天碰到一个问题,在Excel内点击链接到默认浏览器Chrome打开,奇怪的是服务端收到的Session一直对不上. 查了很久发现这个Excel到Chrome的跳转竟然带上了IE的Cookie 和 U ...

  6. Android SlidingMenu开源库及其使用

    极客学院教程: http://www.jikexueyuan.com/course/61_5.html?ss=1 1. SlidingMenu开源库的配置 2. SlidingMenu 的使用 --- ...

  7. day11基础代码——函数指针

    // //  main.m //  Demo11 // //  Created by scjy on 15/10/29. //  Copyright © 2015年 lizhipeng. All ri ...

  8. bzoj3639: Query on a tree VII

    Description You are given a tree (an acyclic undirected connected graph) with n nodes. The tree node ...

  9. Python模块解析之SocketServer(三)——模块思想

            SocketServer 体系 由两个部分构成 BaseServer 和 BaseRequestHandler.思想很简单 BaseServer接受请求,将请求交给BaseReques ...

  10. Gray码 (格雷码) 【二进制】

    以下内容是看了Matrix67的关于二进制的blog(Link)的一点总结与摘录. Gray码,中文“格雷码”,是一种特殊的编码,相邻两个格雷码的二进制表示中有且仅有一位不同,且 n 阶 Gray 码 ...