1169: Super Market
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 3s | 8192K | 458 | 127 | Standard |
1st Jilin University ACM International Collegiate Programming Contest
Mr. Steward is the CEO of ACM(Auction Collecting Market). We all know it is a super market where Mr. Steward stores various of interesting goods from which customers pick out their lovings by themselves. Of course, settle accounts at the cashier before they leave the super market. Mr. Steward is so smart a bussiness man so that his super market becomes more and more thriving and prosperous. So he decides to open a branch super market in the east of the city. But the main super market is located in the west of the city, and when the branch super market is short of some certain type of goods, Mr. Steward has to transport some of it from the main super market to the branch super market by trucks. There are N crosses between the main super market and the branch super market. Every pair of two crosses may be connected by zero or only one one-way street. The main super market is always located on cross 1 and the branch super market is always located on cross N. The number of goods mustn't exceed the limitation of the street where the goods are transported on. For example, look at the graph below:
In this example, there are 3 crosses. The limitation on the street between cross 1 and cross 2 is 4, that means Mr. Steward can never transport more than 4 goods from cross 1 to cross 2 at a time. Similarly, Mr. Steward can never transport more than 3 goods from cross 2 to cross 3 and never more than 5 goods from cross 1 to cross 3 at a time too. Now Mr. Steward needs to know what the maximum number of goods he can transport from the main super market to the branch super market at a time. In this example, it is 8.
Input Specification
The input consists of several test cases. Each test case is in the following format:
The first line of each test case is an integer N(1<=N<=30).
The next N lines each contains N non-negative integers. The jth integer of the (i+1)th line of each test case designating the limitation on the street from cross i to cross j.
The last test case is marked by N=0, which should not be proceeded.
Output Specification
For each test case, your program should print a single line which looks like 'Maximum number of goods: max.', where max is what your program computes.
Sample Input
3 0 4 5 0 0 3 0 0 0 5 0 10 0 40 0 0 0 20 0 0 0 0 0 0 30 0 0 0 0 50 0 0 0 0 0 0
Sample Output
Maximum number of goods: 8. Maximum number of goods: 50.
This problem is used for contest: 110 156
Submit / Problem List / Status / Discuss
#include<stdio.h>
#include<algorithm>
using namespace std;
int flow[35][35],dist[35],gap[35],n;
int find_path(int p,int limit=0x3f3f3f3f)
{
if(p==n-1) return limit;
for(int i=0;i<n;i++)
if(dist[p]==dist[i]+1 && flow[p][i]>0)
{
int t=find_path(i,min(flow[p][i],limit));
if(t<0) return t;
if(t>0)
{
flow[p][i]-=t;
flow[i][p]+=t;
return t;
}
}
int label=n;
for(int i=0;i<n;i++) if(flow[p][i]>0) label=min(label,dist[i]+1);
if(--gap[dist[p]]==0 || dist[0]>=n ) return -1;
++gap[dist[p]=label];
return 0;
}
int sap()
{
int t=0,maxflow=0;
gap[0]=n;
while((t=find_path(0))>=0)
maxflow+=t;
return maxflow;
}
int main()
{
while((scanf("%d",&n),n)!=0)
{
memset(gap,0,sizeof(gap));
memset(flow,0,sizeof(flow));
memset(dist,0,sizeof(dist));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&flow[i][j]);
printf("Maximum number of goods: %d./n",sap());
}
return 0;
}