现在的位置: 首页 > 综合 > 正文

POJ 2699 The Maximum Number of Strong Kings 竞赛图+最大流

2017年10月16日 ⁄ 综合 ⁄ 共 2276字 ⁄ 字号 评论关闭

题目大意:有n个人之间互相竞赛,现在给出每个人赢了多少局。若定义一个人是最高分或者这个人赢了所有比他分高的人,那么这个人就算赢了。问最多可能有多少人赢。

思路:最大流模型的另一种应用。二分图,左边是所有选手,右边是所有比赛。

S->所有选手 f:该选手赢了多少局

所有比赛->T f:1

由于最多只有十个人,所以枚举答案就行了。枚举最多有多少人赢了,如果一个分比较低的人赢了,那么分比他高的人必定也可以赢,所以就假设是分数最高的n个人赢了。

在这些人之间的竞赛需要保证分低的人赢了分高的人,那么

i -> match[i][j] if(score[i] < score[j] && win[i] && win[j]) f:1

剩下的比赛谁赢了都行,那么

i->match[i][j] f:1

j->match[i][j] f:1

要注意这两次不能重复。

最后判断是否满流就行了。

CODE:

#include <queue>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define MAX 1010
#define MAXP 20
#define MAXE 2010
#define INF 0x3f3f3f3f
#define S 0
#define T (MAX - 1)
using namespace std;

struct MaxFlow{
	int head[MAX],total;
	int next[MAXE],aim[MAXE],flow[MAXE];
	
	int deep[MAX];
	
	void Initialize() {
		total = 1;
		memset(head,0,sizeof(head));
	}
	void Add(int x,int y,int f) {
		next[++total] = head[x];
		aim[total] = y;
		flow[total] = f;
		head[x] = total; 
	}
	void Insert(int x,int y,int f) {
		Add(x,y,f);
		Add(y,x,0);
	}
	bool BFS() {
		static queue<int> q;
		while(!q.empty())	q.pop();
		memset(deep,0,sizeof(deep));
		deep[S] = 1;
		q.push(S);
		while(!q.empty()) {
			int x = q.front(); q.pop();
			for(int i = head[x]; i; i = next[i])
				if(flow[i] && !deep[aim[i]]) {
					deep[aim[i]] = deep[x] + 1;
					q.push(aim[i]);
					if(aim[i] == T)	return true;
				}
		}
		return false;
	}
	int Dinic(int x,int f) {
		if(x == T)	return f;
		int temp = f;
		for(int i = head[x]; i; i = next[i]) 
			if(flow[i] && temp && deep[aim[i]] == deep[x] + 1) {
				int away = Dinic(aim[i],min(flow[i],temp));
				if(!away)	deep[aim[i]] = 0;
				flow[i] -= away;
				flow[i^1] += away;
				temp -= away;
			}
		return f - temp;
	}
}solver;

int src[MAX],cnt,p;
int id[MAXP][MAXP];
bool v[MAXP][MAXP];

inline void MakeGraph(int ans)
{
	solver.Initialize();
	memset(v,false,sizeof(v));
	for(int i = 1; i <= cnt; ++i)
		solver.Insert(S,i,src[i]);
	for(int i = 1; i <= ans; ++i)
		for(int j = i + 1; j <= ans; ++j)
			if(src[i] > src[j])
				solver.Insert(j,id[i][j],1),v[i][j] = true;
	for(int i = 1; i <= cnt; ++i)
		for(int j = i + 1; j <= cnt; ++j) {
			solver.Insert(id[i][j],T,1);
			if(!v[i][j]) {
				solver.Insert(i,id[i][j],1);
				solver.Insert(j,id[i][j],1);
			}
		}
}

char s[100];

inline void Kill()
{
	char c;
	while(c = getchar(),!isdigit(c));
	ungetc(c,stdin);
}

int main()
{
	int _T;
	for(cin >> _T; _T--;) {
		int t = 0;
		cnt = 0;
		Kill();
		fgets(s,1000,stdin);
		stringstream SS(s);
		while(SS >> p)	src[++cnt] = p;
		reverse(src + 1,src + cnt + 1);
		for(int i = 1; i <= cnt; ++i)
			for(int j = i + 1; j <= cnt; ++j)
				id[i][j] = ++t + cnt;
		int ans = 0;
		for(int i = cnt; i; --i) {
			MakeGraph(i);
			int max_flow = 0;
			while(solver.BFS())
				max_flow += solver.Dinic(S,INF);
			if(max_flow == cnt * (cnt - 1) / 2) {
				ans = i;
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

抱歉!评论已关闭.