UVA 1306 - The K-League(网络流)

UVA 1306 - The K-League

题目链接

题意:n个球队,已经有一些胜负场,现在还有一些场次,你去分配胜负,问每支球队有没有可能获胜

思路:网络流公平分配模型,把场次当作任务,分配给人,然后先贪心,枚举每个人,让这些人能赢的都赢,剩下的去建图,每个源点连向比赛容量为场次,每个比赛连向2个球队,容量无限大,每个球队连向汇点,容量为每个的人的总和减去当前已经赢的,建完图跑一下最大流,然后判断源点流出的是否都满流即可

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int MAXNODE = 1005;
const int MAXEDGE = 100005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
	int u, v;
	Type cap, flow;
	Edge() {}
	Edge(int u, int v, Type cap, Type flow) {
		this->u = u;
		this->v = v;
		this->cap = cap;
		this->flow = flow;
	}
};

struct Dinic {
	int n, m, s, t;
	Edge edges[MAXEDGE];
	int first[MAXNODE];
	int next[MAXEDGE];
	bool vis[MAXNODE];
	Type d[MAXNODE];
	int cur[MAXNODE];
	vector<int> cut;

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
	}
	void add_Edge(int u, int v, Type cap) {
		edges[m] = Edge(u, v, cap, 0);
		next[m] = first[u];
		first[u] = m++;
		edges[m] = Edge(v, u, 0, 0);
		next[m] = first[v];
		first[v] = m++;
	}

	bool bfs() {
		memset(vis, false, sizeof(vis));
		queue<int> Q;
		Q.push(s);
		d[s] = 0;
		vis[s] = true;
		while (!Q.empty()) {
			int u = Q.front(); Q.pop();
			for (int i = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (!vis[e.v] && e.cap > e.flow) {
					vis[e.v] = true;
					d[e.v] = d[u] + 1;
					Q.push(e.v);
				}
			}
		}
		return vis[t];
	}

	Type dfs(int u, Type a) {
		if (u == t || a == 0) return a;
		Type flow = 0, f;
		for (int &i = cur[u]; i != -1; i = next[i]) {
			Edge& e = edges[i];
			if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i^1].flow -= f;
				flow += f;
				a -= f;
				if (a == 0) break;
			}
		}
		return flow;
	}

	Type Maxflow(int s, int t) {
		this->s = s; this->t = t;
		Type flow = 0;
		while (bfs()) {
			for (int i = 0; i < n; i++)
				cur[i] = first[i];
			flow += dfs(s, INF);
		}
		return flow;
	}

	void MinCut() {
		cut.clear();
		for (int i = 0; i < m; i += 2) {
			if (vis[edges[i].u] && !vis[edges[i].v])
				cut.push_back(i);
		}
	}
} gao;

const int N = 35;
int t, n, win[N], g[N][N], ans[N], an, x[N * N], y[N * N];

bool judge(int u) {
	int sum = win[u];
	for (int i = 1; i <= n; i++)
		sum += g[u][i];
	gao.init(0);
	int tot = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = i + 1; j <= n; j++) {
			if (i == u || j == u || g[i][j] == 0) continue;
			gao.add_Edge(0, ++tot, g[i][j]);
			x[tot] = i; y[tot] = j;
		}
	}
	for (int i = 1; i <= tot; i++) {
		gao.add_Edge(i, x[i] + tot, INF);
		gao.add_Edge(i, y[i] + tot, INF);
	}
	int t = tot + n + 1;
	gao.n = t + 1;
	for (int i = 1; i <= n; i++) {
		if (sum - win[i] < 0) return false;
		gao.add_Edge(i + tot, t, sum - win[i]);
	}
	gao.Maxflow(0, t);
	for (int i = gao.first[0]; i + 1; i = gao.next[i])
		if (gao.edges[i].flow != gao.edges[i].cap) return false;
	return true;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		an = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
			scanf("%d%*d", &win[i]);
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				scanf("%d", &g[i][j]);
		for (int i = 1; i <= n; i++)
			if (judge(i)) ans[an++] = i;
		for (int i = 0; i < an; i++)
			printf("%d%c", ans[i], i == an - 1 ? '\n' : ' ');
	}
	return 0;
}


你可能感兴趣的:(UVA 1306 - The K-League(网络流))