G. Think I'll Buy Me a Football Team


Time Limit: 3000ms



Memory Limit: 131072KB

64-bit integer IO format:

%lld      Java class name: Main

链接:​​http://www.bnuoj.com/v3/contest_show.php?cid=5512#problem/G​




​Submit​​ ​​Status​​ ​​ PID: 11249链接:​




​[PDF Link]​

Falling Stocks. Bankrupted companies. Banks with no Cash. Seems like the best time to invest: ``Think I'll buy me a football team!"

No seriously, I think I have the solution to at least the problem of cash in banks. Banks nowadays are all owing each other great amounts of money and no bank has enough cash to pay other banks' debts even though, on paper at least, they should have enough money to do so. Take for example the inter-bank loans shown in figure (a). The graph shows the amounts owed between four banks (A ...D). For example, A owes B 50M while, at the same time, B owes A 150M. (It is quite common for two banks to owe each other at the same time.) A total amount of 380M in cash is needed to settle all debts between the banks.






In an attempt to decrease the need for cash, and after studying the example carefully, I concluded that there's a lot of cash being transferred unnecessarily. Take a look:


  1. C owes D the same amount as D owes A, so we can say that C owes A an amount of 30M and get D out of the picture.
  2. But since A already owes C 100M, we can say that A owes C an amount of 70M.
  3. Similarly, B owes A 100M only, (since A already owes B 50M.) This reduces the above graph to the one shown in figure (b) which reduces the needed cash amount to 190M (A reduction of 200M, or 53%.)
  4. I can still do better. Rather than B paying A 100M and A paying 70M to C, B can pay 70M (out of A's 100M) directly to C. This reduces the graph to the one shown in figure (c). Banks can settle all their debts with only 120M in cash. A total reduction of 260M or 68%. Amazing!

I have data about inter-bank debts but I can't seem to be able to process it to obtain the minimum amount of cash needed to settle all the debts. Could you please write a program to do that?




Input



Your program will be tested on one or more test cases. Each test case is specified onN + 1 lines where N < 1, 000 is the number of banks and is specified on the first line. The remainingN lines specifies the inter-bank debts using anN×N




Output




k​. ​B​​A


where k is the test case number (starting at 1,)​​​​is a space character, B is the amount of cash needed before reduction andA




Sample Input


4 0 50 100 0 150 0 20 0 0 0 0 30 30 0 0 0 0


Sample Output


1. 380 120


题意:

这道题目很有意思,要求简化债务关系。一开始感觉很复杂,稍加分析后发现其实就是简化一下图型,

第一个数据统计银行所有金额

第二个数据统计别人欠自己的钱的总额。
两个二次循环:

代码:


#include<cmath>
#include<queue>
#include<deque>
#include<stack>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
int dir[4][2]= {{0,-1},{-1,0},{0,1},{1,0}};
const double Pi = acos(-1.0);
const int maxn=1111;
const double eps=1e-10;
int map[maxn][maxn];
int aa[maxn];
int bb[maxn];
int main()
{
int n,i,j,m,k=0,ans,cnt,res;
while(scanf("%d",&n),n)
{
mem(aa,0);
mem(bb,0);
ans=0,cnt=0,res=0;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%d",&map[i][j]);
ans+=map[i][j];
aa[i]+=map[i][j];
}
}

for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
bb[i]+=map[j][i];
}
cnt=aa[i]-bb[i];
if(cnt>0)
{
res+=cnt;
}
}
printf("%d. %d %d\n",++k,ans,res);
}
return 0;
}