TCO 2015 Round 1A DIV 1 1000

Problem Statement

 

You have a weighted bipartite graph. Each partition contains n vertices numbered 0 through n-1. You are given the weights of all edges encoded into a vector <string>A with n elements, each containing n characters. For each i and j,A[i][j] is '0' if there is no edge between vertex i in the first partition and vertex j in the second partition. Otherwise,A[i][j] is between '1' and '9', inclusive, and the digit represents the weight of the corresponding edge.

A perfect matching is a permutation p of 0 through n-1 such that for each i there is an edge (of any positive weight) between vertex i in the first partition and vertex p[i] in the second partition.

Your goal is to have a graph that does not contain any perfect matching. You are allowed to delete edges from your current graph. You do not care about the number of edges you delete, only about their weights. More precisely, you want to reach your goal by deleting a subset of edges with the smallest possible total weight. Compute and return the total weight of deleted edges in an optimal solution.

Definition

 
Class:Revmatching
Method:smallest
Parameters:vector <string>
Returns:int
Method signature:int smallest(vector <string> A)
(be sure your method is public)

Limits

 
Time limit (s):2.000
Memory limit (MB):256
Stack limit (MB):256

Constraints

-

A will contain exactly n elements.

-

Each element in A will be n characters long.

-

n will be between 1 and 20, inclusive.

-

Each character in A will be between '0' and '9', inclusive.

Examples

0) 
 
{"1"}
Returns: 1
There is a single edge. You have to delete it.
1) 
 
{"0"}
Returns: 0
There are no edges and therefore there is no perfect matching.
2) 
 
{"44","44"}
Returns: 8
3) 
 
{"861","870","245"}
Returns: 6
4) 
 
{"01000","30200","11102","10001","11001"}
Returns: 0
5) 
 
{"0111101100","0001101001","1001001000","1000100001","0110011111","0011110100","1000001100","0001100000","1000100001","0101110010"}
Returns: 1


挺烧脑洞的一题,看了别人的代码才明白。。。

对于一个已有的二部图,删除任意边(总边权和最小)使其无法二分匹配

由于节点较少(最多20),可以枚举点集S,假设最终找不到匹配点的节点就在S中,与S中所有点相关联的点集为A。只要通过删除边使得|A|<|S|就能达到目的。

#include <cstdio>
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
#include <array>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define FOR(i,a,b) for(int i(a);i<(b);++i)
#define FORD(i,a,b) for(int i(a);i<=(b);++i)
#define REP(i,a) for(int i(0);i<(a);++i)
const int MAXN = 25;
typedef long long LL;
class Revmatching
{
public:
    int smallest(vector <string> A);
};
int N, mp[MAXN][MAXN];
int getcntone(int x)
{
    int res = 0;
    while(x)
    {
        x &= x-1;
        ++res;
    }
    return res;
}
int Revmatching::smallest(vector <string> A)
{
    int res = 0x3f3f3f3f;
    N = A.size();
    vector<int> sm(N);
    FOR(i,0,N)FOR(j,0,N) mp[i][j]=A[i][j]-'0';
    FOR(flg, 1, 1<<N)
    {
        int cnt = getcntone(flg);
        fill(sm.begin(), sm.end(), 0);
        FOR(j,0,N) if(flg&(1<<j)) FOR(i,0,N) sm[i] += mp[j][i];
        sort(sm.begin(), sm.end());
        int sum = 0;
        FOR(i,0,N-cnt+1) sum += sm[i];
        res = min(res, sum);
    }
    return res;
}
int main()
{
    Revmatching test;
    cout<<test.smallest(vector<string>{"861","870","245"})<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值