[BZOJ 3996][TJOI2015]線性代數

被某神犇吐槽:

你自己寫的題解太少了!

跪。。蒟蒻怎麼能想出來辣麼神的題目OTZ


但還是要認認(len)真真寫題解了=-=。。


話說這道題的題面好不着調=-=



神犇告訴我:

1 2 3

4 5 6

7 8 9

的轉置是

1 4 7

2 5 8

3 6 9


= =然並卵


呢。蘭後就這樣啦

求一個D=【A[1*N的矩陣] * B[N*N的矩陣] - C[1*N的矩陣]】 * [A的轉置N*1的矩陣]最大化

注意到D是一個數值

然後我們發現

D = x1(x1 * a1,1 + x2 * a1,2 + x3 * a1,3...  - c1) + x2(x1 * a2,1 + x2 * a2,2 + x3 * a2,3 .. -c2) + x3...



是不是很神奇!!(有什麼神奇的)


然後變一變

注意到x1,x2,x3...的取值爲0或1

D = x1 * x1 * a1,1 + x1 * x2 * a1,2 + x1 * x3 * a1,3... + x2 * x1 * a2,1 + x2 * x2 * a2,2 + x2 *x3 * a2,3 .. + x3...

      - x1 * c1 - x2 * c2


然後就轉化成了!!

選了某個數xi要花費ci的代價,然後選了xi和xj會有ai, j的貢獻!!


最小割即可


#include 
#define maxn 510
using namespace std;
const int inf = 0x7fffffff;
queueQ;
int S, T, a[maxn][maxn], n;
struct Edge{
	int to, next, w;
}edge[5000010];
int h[100010], cnt = 1;
void add(int u, int v, int w){
	cnt ++;
	edge[cnt].to = v;
	edge[cnt].next = h[u];
	edge[cnt].w = w;
	h[u] = cnt;
	swap(u, v), w = 0;
	cnt ++;
	edge[cnt].to = v;
	edge[cnt].next = h[u];
	edge[cnt].w = w;
	h[u] = cnt;
}

int d[maxn];
bool BFS(){
	memset(d, -1, sizeof d);
	Q.push(S);d[S] = 0;
	while(!Q.empty()){
		int u = Q.front();Q.pop();
		for(int i = h[u]; i; i = edge[i].next){
			if(edge[i].w == 0)continue;
			int v = edge[i].to;
			if(d[v] == -1)d[v] = d[u] + 1, Q.push(v);
		}
	}
	return d[T] != -1;
}

int DFS(int x, int a){
	if(x == T || a == 0)return a;
	int used = 0, f;
	for(int i = h[x]; i; i = edge[i].next){
		int v = edge[i].to;
		if(d[v] == d[x] + 1){
			f = DFS(v, min(a-used, edge[i].w));
			edge[i].w -= f;
			edge[i^1].w += f;
			used += f;
			if(used == a)return used;
		}
	}
	if(!used)d[x] = -1;
	return used;
}


int Dinic(){
	int ret = 0;
	while(BFS())
	    ret += DFS(S, inf);
	return ret;
}


int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= n; j ++)
			scanf("%d", &a[i][j]);

	S = 0, T = n+1;
	int total = 0;
	for(int i = 1; i <= n; i ++){
		for(int j = 1; j <= n; j ++){
			total += a[i][j];
			add(S, i, 2 * a[i][j]);
   			add(i, j, a[i][j]);
   			add(j, i, a[i][j]);
		}
	}
	
	int x = 0;
	for(int i = 1; i <= n; i ++){
		scanf("%d", &x);
		add(i, T, 2 * x);
	}
	printf("%d\n", total - Dinic() / 2);
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章