zl程序教程

您现在的位置是:首页 >  云平台

当前栏目

UVaLive 2531 The K-League (网络流)

网络 The UVALive
2023-09-11 14:17:18 时间

题意:有 n 个队伍进行比赛,每个队伍比赛数目是一样的,每场恰好一个胜一个负,给定每个队伍当前胜的场数败的数目,以及两个队伍剩下的比赛场数,问你冠军队伍可能是哪些队。

析:对每个队伍 i 进行判断是不是能冠军,最优的情况的就是剩下的比赛全都胜,也就是一共胜的数目就是剩下的要比赛的数再加上原来胜的数目sum,然后把每两个队伍比赛看成一个结点,(u, v),然后从 s 向 结点加一条容量要打的比赛数目的容量,然后从 (u, v) 向 u 和 v 分别加一条容量为无穷大的边,然后每个 u 向 t 加一条容量为 sum - w[i] ,跑一个最大流,如果是满流是,那么就是有解,也就是 i 可能是冠军。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 25 * 25 + 25 + 50;
const int maxm = 1e6 + 5;
const int mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

struct Edge{
  int from, to, cap, flow;
};

struct Dinic{
  int n, m, s, t;
  vector<Edge> edges;
  vector<int> G[maxn];
  bool vis[maxn];
  int d[maxn];
  int cur[maxn];

  void init(int n){
    this-> n = n;
    for(int i = 0; i < n; ++i)  G[i].cl;
    edges.cl;
  }

  void addEdge(int from, int to, int cap){
    edges.pb((Edge){from, to, cap, 0});
    edges.pb((Edge){to, from, 0, 0});
    m = edges.sz;
    G[from].pb(m - 2);
    G[to].pb(m - 1);
  }

  bool bfs(){
    ms(vis, 0);  d[s] = 0;  vis[s] = 1;
    queue<int> q;
    q.push(s);

    while(!q.empty()){
      int u = q.front();  q.pop();
      for(int i = 0; i < G[u].sz; ++i){
        Edge &e = edges[G[u][i]];
        if(!vis[e.to] && e.cap > e.flow){
          vis[e.to] = 1;
          d[e.to] = d[u] + 1;
          q.push(e.to);
        }
      }
    }
    return vis[t];
  }

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

  int maxflow(int s, int t){
    this-> s = s;
    this-> t = t;
    int flow = 0;
    while(bfs()){ ms(cur, 0);  flow += dfs(s, INF); }
    return flow;
  }
};

Dinic dinic;

int w[30], d[30];
int a[30][30];

int main(){
  int T;  cin >> T;
  while(T--){
    scanf("%d", &n);
    int s = 0, t = n * n + n + 1;
    for(int i = 1; i <= n; ++i)  scanf("%d %d", w + i, d + i);
    for(int i = 1; i <= n; ++i)
      for(int j = 1; j <= n; ++j)
        scanf("%d", a[i] + j);

    vector<int> ans;
    int mmax = *max_element(w+1, w+n+1);
    for(int i = 1; i <= n; ++i){
      int sum = accumulate(a[i]+1, a[i]+n+1, w[i]);
      if(sum < mmax)  continue;
      dinic.init(t + 5);
      int cnt = 0;
      for(int j = 1; j <= n; ++j)
        for(int k = j+1; k <= n; ++k){
          if(j == i || i == k)  continue;
          int x = (j-1)*n + k;
          dinic.addEdge(s, x, a[j][k]);
          dinic.addEdge(x, j + n*n, INF);
          dinic.addEdge(x, k + n*n, INF);
          cnt += a[j][k];
        }
      for(int j = 1; j <= n; ++j)
        if(i != j)  dinic.addEdge(j + n*n, t, sum - w[j]);
      if(cnt == dinic.maxflow(s, t))  ans.push_back(i);
    }
    for(int i = 0; i < ans.sz; ++i)
      printf("%d%c", ans[i], " \n"[i+1==ans.sz]);
  }
  return 0;
}