POJ 2289 Jamie's Contact Groups & POJ 2112 Optimal Milking 【网络流 + 二分答案】【另一做法,二分+多重匹配】
两题大意类似。大概,对于N个A类物体和P个B类物体进行匹配,然后每个B类物体可以匹配多个A类物体,但是有限制。求出满足这个限制的最优答案。
2112的特点是需要先floyd求出点之间最短距离,限制为匹配中最短路中最长的那一个尽量短;2289的特点是读入很恶心,善用istringstream流,这题的限制是使每个B类物体的最大匹配最小。
多重匹配的做法有空再写~
题目链接: http://poj.org/problem?id=2112
Optimal Milking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 5780 | Accepted: 2195 | |
Case Time Limit: 1000MS |
Description
FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C.
Each milking point can "process" at most M (1 <= M <= 15) cows each day.
Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.
Each milking point can "process" at most M (1 <= M <= 15) cows each day.
Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.
Input
* Line 1: A single line with three space-separated integers: K, C, and M.
* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.
* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.
Output
A single line with a single integer that is the minimum possible total distance for the furthest walking cow.
Sample Input
2 3 2
0 3 2 1 1
3 0 3 2 0
2 3 0 1 0
1 2 1 0 2
1 0 0 2 0
Sample Output
2
Source
USACO 2003 U S Open
报告:
本题大意是C个奶牛和K个奶场匹配。通过一个(k + c) ^ 2的矩阵来求出各点间最短路。
然后网络流建图,源连k个奶场,容量为m,路长为0;k个奶场连c个奶牛,如果最短录存在,容量为1,路长为最短路;c个奶牛连汇,容量为1,路长为0。
二分答案(最长的最短路长),网络流判定可行性(即长于答案的弧不进行增广),输出答案。
注意:
再次注意下标。源0,汇n。floyd是1~(n-1) 而且 map[i][k] == inf or map[k][j] == inf 不能update map[i][j] 。。。这题读入太二 了,要把非i = j ,map[i][j] = 0 改为map[i][j] = inf。。表示初始不连通。
报告:
本题大意是C个奶牛和K个奶场匹配。通过一个(k + c) ^ 2的矩阵来求出各点间最短路。
然后网络流建图,源连k个奶场,容量为m,路长为0;k个奶场连c个奶牛,如果最短录存在,容量为1,路长为最短路;c个奶牛连汇,容量为1,路长为0。
二分答案(最长的最短路长),网络流判定可行性(即长于答案的弧不进行增广),输出答案。
注意:
再次注意下标。源0,汇n。floyd是1~(n-1) 而且 map[i][k] == inf or map[k][j] == inf 不能update map[i][j] 。。。这题读入太二 了,要把非i = j ,map[i][j] = 0 改为map[i][j] = inf。。表示初始不连通。
#include
<
iostream
>
#include < cstdio >
#include < cstring >
using namespace std;
#define maxn 250
#define maxm 130000
const int inf = ~ 0u >> 1 ;
struct edge
{
int p,next,val,anti,len;
edge(){}
edge( int a, int b, int c, int d, int l):p(a),next(b),val(c),anti(d),len(l){}
}v[maxn],e[maxm],path[maxn],arc[maxn];
int flow[maxn],pre[maxn],d[maxn],cnt[maxn];
int n,tot,lo,hi,mid;
const int up = 60000 ;
void init()
{
tot = 0 ;
for ( int i = 0 ;i <= n;i ++ )
{
d[i] = 0 ;
v[i].next = - 1 ;
cnt[i] = 0 ;
}
cnt[ 0 ] = n + 1 ;
}
void add( int a, int b, int c, int d)
{
e[tot] = edge(b,v[a].next,c,tot + 1 ,d);
v[a].next = tot ++ ;
e[tot] = edge(a,v[b].next, 0 ,tot - 1 ,d);
v[b].next = tot ++ ;
}
int map[maxn][maxn];
void floyd()
{
for ( int k = 1 ;k <= n - 1 ;k ++ )
for ( int i = 1 ;i <= n - 1 ;i ++ )
for ( int j = 1 ;j <= n - 1 ;j ++ )
{
if (i == j || map[i][k] == inf || map[k][j] == inf) // WA point , disconnected edges can not be used for update map[i][j].
continue ;
if (map[i][j] == inf || map[i][j] > map[i][k] + map[k][j])
map[i][j] = map[i][k] + map[k][j];
}
}
int mflow( int s, int t)
{
int total,i,k,loc,least,now;
bool flag;
for (i = 0 ;i <= n;i ++ )
arc[i].next = v[i].next;
for (total = 0 ,i = s,now = inf;d[i] < n + 1 ;)
{
flow[i] = now;
for (flag = false ,k = arc[i].next; ~ k;k = e[k].next)
{
if (e[k].val && e[k].len <= mid && d[i] == d[e[k].p] + 1 )
{
now = min(e[k].val,now);
pre[e[k].p] = i;
path[e[k].p].next = k; // differ it
arc[i].next = k; // from above
i = e[k].p;
if (i == t)
{
for (total += now;i != s;i = pre[i])
{
e[path[i].next].val -= now;
e[e[path[i].next].anti].val += now;
}
now = inf;
}
flag = true ;
break ;
}
}
if ( ! flag)
{
for (least = n + 1 ,k = v[i].next; ~ k;k = e[k].next)
{
if (e[k].val && e[k].len <= mid && least > d[e[k].p])
{
loc = k;
least = d[e[k].p];
}
}
arc[i].next = loc; // do not forget
cnt[d[i]] -- ;
if (cnt[d[i]] == 0 )
break ;
d[i] = least + 1 ;
cnt[d[i]] ++ ;
if (i != s)
{
i = pre[i];
now = flow[i];
}
}
}
return total;
}
int main()
{
int k,c,m;
scanf( " %d %d %d " , & k, & c, & m);
n = k + c + 1 ;
for ( int i = 1 ;i <= k + c;i ++ )
for ( int j = 1 ;j <= k + c;j ++ )
{
scanf( " %d " , & map[i][j]);
if (i != j && map[i][j] == 0 )
map[i][j] = inf;
}
floyd(); /*
for(int i = 1;i <= n - 1;i++)
for(int j = 1;j <= n - 1;j++)
{
printf("%d ",map[i][j]);
if(j == n - 1)printf("\n");
} */
lo = 1 ;
hi = up;
while (lo < hi)
{
mid = (lo + hi) >> 1 ;
init(); // do not forget
for ( int i = 1 ;i <= k;i ++ )
{
add( 0 ,i,m, 0 );
for ( int j = k + 1 ;j <= k + c;j ++ )
if (map[i][j] != inf)
add(i,j, 1 ,map[i][j]);
}
for ( int i = k + 1 ;i <= k + c;i ++ )
add(i,n, 1 , 0 );
int ans = mflow( 0 ,n);
// cout << "ans" << ans << endl;
if (ans == c)
hi = mid;
else
lo = mid + 1 ;
// cout << " lo" << lo << " hi" << hi << endl;
}
printf( " %d\n " ,(lo + hi) >> 1 );
}
#include < cstdio >
#include < cstring >
using namespace std;
#define maxn 250
#define maxm 130000
const int inf = ~ 0u >> 1 ;
struct edge
{
int p,next,val,anti,len;
edge(){}
edge( int a, int b, int c, int d, int l):p(a),next(b),val(c),anti(d),len(l){}
}v[maxn],e[maxm],path[maxn],arc[maxn];
int flow[maxn],pre[maxn],d[maxn],cnt[maxn];
int n,tot,lo,hi,mid;
const int up = 60000 ;
void init()
{
tot = 0 ;
for ( int i = 0 ;i <= n;i ++ )
{
d[i] = 0 ;
v[i].next = - 1 ;
cnt[i] = 0 ;
}
cnt[ 0 ] = n + 1 ;
}
void add( int a, int b, int c, int d)
{
e[tot] = edge(b,v[a].next,c,tot + 1 ,d);
v[a].next = tot ++ ;
e[tot] = edge(a,v[b].next, 0 ,tot - 1 ,d);
v[b].next = tot ++ ;
}
int map[maxn][maxn];
void floyd()
{
for ( int k = 1 ;k <= n - 1 ;k ++ )
for ( int i = 1 ;i <= n - 1 ;i ++ )
for ( int j = 1 ;j <= n - 1 ;j ++ )
{
if (i == j || map[i][k] == inf || map[k][j] == inf) // WA point , disconnected edges can not be used for update map[i][j].
continue ;
if (map[i][j] == inf || map[i][j] > map[i][k] + map[k][j])
map[i][j] = map[i][k] + map[k][j];
}
}
int mflow( int s, int t)
{
int total,i,k,loc,least,now;
bool flag;
for (i = 0 ;i <= n;i ++ )
arc[i].next = v[i].next;
for (total = 0 ,i = s,now = inf;d[i] < n + 1 ;)
{
flow[i] = now;
for (flag = false ,k = arc[i].next; ~ k;k = e[k].next)
{
if (e[k].val && e[k].len <= mid && d[i] == d[e[k].p] + 1 )
{
now = min(e[k].val,now);
pre[e[k].p] = i;
path[e[k].p].next = k; // differ it
arc[i].next = k; // from above
i = e[k].p;
if (i == t)
{
for (total += now;i != s;i = pre[i])
{
e[path[i].next].val -= now;
e[e[path[i].next].anti].val += now;
}
now = inf;
}
flag = true ;
break ;
}
}
if ( ! flag)
{
for (least = n + 1 ,k = v[i].next; ~ k;k = e[k].next)
{
if (e[k].val && e[k].len <= mid && least > d[e[k].p])
{
loc = k;
least = d[e[k].p];
}
}
arc[i].next = loc; // do not forget
cnt[d[i]] -- ;
if (cnt[d[i]] == 0 )
break ;
d[i] = least + 1 ;
cnt[d[i]] ++ ;
if (i != s)
{
i = pre[i];
now = flow[i];
}
}
}
return total;
}
int main()
{
int k,c,m;
scanf( " %d %d %d " , & k, & c, & m);
n = k + c + 1 ;
for ( int i = 1 ;i <= k + c;i ++ )
for ( int j = 1 ;j <= k + c;j ++ )
{
scanf( " %d " , & map[i][j]);
if (i != j && map[i][j] == 0 )
map[i][j] = inf;
}
floyd(); /*
for(int i = 1;i <= n - 1;i++)
for(int j = 1;j <= n - 1;j++)
{
printf("%d ",map[i][j]);
if(j == n - 1)printf("\n");
} */
lo = 1 ;
hi = up;
while (lo < hi)
{
mid = (lo + hi) >> 1 ;
init(); // do not forget
for ( int i = 1 ;i <= k;i ++ )
{
add( 0 ,i,m, 0 );
for ( int j = k + 1 ;j <= k + c;j ++ )
if (map[i][j] != inf)
add(i,j, 1 ,map[i][j]);
}
for ( int i = k + 1 ;i <= k + c;i ++ )
add(i,n, 1 , 0 );
int ans = mflow( 0 ,n);
// cout << "ans" << ans << endl;
if (ans == c)
hi = mid;
else
lo = mid + 1 ;
// cout << " lo" << lo << " hi" << hi << endl;
}
printf( " %d\n " ,(lo + hi) >> 1 );
}
http://poj.org/problem?id=2289
Jamie's Contact Groups
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 4004 | Accepted: 1291 |
Description
Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list in her cell phone. The contact list has become so long that it often takes a long time for her to browse through the whole list to find a friend's number. As Jamie's best friend and a programming genius, you suggest that she group the contact list and minimize the size of the largest group, so that it will be easier for her to search for a friend's number among the groups. Jamie takes your advice and gives you her entire contact list containing her friends' names, the number of groups she wishes to have and what groups every friend could belong to. Your task is to write a program that takes the list and organizes it into groups such that each friend appears in only one of those groups and the size of the largest group is minimized.
Input
There will be at most 20 test cases. Ease case starts with a line containing two integers N and M. where N is the length of the contact list and M is the number of groups. N lines then follow. Each line contains a friend's name and the groups the friend could belong to. You can assume N is no more than 1000 and M is no more than 500. The names will contain alphabet letters only and will be no longer than 15 characters. No two friends have the same name. The group label is an integer between 0 and M - 1. After the last test case, there is a single line `0 0' that terminates the input.
Output
For each test case, output a line containing a single integer, the size of the largest contact group.
Sample Input
3 2
John 0 1
Rose 1
Mary 1
5 4
ACM 1 2 3
ICPC 0 1
Asian 0 2 3
Regional 1 2
ShangHai 0 2
0 0
Sample Output
2 2
Source
Shanghai 2004
题意:
N个人,M个组。问使每组人最少的匹配方式。二分容量得正确答案。
建图,源连人,容量为1;人连组,根据读入,容量为1;组连汇,容量二分。
读入很二啊,不知道一个人能连多少组,用istringstream处理很方便。
二分网络流,每次都需要重新构图,尤其是像这题每次都需要重新改边容量的。(尽管只是一部分边的)略二啊。。。
题意:
N个人,M个组。问使每组人最少的匹配方式。二分容量得正确答案。
建图,源连人,容量为1;人连组,根据读入,容量为1;组连汇,容量二分。
读入很二啊,不知道一个人能连多少组,用istringstream处理很方便。
二分网络流,每次都需要重新构图,尤其是像这题每次都需要重新改边容量的。(尽管只是一部分边的)略二啊。。。
#include
<
iostream
>
#include < cstdio >
#include < cstring >
#include < sstream >
#include < fstream >
#include < cctype >
#define maxn 2000
#define maxe 1200000
using namespace std;
const int inf = ~ 0u >> 1 ;
struct edge
{
int p,next,val,anti;
edge(){}
edge( int a, int b, int c, int d):p(a),next(b),val(c),anti(d){}
}v[maxn],e[maxe],arc[maxn],path[maxn],e2[maxe],v2[maxn];
int flow[maxn],cnt[maxn],d[maxn],pre[maxn];
int n,tot,tot2;
void init()
{
tot = 0 ;
for ( int i = 0 ;i <= n;i ++ )
{
d[i] = 0 ;
cnt[i] = 0 ;
v[i].next = - 1 ;
}
cnt[ 0 ] = n + 1 ;
}
void add( int p, int q, int val)
{
e[tot] = edge(q,v[p].next,val,tot + 1 );
v[p].next = tot ++ ;
e[tot] = edge(p,v[q].next, 0 ,tot - 1 );
v[q].next = tot ++ ;
} /*
void adde(int p,int q,int val)
{
add(p,q,val);
add(q,p,0);
} */
int mflow( int s, int t)
{
// cout << inf << endl;
int k,least,loc,i,now,total;
bool flag;
for ( int i = 0 ;i <= n;i ++ )
arc[i].next = v[i].next;
for (i = s,total = 0 ,now = inf;d[s] < n + 1 ;)
{
flow[i] = now;
for (flag = false ,k = arc[i].next; ~ k;k = e[k].next)
{
if (e[k].val > 0 && d[i] == d[e[k].p] + 1 )
{
now = min(e[k].val,now);
pre[e[k].p] = i;
path[e[k].p].next = k;
arc[i].next = k;
i = e[k].p;
if (i == t)
{
for (total += now;i != s;i = pre[i])
{
e[path[i].next].val -= now;
e[e[path[i].next].anti].val += now;
}
now = inf;
}
flag = true ;
break ;
}
}
if ( ! flag)
{
for (least = n,k = v[i].next; ~ k;k = e[k].next)
{
if (e[k].val && d[e[k].p] < least)
{
loc = k;
least = d[e[k].p];
}
}
arc[i].next = loc;
cnt[d[i]] -- ;
if (cnt[d[i]] == 0 )
break ;
d[i] = least + 1 ;
cnt[d[i]] ++ ;
if (i != s)
{
i = pre[i];
now = flow[i];
}
}
}
return total;
}
char str[ 10005 ];
int main()
{
int N,M;
while (scanf( " %d %d " , & N, & M) == 2 && (N || M))
{
init();
n = N + M + 1 ;
gets(str);
for ( int i = 1 ;i <= N;i ++ )
{
add( 0 ,i, 1 );
gets(str); // cin.getline(str,10000);
// cout << str << endl;
int l = strlen(str);
int j = 0 ;
while (j < l)
{
while (j < l && ! isdigit(str[j]))
j ++ ;
int temp = 0 ;
if (j >= l)
break ;
bool mark = false ;
while (j < l && str[j] != ' ' )
{
// cout << j << endl;
mark = true ;
temp = temp * 10 + str[j] - ' 0 ' ;
j ++ ;
}
if (mark)
add(i,temp + N + 1 , 1 );
}
/* istringstream in(str);
in >> str;
int p;
//cout << str << endl;
while(in >> p)
{
add(i,p + N + 1,1);
//cout << p << ' ';
} */
// cout << endl;
}
for ( int i = 0 ;i < M;i ++ )
add(i + N + 1 ,n,N);
for ( int i = 0 ;i < tot;i ++ )
e2[i] = e[i];
for ( int i = 0 ;i <= n;i ++ )
v2[i] = v[i];
int lo = 1 ,hi = N;
tot2 = tot;
while (lo < hi)
{
int mid = (lo + hi) >> 1 ;
init();
tot = tot2;
for ( int i = 0 ;i < tot;i ++ )
e[i] = e2[i];
for ( int i = 0 ;i <= n;i ++ )
v[i] = v2[i];
for ( int k = v[n].next; ~ k;k = e[k].next)
{
if (e[k].val == 0 )
e[k - 1 ].val = mid;
}
int ans = mflow( 0 ,n);
// cout << ans << endl;
if (ans < N)
lo = mid + 1 ;
else
hi = mid;
}
printf( " %d\n " ,(lo + hi) / 2 );
}
}
#include < cstdio >
#include < cstring >
#include < sstream >
#include < fstream >
#include < cctype >
#define maxn 2000
#define maxe 1200000
using namespace std;
const int inf = ~ 0u >> 1 ;
struct edge
{
int p,next,val,anti;
edge(){}
edge( int a, int b, int c, int d):p(a),next(b),val(c),anti(d){}
}v[maxn],e[maxe],arc[maxn],path[maxn],e2[maxe],v2[maxn];
int flow[maxn],cnt[maxn],d[maxn],pre[maxn];
int n,tot,tot2;
void init()
{
tot = 0 ;
for ( int i = 0 ;i <= n;i ++ )
{
d[i] = 0 ;
cnt[i] = 0 ;
v[i].next = - 1 ;
}
cnt[ 0 ] = n + 1 ;
}
void add( int p, int q, int val)
{
e[tot] = edge(q,v[p].next,val,tot + 1 );
v[p].next = tot ++ ;
e[tot] = edge(p,v[q].next, 0 ,tot - 1 );
v[q].next = tot ++ ;
} /*
void adde(int p,int q,int val)
{
add(p,q,val);
add(q,p,0);
} */
int mflow( int s, int t)
{
// cout << inf << endl;
int k,least,loc,i,now,total;
bool flag;
for ( int i = 0 ;i <= n;i ++ )
arc[i].next = v[i].next;
for (i = s,total = 0 ,now = inf;d[s] < n + 1 ;)
{
flow[i] = now;
for (flag = false ,k = arc[i].next; ~ k;k = e[k].next)
{
if (e[k].val > 0 && d[i] == d[e[k].p] + 1 )
{
now = min(e[k].val,now);
pre[e[k].p] = i;
path[e[k].p].next = k;
arc[i].next = k;
i = e[k].p;
if (i == t)
{
for (total += now;i != s;i = pre[i])
{
e[path[i].next].val -= now;
e[e[path[i].next].anti].val += now;
}
now = inf;
}
flag = true ;
break ;
}
}
if ( ! flag)
{
for (least = n,k = v[i].next; ~ k;k = e[k].next)
{
if (e[k].val && d[e[k].p] < least)
{
loc = k;
least = d[e[k].p];
}
}
arc[i].next = loc;
cnt[d[i]] -- ;
if (cnt[d[i]] == 0 )
break ;
d[i] = least + 1 ;
cnt[d[i]] ++ ;
if (i != s)
{
i = pre[i];
now = flow[i];
}
}
}
return total;
}
char str[ 10005 ];
int main()
{
int N,M;
while (scanf( " %d %d " , & N, & M) == 2 && (N || M))
{
init();
n = N + M + 1 ;
gets(str);
for ( int i = 1 ;i <= N;i ++ )
{
add( 0 ,i, 1 );
gets(str); // cin.getline(str,10000);
// cout << str << endl;
int l = strlen(str);
int j = 0 ;
while (j < l)
{
while (j < l && ! isdigit(str[j]))
j ++ ;
int temp = 0 ;
if (j >= l)
break ;
bool mark = false ;
while (j < l && str[j] != ' ' )
{
// cout << j << endl;
mark = true ;
temp = temp * 10 + str[j] - ' 0 ' ;
j ++ ;
}
if (mark)
add(i,temp + N + 1 , 1 );
}
/* istringstream in(str);
in >> str;
int p;
//cout << str << endl;
while(in >> p)
{
add(i,p + N + 1,1);
//cout << p << ' ';
} */
// cout << endl;
}
for ( int i = 0 ;i < M;i ++ )
add(i + N + 1 ,n,N);
for ( int i = 0 ;i < tot;i ++ )
e2[i] = e[i];
for ( int i = 0 ;i <= n;i ++ )
v2[i] = v[i];
int lo = 1 ,hi = N;
tot2 = tot;
while (lo < hi)
{
int mid = (lo + hi) >> 1 ;
init();
tot = tot2;
for ( int i = 0 ;i < tot;i ++ )
e[i] = e2[i];
for ( int i = 0 ;i <= n;i ++ )
v[i] = v2[i];
for ( int k = v[n].next; ~ k;k = e[k].next)
{
if (e[k].val == 0 )
e[k - 1 ].val = mid;
}
int ans = mflow( 0 ,n);
// cout << ans << endl;
if (ans < N)
lo = mid + 1 ;
else
hi = mid;
}
printf( " %d\n " ,(lo + hi) / 2 );
}
}