网络流。枚举每个队夺冠。设win为夺冠赢得场数,那么显然别的队赢得次数都不能超过win。于是我们可以从源点s向每个队u(除了当前枚举的队)加边,容量为win-w[i],即该队还能赢多少场,然后再从从该队向节点(u,v)连边,容量为a[u][v]表示u和v比最多赢a[u][v]场,最后从节点(u, v) 向汇点t连边,容量为a[u][v],表示这个节点最多能产生a[u][v]场胜利。如果最大流等于所有的a[u][v]之和的话,就表示可以满足。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from, to, cap, flow;
Edge() {}
Edge(int from, int to, int cap, int flow)
:from(from), to(to), cap(cap), flow(flow) {}
};
struct ISAP
{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn], cur[maxn];
int p[maxn], num[maxn];
void ClearAll(int n)
{
this->n = n;
for(int i = 0; i < n; i ++) G[i].clear();
edges.clear();
}
void ClearFlow()
{
for(int i = 0; i < edges.size(); i ++)
edges[i].flow = 0;
}
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
CLR(vis, false);
queue<int> Q;
Q.push(t);
vis[t] = 1; d[t] = 0;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(int i = 0; i < G[x].size(); i ++)
{
Edge& e = edges[G[x][i]^1];
if(!vis[e.from] && e.cap > e.flow)
{
vis[e.from] = true;
d[e.from] = d[x] + 1;
Q.push(e.from);
}
}
}
return vis[s];
}
int Augment()
{
int x = t, a = INF;
while(x != s)
{
Edge& e = edges[p[x]];
a = min(a, e.cap-e.flow);
x = edges[p[x]].from;
}
x = t;
while(x != s)
{
edges[p[x]].flow += a;
edges[p[x]^1].flow -= a;
x = edges[p[x]].from;
}
return a;
}
int Maxflow(int s, int t, int limit)
{
this->s = s; this->t = t;
int flow = 0;
BFS();CLR(num, 0);
for(int i = 0; i < n; i ++) num[d[i]] ++;
int x = s;
CLR(cur, 0);
while(d[s] < n)
{
if(x == t)
{
flow += Augment();
if(flow >= limit) return flow;
x = s;
}
int ok = 0;
for(int i = cur[x]; i < G[x].size(); i ++)
{
Edge& e = edges[G[x][i]];
if(e.cap > e.flow && d[x] == d[e.to] + 1)
{
ok = 1;
p[e.to] = G[x][i];
cur[x] = i;
x = e.to;
break;
}
}
if(!ok)
{
int m = n - 1;
for(int i = 0; i < G[x].size(); i ++)
{
Edge& e = edges[G[x][i]];
if(e.cap > e.flow) m = min(m, d[e.to]);
}
if(-- num[d[x]] == 0) break;
num[d[x] = m + 1] ++;
cur[x] = 0;
if(x != s) x = edges[p[x]].from;
}
}
return flow;
}
void Reduce()
{
for(int i = 0; i < edges.size(); i ++)
edges[i].cap -= edges[i].flow;
}
vector<int> Mincut()
{
vector<int> ans;
BFS();
for(int i = 0; i < edges.size(); i ++)
{
Edge e = edges[i];
if(!vis[e.from] && vis[e.to] && e.cap > 0) ans.push_back(i);
}
return ans;
}
} sol;
int a[30][30], w[30], d[30];
vector<int> ans;
int main()
{
int T, n;
scanf("%d", &T);
while(T --)
{
scanf("%d", &n);
for(int i = 0; i < n; i ++)
{
scanf("%d%d", &w[i], &d[i]);
}
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
scanf("%d", &a[i][j]);
ans.clear();
for(int i = 0; i < n; i ++)
{
sol.ClearAll(n + n * n + 2);
int s = n + n * n, t = s + 1;
int win = w[i], tot = 0;bool flag = true;
for(int j = 0; j < n; j ++) win += a[i][j];
for(int j = 0; j < n; j ++) if(j != i)
{
if(win - w[j] < 0)
{
flag = false;break;
}
sol.AddEdge(s, j, win - w[j]);
for(int k = 0; k < j; k ++) if(k != i)
{
sol.AddEdge(j, j * n + k, a[j][k]);
sol.AddEdge(k, j * n + k, a[j][k]);
sol.AddEdge(j * n + k, t, a[j][k]);
tot += a[j][k];
}
}
if(!flag) continue;
if(sol.Maxflow(s, t, INF) == tot)
ans.push_back(i + 1);
}
for(int i = 0; i < ans.size(); i ++)
printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' ');
}
}
————————————————
版权声明:本文为CSDN博主「ok_again」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ok_again/article/details/22422635