注:此题无序,也无嵬

正文

我们这题求得事实上是一个最大费用最大流,最后的对每条边进行枚举,额然后,如果最大费用小了,就计入答案。。

算是,比较水吧

还有,一开始WA了两次是因为,dis应初始化为负无穷

/* make by ltao */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <string>
#include <complex>
#include <sstream>
#include <set>
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define sz 666666
#define fake int
#define re return
#define get() getchar()
#define inf 1e6
#define diss(i) dis[i]+h[i]
using namespace std;
int read(){
    int x=0;bool f=0;
    char ch=get();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=1;
        ch=get();
    }
    while(ch<='9'&&ch>='0'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=get();
    }
    return f?-x:x;
}
const int Maxn=81;
#define Maxx 2*Maxn
#define Maxm Maxn*Maxn*2
int n,cnt,s,t,maxflow,maxcost,h[Maxx],dis[Maxx],cur[Maxx],a[Maxx];
bool vis[Maxx];
struct Edge{
    int to,lac,flow,cost;
    bool can;
    void insert(int x,int y,int z,int w){
        to=y;lac=h[x];flow=z;cost=w;h[x]=cnt++;
    }
}edge[Maxm],vedge[Maxm];
void add_edge(int x,int y,int z,int w){
    vedge[cnt].insert(x,y,z,w);
    vedge[cnt].insert(y,x,0,-w);
}
int val=-1;
bool spfa(int s,int t){
    for(int i=1;i<=t;i++) dis[i]=-0x3f3f3f3f;
    memcpy(cur,h,sizeof cur);
    queue<int> q;
    dis[s]=0;vis[s]=1;q.push(s);
    while(!q.empty()){
        int fr=q.front();
        vis[fr]=0;
        q.pop();
        for(int i=h[fr];i!=-1;i=edge[i].lac){
            int to=edge[i].to;
            if(edge[i].flow>0&&!edge[i].can&&dis[to]<dis[fr]+edge[i].cost){
                dis[to]=dis[fr]+edge[i].cost;
                if(!vis[to]){
                    vis[to]=1;
                    q.push(to);
                }
            }
        }
    }
    return dis[t]!=-0x3f3f3f3f;
}
int dfs(int u,int min1){
    if(u==t) return min1;
    int sum=min1;vis[u]=1;
    for(int i=cur[u];i!=-1;i=edge[i].lac){
        int to=edge[i].to;
        if(!vis[to]&&!edge[i].can&&dis[to]==dis[u]+edge[i].cost&&edge[i].flow>0){
            cur[u]=i;
            int ret=dfs(to,min(sum,edge[i].flow));
            sum-=ret;edge[i].flow-=ret;edge[i^1].flow+=ret;
            if(sum==0) break;
        }
    }
    vis[u]=0;
    return min1-sum;
}
void Dinic(int s,int t){
    while(spfa(s,t)){
        int ret=dfs(s,0x3f3f3f3f);
        maxflow+=ret;
        maxcost+=ret*dis[t];
    }
    re ;
}
int main(){
    freopen("YJOI2016.in","r",stdin);
    n=read();
    s=0;t=n<<1|1;
    memset(h,-1,sizeof h);
    for(int i=1;i<=n;i++) add_edge(s,i,1,0);
    for(int j=n+1;j<=n<<1;j++) add_edge(j,t,1,0);
    for(int i=1;i<=n;i++) for(int j=n+1;j<=n*2;j++) add_edge(i,j,1,read());
    memcpy(edge,vedge,sizeof edge);
    Dinic(s,t);
    int cnt1=0;
    for(int i=0;i<cnt;i++){
        int to=edge[i].to,fr=edge[i^1].to;
        if(!edge[i].flow&&fr<=n&&fr>=1&&to>n&&to<=n*2)
            a[++cnt1]=i;
    }
    printf("%d\n",maxcost);
    int max1=maxcost;
    for(int i=1;i<=cnt1;i++){
        memcpy(edge,vedge,sizeof edge);
        edge[a[i]].can=1;
        edge[a[i]^1].can=1;
        maxflow=0;
        maxcost=0;
        Dinic(s,t);
        if(maxflow==n&&maxcost!=max1)
            printf("%d %d\n",edge[a[i]^1].to,edge[a[i]].to-n);
        edge[a[i]].can=0;
        edge[a[i]^1].can=0;
    }
    re 0;
}

我看到有人拿km写的,算了,我还是好好用费用流吧,,,

[TJOI2014] 匹配的更多相关文章

  1. 【刷题】BZOJ 5154 [Tjoi2014]匹配

    Description 有N个单身的男孩和N个单身女孩,男孩i和女孩j在一起得到的幸福值为Hij.一个匹配即对这N个男孩女孩的安排: 每个男孩恰好有一个女朋友,每个女孩恰好有一个男朋友.一个匹配的幸福 ...

  2. BZOJ5154 [Tjoi2014]匹配 【KM算法 + 枚举】

    题目链接 BZOJ5154 题解 先跑出一个匹配方案 然后暴力删去每对匹配再检验一下答案是否减小 使用KM算法提升速度 #include<algorithm> #include<io ...

  3. javascript匹配各种括号书写是否正确

    今天在codewars上做了一道题,如下 看上去就是验证三种括号各种嵌套是否正确书写,本来一头雾水,一种括号很容易判断, 但是三种怎么判断! 本人只是个前端菜鸟,,不会什么高深的正则之类的. 于是,在 ...

  4. scanf类型不匹配造成死循环

        int i = 0; while (flag) { printf("please input a number >>> "); scanf("% ...

  5. 使用注解匹配Spring Aop切点表达式

    Spring中的类基本都会标注解,所以使用注解匹配切点可以满足绝大部分需求 主要使用@within()/@target @annotaton() @args()等... 匹配@Service类中的所有 ...

  6. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

  7. webpack配置别名alias出现的错误匹配

    @(webpack) webpack是一款功能强大的前端构建工具,不仅仅是针对js,它也可通过各种loader来构建相关的less,html,image等各种资源,将webpack配合流程制定工具gu ...

  8. perl 如何匹配ASCII码以及ASCII码转换

    匹配ASCII码:   /[:ascii:]/ ASCII码转换为数字: ord() 数字转换为ASCII码: chr()

  9. SQL连接操作符介绍(循环嵌套, 哈希匹配和合并连接)

    今天我将介绍在SQLServer 中的三种连接操作符类型,分别是:循环嵌套.哈希匹配和合并连接.主要对这三种连接的不同.复杂度用范例的形式一一介绍. 本文中使用了示例数据库AdventureWorks ...

随机推荐

  1. python学习Day7--字符串操作

    [主要内容] 1. 补充基础数据类型的相关知识点 1. str. join() 把列表变成字符串 2. 列表不能再循环的时候删除. 因为索引会跟着改变 3. 字典也不能直接循环删除. 把要删除的内容记 ...

  2. 《Python学习手册 第五版》 -第5章 数值类型

    本章是承接第四章整体说明之后,将对”数值类型“展开详细的说明 数值类型这一章主要通过一下几个内容来讲解: 1.数值类型有哪些? 2.表达式运算符:有哪些?有什么规范? 3.数值的显示格式 接下来,从第 ...

  3. 利用Kubernetes中的leaderelection实现组件高可用

    在Kubernetes中,通常kube-schduler和kube-controller-manager都是多副本进行部署的来保证高可用,而真正在工作的实例其实只有一个.这里就利用到 leaderel ...

  4. 《数据结构与算法分析-Java语言描述》 分享下载

    书籍信息 书名:<数据结构与算法分析-Java语言描述> 原作名:Data Structures and Algorithm Analysis in Java 作者: 韦斯 (Mark A ...

  5. USBWebServer - 在U盘里搭一个Web服务器!

    文章选自我的博客:https://blog.ljyngup.com/archives/321.html/ 本文将介绍一款可以在U盘内直接搭建Web服务器的软件 软件可以免安装直接在U盘内运行,适合外出 ...

  6. 《算法导论》第二章demo代码实现(Java版)

    <算法导论>第二章demo代码实现(Java版) 前言 表示晚上心里有些不宁静,所以就写一篇博客,来缓缓.囧 拜读<算法导论>这样的神作,当然要做一些练习啦.除了练习题与思考题 ...

  7. Bookshelf 2 01背包

    B - Bookshelf 2 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submi ...

  8. redis系列-要命的zrangebyscore

    0x0 引子 无论做哪种业务都躲不开排行功能.Redis 的 Sorted Sets 结构就是为排行而生的.它简单易用,效率奇高.同时它也有坑,你真的了解它吗? 老规矩,先讲故事,后科普.这里是 So ...

  9. 关于simplememory theme的设置和感想

    前言 首先,这是我第一次自己个性化博客的主题.如果下文所写如有不妥之处还望大佬指出 参考 这次设置多亏了GitHub上的开源代码:https://github.com/BNDong/Cnblogs-T ...

  10. 多线程笔记 - disruptor

    disruptor 可以理解为一个生产消费的框架. 具体翻译教程: http://ifeve.com/disruptor-getting-started/ 这个框架从数据上看, 是很强大的. 号称1s ...