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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
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) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
|||||||||||||
5) | |||||||||||||
|
挺烧脑洞的一题,看了别人的代码才明白。。。
对于一个已有的二部图,删除任意边(总边权和最小)使其无法二分匹配
由于节点较少(最多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; }