3996: [TJOI2015]线性代数

3996: [TJOI2015]线性代数

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 1221   Solved: 765
[ Submit][ Status][ Discuss]

Description

给出一个N*N的矩阵B和一个1*N的矩阵C。求出一个1*N的01矩阵A.使得

D=(A*B-C)*A^T最大。其中A^T为A的转置。输出D

Input

第一行输入一个整数N,接下来N行输入B矩阵,第i行第J个数字代表Bij.
接下来一行输入N个整数,代表矩阵C。矩阵B和矩阵C中每个数字都是不超过1000的非负整数。

Output

输出最大的D

Sample Input

3
1 2 1
3 1 0
1 2 3
2 3 7

Sample Output

2

HINT

 1<=N<=500

Source

[ Submit][ Status][ Discuss]



先暴力手算三个矩阵乘法,,列出答案的通式为∑∑ai*aj*bij - ∑ai*ci

也就是说,对于矩阵A中任意一个位置i,填1先消耗ci的代价

若同时A中还有一位置j也填1,这样就能得到bij + bji的贡献(i == j类似--不多列了)

最大权闭合子图了,建模然后网络流解决


数组开小TLE一发。。GG

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 505;
const int maxm = maxn*maxn*10;
const int INF = ~0U>>1;

struct E{
	int to,cap,flow; E(){}
	E(int to,int cap,int flow): to(to),cap(cap),flow(flow){}
}edgs[maxm];

int n,tot,s,t,cnt,ans,L[maxm],vis[maxm],cur[maxm];

queue <int> Q;
vector <int> v[maxm];

void Add(int x,int y,int cap)
{
	v[x].push_back(cnt); edgs[cnt++] = E(y,cap,0);
	v[y].push_back(cnt); edgs[cnt++] = E(x,0,0);
}

bool BFS()
{
	for (int i = 1; i <= tot; i++)
		L[i] = vis[i] = 0;
	L[s] = vis[s] = 1; Q.push(s);
	while (!Q.empty())
	{
		int k = Q.front(); Q.pop();
		for (int i = 0; i < v[k].size(); i++)
		{
			E e = edgs[v[k][i]];
			if (e.cap == e.flow) continue;
			if (vis[e.to]) continue;
			L[e.to] = L[k] + 1; vis[e.to] = 1; Q.push(e.to);
		}
	}
	return vis[t];
}

int Dinic(int x,int a)
{
	if (x == t) return a;
	int flow = 0;
	for (int &i = cur[x]; i < v[x].size(); i++)
	{
		E &e = edgs[v[x][i]];
		if (e.cap == e.flow) continue;
		if (L[e.to] != L[x] + 1) continue;
		int f = Dinic(e.to,min(a,e.cap - e.flow));
		if (!f) continue;
		flow += f;
		e.flow += f;
		edgs[v[x][i]^1].flow -= f;
		a -= f;
		if (!a) return flow;
	}
	if (!flow) L[x] = 0;
	return flow;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n; s = n + 1; 
	t = n + 2; tot = t;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			int x; scanf("%d",&x); ans += x;
			if (i == j) Add(i,t,x);
			else Add(i,++tot,INF),Add(j,tot,INF),Add(tot,t,x);
		}
	for (int i = 1; i <= n; i++)
	{
		int x; scanf("%d",&x);
		Add(s,i,x);
	}
	
	int MaxFlow = 0;
	while (BFS())
	{
		for (int i = 1; i <= tot; i++) cur[i] = 0;
		MaxFlow += Dinic(s,INF);
	}
	cout << ans - MaxFlow;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值