标签:
Description
Supporters for the professional soccer clubs participating in the K-League, formerly the Korea Professional Soccer League, hold orderly and organized cheering, as did the Red Devils, the official supporters for the Korean national soccer team during the 2002 Korea-Japan World Cup. After many games of this season have been played, the supporters may wonder whether the team S they are backing can still win the championship. In other words, can winners be assigned for the remaining games so that no team ends with more victories than S?(Two or more teams can win the championship jointly.)
You are given the current number of wins and defeats, wi and di, for every team i, 1<=i<=n, and the remaining number, ai,j, of games to be played between every pair of teams i and j, 1<=i,j<=n, where n is the number of teams. The teams are numbered 1,2,…,n. You are to find all teams that have a possibility of winning the championship. Every team has to play the same number games during the season. For simplicity, we assume that there are no draws, that is, every game has a winner and a loser.
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of three lines: the first line has an integer n, 1<=n<=25, that represents the number of teams in the test case; the second line contains 2n nonnegative integers w1,d1,w2,d2… each at most 100, where wi and di are the current numbers of wins and defeats for team i, respectively; the third line contains n2 nonnegative integers a1,1,a1,2,… each at most 10, where ai,j is the remaining number of games to be played between teams i and j . For all i and j, ai,j=aj,i. If i=j, then ai,j=0. The integers given in a line are delimited by one or more spaces.
Output
Print exactly one line for each test case. The line should contain all teams that have a possibility of winning the championship, in an increasing order of team numbers.
Sample Input
3
3
2 0 1 1 0 2
0 2 2
2 0 2
2 2 0
3
4 0 2 2 0 4
0 1 1
1 0 1
1 1 0
4
0 3 3 1 1 3 3 0
0 0 0 2
0 0 1 0
0 1 0 0
2 0 0 0
Sample Output
1 2 3
1 2
2 4
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <cctype>
using namespace std;
typedef long long ll;
const int PO = 30;
const int N = 1000;
const int M = 20000;
const int INF = 0x3f3f3f3f;
int n, F[PO][PO], s, t;
struct Node{
int w, l;
}team[PO];
namespace IO {
const static int maxn = 20 << 20;
static char buf[maxn], *pbuf = buf, *End;
void init() {
int c = fread(buf, 1, maxn, stdin);
End = buf + c;
}
int &readint() {
static int ans;
ans = 0;
while (pbuf != End && !isdigit(*pbuf)) pbuf ++;
while (pbuf != End && isdigit(*pbuf)) {
ans = ans * 10 + *pbuf - ‘0‘;
pbuf ++;
}
return ans;
}
}
struct Edge{
int from, to;
int cap, flow;
};
struct Dinic{
vector<Edge> edges;
vector<int> G[M];
int vis[N], d[N];
int cur[M];
int ans;
void init() {
ans = 0;
for (int i = 0; i < M; i++) G[i].clear();
edges.clear();
}
void addEdge(int from, int to, int cap) {
edges.push_back((Edge){from, to, cap, 0});
edges.push_back((Edge){to, from, 0, 0});
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
int BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = 0; i < G[u].size(); 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].size(); i++) {
Edge &e = edges[G[u][i]];
if (d[u] + 1 == d[e.to] && (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 dinic() {
while (BFS()) {
memset(cur, 0, sizeof(cur));
ans += DFS(s, INF);
}
return ans;
}
}din;
void input() {
n = IO::readint();
s = 0, t = n * (n + 1) + 2;
for (int i = 1; i <= n; i++) {
team[i].w = IO::readint();
team[i].l = IO::readint();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
F[i][j] = IO::readint();
}
}
}
int getTotal(int x) {
int sum = team[x].w;
for (int i = 1; i <= n; i++) {
sum += F[x][i];
}
return sum;
}
void solve() {
for (int i = 1; i <= n; i++) {
din.addEdge(i, t, 0);
}
int cnt = 1, rec = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
din.addEdge(s, cnt + n, F[i][j]);
rec += F[i][j];
din.addEdge(cnt + n, i, INF);
din.addEdge(cnt + n, j, INF);
cnt++;
}
}
int flag = 0;
for (int i = 1; i <= n; i++) {
int tot = getTotal(i), cap;
for (int j = 0; j < din.edges.size(); j++) din.edges[j].flow = 0;
for (int j = 0; j < 2 * n; j += 2) {
cap = tot - team[(j / 2) + 1].w;
if (cap < 0) {
cap = -1;
break;
}
din.edges[j].cap = cap;
}
if (cap == -1) continue;
din.ans = 0;
if (din.dinic() == rec) {
if (flag) printf(" ");
printf("%d", i);
flag = 1;
}
}puts("");
}
int main() {
IO::init();
int T;
T = IO::readint();
while (T--) {
din.init();
input();
solve();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不可转载。
标签:
原文地址:http://blog.csdn.net/llx523113241/article/details/48105271