Description

在一些一对一游戏的比赛(如下棋、乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之为剪刀石头布情况。有的时候,无聊的人们会津津乐道于统计有多少这样的剪刀石头布情况发生,即有多少对无序三元组(A, B, C),满足其中的一个人在比赛中赢了另一个人,另一个人赢了第三个人而第三个人又胜过了第一个人。注意这里无序的意思是说三元组中元素的顺序并不重要,将(A, B, C)、(A, C, B)、(B, A, C)、(B, C, A)、(C, A, B)和(C, B, A)视为相同的情况。
N个人参加一场这样的游戏的比赛,赛程规定任意两个人之间都要进行一场比赛:这样总共有场比赛。比赛已经进行了一部分,我们想知道在极端情况下,比赛结束后最多会发生多少剪刀石头布情况。即给出已经发生的比赛结果,而你可以任意安排剩下的比赛的结果,以得到尽量多的剪刀石头布情况。

Input

输入文件的第1行是一个整数N,表示参加比赛的人数。
之后是一个NN列的数字矩阵:一共N行,每行N列,数字间用空格隔开。
在第(i+1)行的第j列的数字如果是1,则表示i在已经发生的比赛中赢了j;该数字若是0,则表示在已经发生的比赛中i败于j;该数字是2,表示ij之间的比赛尚未发生。数字矩阵对角线上的数字,即第(i+1)行第i列的数字都是0,它们仅仅是占位符号,没有任何意义。
输入文件保证合法,不会发生矛盾,当ij时,第(i+1)行第j列和第(j+1)行第i列的两个数字要么都是2,要么一个是0一个是1。

Output

输出文件的第1行是一个整数,表示在你安排的比赛结果中,出现了多少剪刀石头布情况。
输出文件的第2行开始有一个和输入文件中格式相同的NN列的数字矩阵。第(i+1)行第j个数字描述了ij之间的比赛结果,1表示i赢了j,0表示i负于j,与输入矩阵不同的是,在这个矩阵中没有表示比赛尚未进行的数字2;对角线上的数字都是0。输出矩阵要保证合法,不能发生矛盾。
 
PS:这题太牛叉了值得一做……
 
代码(896MS):
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXV = MAXN * MAXN;
const int MAXE = MAXN * MAXV;
const int INF = 0x7f7f7f7f; struct ZWK_FLOW {
int head[MAXV], dis[MAXV];
int to[MAXE], next[MAXE], flow[MAXE], cost[MAXE];
int n, ecnt, st, ed; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c, int w) {
to[ecnt] = v; flow[ecnt] = c; cost[ecnt] = w; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; flow[ecnt] = ; cost[ecnt] = -w; next[ecnt] = head[v]; head[v] = ecnt++;
//printf("%d %d %d %d\n", u, v, c, w);
} void spfa() {
for(int i = ; i <= n; ++i) dis[i] = INF;
priority_queue<pair<int, int> > que;
dis[st] = ; que.push(make_pair(, st));
while(!que.empty()) {
int u = que.top().second, d = -que.top().first; que.pop();
if(d != dis[u]) continue;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && dis[v] > d + cost[p]) {
dis[v] = d + cost[p];
que.push(make_pair(-dis[v], v));
}
}
}
int t = dis[ed];
for(int i = ; i <= n; ++i) dis[i] = t - dis[i];
} int minCost, maxFlow;
bool vis[MAXV]; int add_flow(int u, int aug) {
if(u == ed) {
maxFlow += aug;
minCost += dis[st] * aug;
return aug;
}
vis[u] = true;
int now = aug;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && !vis[v] && dis[u] == dis[v] + cost[p]) {
int t = add_flow(v, min(now, flow[p]));
flow[p] -= t;
flow[p ^ ] += t;
now -= t;
if(!now) break;
}
}
return aug - now;
} bool modify_label() {
int d = INF;
for(int u = ; u <= n; ++u) if(vis[u]) {
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && !vis[v]) d = min(d, dis[v] + cost[p] - dis[u]);
}
}
if(d == INF) return false;
for(int i = ; i <= n; ++i) if(vis[i]) dis[i] += d;
return true;
} int min_cost_flow(int ss, int tt, int nn) {
st = ss, ed = tt, n = nn;
minCost = maxFlow = ;
spfa();
while(true) {
while(true) {
for(int i = ; i <= n; ++i) vis[i] = false;
if(!add_flow(st, INF)) break;
}
if(!modify_label()) break;
}
return minCost;
}
} G; int n, m;
int mat[MAXN][MAXN], ans[MAXN][MAXN]; inline int encode(int i, int j) {
if(i > j) swap(i, j);
return i * n + j;
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; ++i) for(int j = ; j <= n; ++j) scanf("%d", &mat[i][j]);
m = n * n;
int ss = n + m + , tt = ss + ;
G.init();
int sum = n * (n - ) * (n - ) / ;
for(int i = ; i <= n; ++i) {
for(int j = , tmp = ; j < n; ++j, tmp += ) G.add_edge(ss, i, , tmp);
for(int j = ; j <= n; ++j) if(mat[i][j] != )
ans[i][j] = G.ecnt, G.add_edge(i, encode(i, j), , );
}
for(int i = ; i <= m; ++i) G.add_edge(i + n, tt, , );
int x = G.min_cost_flow(ss, tt, tt);
printf("%d\n", sum - (x - n * (n - ) / ) / );
for(int i = ; i <= n; ++i) {
for(int j = ; j <= n; ++j) {
if(j != ) printf(" ");
if(mat[i][j] != ) printf("%d", mat[i][j]);
else {
if(G.flow[ans[i][j]] == ) printf("");
else printf("");
}
}
puts("");
}
}

最新文章

  1. apache服务器启动时提示httpd: apr_sockaddr_info_get() failed for
  2. ASP.NET MVC WEB API字段出现k__BackingField
  3. spring注解和xml方式区别详解
  4. 【LeetCode OJ】Pascal&#39;s Triangle
  5. Python 字典和列表的对比应用
  6. ICPC-CAMP day1 D.Around the world
  7. Example of how to use both JDK 7 and JDK 8 in one build.--reference
  8. 如何让多个不同版本的jquery库共存
  9. 使用maven+eclipse搭建最简单的struts2的helloworld
  10. Dubbo与Zookeeper、SpringMVC整合和使用
  11. JDBC 连接数据库的步骤
  12. Jquery对话框基本配置
  13. 关于HTML、CSS、JavaScript三者关系的简述
  14. k8s编排最佳实践
  15. IDEA+Springboot+JRebel热部署实现
  16. SQL Server nested loop join 效率试验
  17. Django有关的所有命令
  18. (转)netty、mina性能对比分析
  19. WPF中使用BitmapImage处理图片文件(转)
  20. cuda8.0 出错:/usr/bin/ld: 找不到 -lGL【转】

热门文章

  1. golang-Tag
  2. Spring知识点总结(四)之SpringAOP基础
  3. [Linux/Unix]常用命令
  4. IOS异步获取数据并刷新界面dispatch_async的使用方法
  5. 混合应用开发:Phonegap VS AppCan
  6. Angularjs基础(一)
  7. D - 湫湫系列故事——减肥记II
  8. C# 用HttpWebRequest模拟一个虚假的IP伪造ip
  9. 微信小程序使用相机
  10. 学习photoshop心得