Problem description]
In some one-on-one Games (such as playing chess, playing table tennis, and playing badminton), we often encounter interesting situations where A is better than B, B is better than C, and C is better than, the image may be called the case of scissors. Sometimes, bored people will repeat how many of these scissors may happen, that is, how many pairs of unordered three tuples (A, B, C ), meet one of them to win another person in the competition, the other to win the third person, and the third to win the first person. Note that the unordered sequence here means that the sequence of elements in the three tuples is not important. It will be (A, B, C), (A, C, B), (B, A, C), (B, C, A), (C, A, B) and (C, B, A) are considered the same situation.
There are n people participating in a game like this. The competition requires that any two people have to compete in a game: in this case, there is a total of games. The competition is already in progress, and we want to know the maximum number of scissors and stones that will happen after the competition ends in extreme cases. That is to say, you can give the results of the game that has already occurred, and you can arrange the results of the remaining game to get as many Scissors as possible.
[Input file]
The row 1st in the input file is an integer N, indicating the number of participants.
The following is a matrix of N rows and n columns: N rows in total, n columns in each row, and numbers are separated by spaces.
If the number in column J of row (I + 1) is 1, it indicates that I has won J in the competition; if the number is 0, it indicates that I was defeated by J in an existing competition. The number is 2, indicating that the competition between I and j has not yet occurred. The number on the diagonal of the Number Matrix, that is, the numbers in column I of the (I + 1) Row are all 0. They are only placeholder symbols and have no meaning.
The input file is valid and does not conflict with each other. When I = J, column J and column (J + 1) of the (I + 1) Column) the two numbers in column I of the row are either 2 or 0 and 1.
[Output file]
The row 1st of the output file is an integer, indicating how many scissors are shown in the result of your game.
The first row of the output file contains a Number Matrix of N rows and n columns in the same format as the input file. The J number in line (I + 1) describes the results of the game between I and j. 1 indicates that I wins J, 0 indicates that I loses J, different from the input matrix, this matrix does not indicate the number 2 that has not been performed in the competition; the number on the diagonal is 0. The output matrix must be legal and cannot conflict with each other.
[Example input]
3
0 1 2
0 0 2
2 2 0
[Sample output]
1
0 1 0
0 0 1
1 0 0
[Scoring standard]
For each test point, you can obtain the full score of the test point only when the number in the first line of your program output is consistent with the standard answer and a legal solution is provided, otherwise, the test point is scored 0 points.
[Data scale]
N ≤ 6 of 30% of data;
N ≤ 100% of the data.
Question: data stored on the Internet
Q: In the game diagram, we plan to win or lose, making the smallest ring the most, that is, the case of scissors
Analysis: from the front analysis, we can't see anything. We can use the dual nature to find situations that cannot constitute a scissors and stone cloth. For any three points, there are also three edges between the three points. If the inbound degree of one of the points is 2, this figure is not the case of the scissors, for the inbound degrees in [I] of a vertex in the entire graph, there are C (in [I], 2) cases, then the number of headers that can constitute the scissors is C (n, 3)-sum {C (I [I], 2)} (1 <= I <= N)
Convert this formula with S = N * (n-1) * (n-2)-sum {in [I] ^ 2}/2 + sum {in [I]}/2
Since S = N * (n-1) * (n-2) + sum {in [I]}/2 is a value, therefore, you only need to ensure that the sum {in [I] ^ 2}/2 is the minimum value.
In fact, it is not easy to think of it here. In fact, I did not even think of it here =
How can we minimize this value? You can fix all edges first, and then adjust to make the value smaller... I have never tried this method. I'm more surprised that the solution is actually a cost stream ??
There are still some unfixed edges indicating a node and N nodes in the virtual Source and Sink points. For fixed edges, The Source Vertex and the corresponding vertex are connected to an edge, capacity is 1 and cost is 0. For non-fixed edges, the connection capacity between the Source Vertex and the corresponding node of this edge is 1, and the cost is 0, the corresponding node of this edge and the two endpoints of this edge are connected to an edge with a capacity of 1 and a cost of 0 respectively. How can we calculate in [I] ^ 2 for N points, that is, the traffic square?
The question solution is calculated by cleverly converting it into a fee,
Traffic 1 2 3 4... n
Cost 1 4 9 16... n ^ 2
We can split this edge into n edges with a capacity of 1 at the cost of 1, 3, 5, 7, and 9 respectively...
And then find the maximum flow of the minimum fee
The idea of the whole question is rather clever... Orz
PS: it is one thing that Changchun is hard at playing. I feel that my thinking is too weak. I want to do some Oi-related questions recently. I feel very hard at thinking. Even if I have an algorithm for the whole question, but I still can't do it. Only by thinking more can I be stronger.
Code:
#include<cstdio>#include<iostream>using namespace std;const int mm=1111111;const int mn=111111;const int oo=1e9;int node,src,dest,edge;int ver[mm],flow[mm],cost[mm],next[mm];int head[mn],dis[mn],q[mn],p[mn],vis[mn];void prepare(int _node,int _src,int _dest){ node=_node,src=_src,dest=_dest; for(int i=0;i<node;++i)head[i]=-1,vis[i]=0; edge=0;}void addedge(int u,int v,int f,int c){ ver[edge]=v,flow[edge]=f,cost[edge]=c,next[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,cost[edge]=-c,next[edge]=head[v],head[v]=edge++;}bool spfa(){ int i,u,v,l,r=0,tmp; for(i=0;i<node;++i)dis[i]=oo; dis[q[r++]=src]=0; p[dest]=p[src]=-1; for(l=0;l!=r;(++l>=mn)?l=0:l) for(i=head[u=q[l]],vis[u]=0;i>=0;i=next[i]) if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i])) { dis[v]=tmp; p[v]=i^1; if(vis[v])continue; vis[q[r++]=v]=1; if(r>=mn)r=0; } return p[dest]>=0;}int spfaflow(){ int i,ret=0,delta; while(spfa()) { for(i=p[dest],delta=oo;i>=0;i=p[ver[i]]) if(flow[i^1]<delta)delta=flow[i^1]; for(i=p[dest];i>=0;i=p[ver[i]]) flow[i]+=delta,flow[i^1]-=delta; ret+=delta*dis[dest]; } return ret;}int a[111][111];int i,j,k,l,n,m,ans;int main(){ while(~scanf("%d",&n)) { for(m=0,i=1;i<=n;++i) for(j=1;j<=n;++j) { scanf("%d",&a[i][j]); if(i<j&&a[i][j]>1)++m; } prepare(m+n+2,0,m+n+1); for(k=0,i=1;i<=n;++i) for(j=i+1;j<=n;++j) { if(a[i][j]<2) { if(a[i][j])addedge(src,m+j,1,0); else addedge(src,m+i,1,0); } else { ++k; addedge(src,k,1,0); addedge(k,m+i,1,0); addedge(k,m+j,1,0); } } for(i=1;i<=n;++i) for(j=1;j<=n;++j) addedge(m+i,dest,1,j*2-1); ans=n*(n-1)*(n-2)/3+n*(n-1)/2; ans=(ans-spfaflow())/2; for(k=0,i=1;i<=n;++i) for(j=i+1;j<=n;++j) if(a[i][j]>1) for(l=head[++k];l>=0;l=next[l]) if(!flow[l]) { if(ver[l]-m==i)a[j][i]=1,a[i][j]=0; else a[i][j]=1,a[j][i]=0; } printf("%d\n",ans); for(i=1;i<=n;++i) for(j=1;j<=n;++j) printf("%d%c",a[i][j],j<n?' ':'\n'); } return 0;}