uvalive2531一种分配模型(训练指南)p369

#include<iostream>//没什么好说的按照刘汝佳的书写就好了,挺简单的。。
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int n,a[30][30];
struct edgee
{
	int to, cap;
};
edgee edge[8000];
int first[2000], nextt[8000],d[2000],change[2000];
int edgetot;
int win[100], defeat[100];
vector<int>team;
void addedge(int from, int to, int cap)
{
	edge[edgetot].to = to;
	edge[edgetot].cap = cap;
	nextt[edgetot] = first[from];
	first[from] = edgetot;
	edgetot++;
	edge[edgetot].to = from;
	edge[edgetot].cap = 0;
	nextt[edgetot] = first[to];
	first[to] = edgetot;
	edgetot++;
}
bool bfs(int s,int t)
{
	for (int i = 0; i <= n + n*n + 2; i++)d[i] = -1;
	d[s] = 0;
	queue<int>que;
	que.push(s);
	while (!que.empty())
	{
		int temp = que.front();
		if (temp == t)return true;
		que.pop();
		for (int i = first[temp]; i != -1; i = nextt[i])
		{
			int to = edge[i].to;
			if (d[to] == -1 && edge[i].cap)
			{
				d[to] = d[temp] + 1;
				que.push(to);
			}
		}
	}
	return false;
}
int dfs(int x, int t, int flow)
{
	if (x == t || flow == 0)
		return flow;
	int allflow = 0; 
	for (int &i = change[x]; i != -1; i = nextt[i])
	{
		int to = edge[i].to;
		if (d[to] == d[x] + 1 && edge[i].cap)
		{
			int tempflow = dfs(to, t, min(flow, edge[i].cap));
			flow -= tempflow; allflow += tempflow;
			edge[i].cap -= tempflow; edge[i ^ 1].cap += tempflow;
			if (flow == 0)break;
		}
	}
	if (allflow == 0)d[x] = -1;
	return allflow;
}
int dicnic(int s, int t, int fini)//这题对流没有限制,求出最大流就好
{
	int allflow = 0;
	while (bfs(s, t))
	{
		for (int i = 0; i <= n*n + n + 1; i++)change[i] = first[i];
		allflow += dfs(s, t, 1000000000);
	}
	return allflow;
}
bool control(int num, int ww)
{
	int wnum = ww;
	for (int i = 1; i <= n; i++)
	{
		wnum += a[num][i];
	}
	edgetot = 0;
	int diantot = n; int sum = 0;
	for (int i = 0; i <= n*n + n + 1; i++)first[i] = -1;
	for (int i = 1; i <= n; i++)
	{
		if (i == num)continue;
		if (wnum - win[i] < 0)return false;
		addedge(i, n + n*n + 1, wnum - win[i]);
	}
	for (int i = 1; i <= n; i++)
	{
		if (i == num)continue;
		for (int j = i + 1; j <= n; j++)
		{
			if (j == num)continue;
			diantot++;
			sum += a[i][j];
			addedge(0, diantot, a[i][j]);
			addedge(diantot, i, 1000000000);
			addedge(diantot, j, 1000000000);
		}
	}
	int q=dicnic(0, n*n + n + 1,1000000000);
	return q == sum;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		team.clear();
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
			scanf("%d %d", &win[i], &defeat[i]);
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				scanf("%d", &a[i][j]);
		for (int i = 1; i <= n; i++)
			if (control(i, win[i]))
				team.push_back(i);
		for (int i = 0; i < team.size(); i++)
		{
			if (i != team.size() - 1)
				printf("%d ", team[i]);
			else
				printf("%d\n", team[i]);
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值