棋盘填数0~K,规定各行列和,判断方案数是否唯一,唯一输出解。
记得似乎POJ上有道类似的题目之前做过。。
点化边,为每个点建立一条边容量为K,那么跑出来的流量就是答案。
然后要限制指定边集的总和,那么用1条边连接这些要限制的边,容量为行或列和,假如跑网络流,即满流存在解。
行列分别建点。具体见图方法显然。
4975 真心跪,时间卡的不要不要的。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f, N = 1005, M = 400005;
int level[N], cnt, v[M], w[M], p[M], h[N], q[M], s, t, mat[512][512];
void add(int a, int b, int c) {
p[++cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt;
p[++cnt] = h[b]; v[cnt] = a; w[cnt] = 0; h[b] = cnt;
}
bool bfs() {
int f = 0, r = 0, u, i;
memset(level, -1, sizeof level);
q[r++] = s; level[s] = 1;
while (f < r) {
u = q[f++];
for (i = h[u]; i; i = p[i]) {
if (w[i] && level[v[i]] == -1) {
level[v[i]] = level[u] + 1;
q[r++] = v[i];
}
}
}
return level[t] != -1;
}
int dfs(int u, int low) {
int i, tmp = 0, res = 0;
if (u == t) return low;
for (i = h[u]; i && res < low; i = p[i]) {
if (w[i] && level[v[i]] == level[u] + 1) {
tmp = dfs(v[i], min(w[i], low - res));
w[i] -= tmp; w[i ^ 1] += tmp; res += tmp;
}
}
if (!res) level[u] = -1;
return res;
}
int dinic() {
int ans = 0;
while (bfs()) ans += dfs(s, inf);
return ans;
}
int vis[N];
int judge(int x, int fa) {
if (vis[x]) return 1;
vis[x] = 1;
for (int i = h[x]; i; i = p[i])
if (w[i] && v[i] != fa && v[i] != s && v[i] != t)
if (judge(v[i], x)) return 1;
vis[x] = 0;
return 0;
}
int main() {
int i, j, c, s1, s2, n, m, k;
while (scanf("%d%d%d", &n, &m, &k) == 3) {
s = 0; t = n + m + 1; s1 = s2 = 0; cnt = 1;
memset(h, 0, sizeof h);
for (i = 1; i <= n; ++i) {
scanf("%d", &c); s1 += c;
add(s, i, c);
}
for (i = 1; i <= n; ++i)
for (j = 1; j <= m; ++j)
add(i, j + n, k);
for (i = 1; i <= m; ++i) {
scanf("%d", &c); s2 += c;
add(i + n, t, c);
}
if (s1 != s2) puts("Impossible");
else {
int d = dinic();
if (d != s1) puts("Impossible");
else {
memset(vis, 0, sizeof vis);
bool flag = 1;
for (i = 1; i <= n; ++i)
if (judge(i, 0)) {
flag = 0; break;
}
if (!flag) puts("Not Unique");
else {
puts("Unique");
for (j = 1; j <= n; ++j)
for (i = h[j]; i; i = p[i])
if (n < v[i] && v[i] <= n + m)
mat[j][v[i] - n] = k - w[i];
for (i = 1; i <= n; ++i)
for (j = 1; j <= m; ++j)
printf("%d%c", mat[i][j], j == m ? '\n' : ' ');
}
}
}
}
return 0;
}
包括2016级新生)除了校赛,还有什么途径可以申请
Problem Description
Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has a good memory and she can remember all drawings she has seen.
Today Alice designs a game using these drawings in her memory. First, she matches K+1 colors appears in the picture to K+1 different integers(from 0 to K). After that, she slices the drawing into grids and there are N rows and M columns. Each grid has an integer on it(from 0 to K) representing the color on the corresponding position in the original drawing. Alice wants to share the wonderful drawings with Bob and she tells Bob the size of the drawing, the number of different colors, and the sum of integers on each row and each column. Bob has to redraw the drawing with Alice’s information. Unfortunately, somtimes, the information Alice offers is wrong because of Alice’s poor math. And sometimes, Bob can work out multiple different drawings using the information Alice provides. Bob gets confused and he needs your help. You have to tell Bob if Alice’s information is right and if her information is right you should also tell Bob whether he can get a unique drawing.
Input
The input contains mutiple testcases.
For each testcase, the first line contains three integers N(1 ≤ N ≤ 400) , M(1 ≤ M ≤ 400) and K(1 ≤ K ≤ 40).
N integers are given in the second line representing the sum of N rows.
M integers are given in the third line representing the sum of M columns.
The input is terminated by EOF.
Output
For each testcase, if there is no solution for Bob, output “Impossible” in one line(without the quotation mark); if there is only one solution for Bob, output “Unique” in one line(without the quotation mark) and output an N * M matrix in the following N lines representing Bob’s unique solution; if there are many ways for Bob to redraw the drawing, output “Not Unique” in one line(without the quotation mark).
Sample Input
2 2 4
4 2
4 2
4 2 2
2 2 5 0
5 4
1 4 3
9
1 2 3 3
Sample Output
Not Unique
Impossible
Unique
1 2 3 3
Author
Fudan University
Source
2014 Multi-University Training Contest 3