POJ 1422 & HDU 1151 & ZOJ 1525 Air Raid - POJ 2060 & ZOJ 2221 Taxi Cab Scheme【二分图最大匹配 -- 最小路径覆盖】
Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's streets form no cycles.
With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.
Input
Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:
no_of_intersections
no_of_streets
S1 E1
S2 E2
......
Sno_of_streets Eno_of_streets
The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk <= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.
There are no blank lines between consecutive sets of data. Input data are correct.
Output
The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town.
Sample Input
2
4
3
3 4
1 3
2 3
3
3
1 3
1 2
2 3
Sample Output
2
1
有向无环图的最小路径覆盖可以转化为二分图的最大匹配模型。
非DAG的对应问题貌似是NP问题,因为这个只能转化成一般图,不能体现二分图的特殊性质。
将原图中的点拆为两倍,pi 拆为 pi1 和 pi2。
对于原图中每条有向边i-j,在二分图中连为pi1-pj2。
由于这样的结构,就实现了,在找交错路的过程中,不会出现两个点同时指向一个点的情况(违背匹配原则),即二分图最大匹配的性质保证了找出来的交错路们在原图中所构成的一定是一条或几条无分支链状路径。
所以覆盖全图需要的最小路径数即为点数|P|-匹配数|M|。
附上Hungary代码:
#include < cstdio >
using namespace std;
#include < cstring >
#define maxn 150
bool map[maxn][maxn],vis[maxn];
int mac[maxn];
int n,m;
bool find( int x)
{
for ( int i = 1 ;i <= n;i ++ )
{
if (map[x][i] && ! vis[i])
{
vis[i] = true ;
if ( ! mac[i] || find(mac[i]))
{
mac[i] = x;
return true ;
}
}
}
return false ;
}
void solve()
{
memset(map, 0 , sizeof (map));
memset(mac, 0 , sizeof (mac));
scanf( " %d %d " , & n, & m);
for ( int i = 0 ;i < m;i ++ )
{
int a,b;
scanf( " %d %d " , & a, & b);
map[a][b] = 1 ;
}
int ans = 0 ;
for ( int i = 1 ;i <= n;i ++ )
{
memset(vis, false , sizeof (vis));
if (find(i))
ans ++ ;
}
// cout << ans << endl;
printf( " %d\n " ,n - ans);
}
int main()
{
int t;
scanf( " %d " , & t);
for ( int i = 0 ;i < t;i ++ )
solve();
}
附上MaxFlow代码:
#include < cstdio >
#include < cstring >
using namespace std;
#define maxn 250
#define maxe 30000
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],path[maxn],arc[maxn];
int n,N,m,tot,flow[maxn],pre[maxn],cnt[maxn],d[maxn];
void init()
{
tot = 0 ;
memset(cnt, 0 , sizeof (cnt));
memset(d, 0 , sizeof (cnt));
for ( int i = 0 ;i <= n;i ++ )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 ++ ;
}
int mflow( int s, int t)
{
int i,k,least,loc,now,total;
bool flag;
for ( int i = 0 ;i <= n;i ++ )arc[i].next = v[i].next;
for (total = 0 ,i = s,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 && d[i] == d[e[k].p] + 1 )
{
now = min(e[k].val,now);
path[e[k].p].next = k;
arc[i].next = k;
pre[e[k].p] = i;
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 && 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;
}
void solve()
{
scanf( " %d %d " , & N, & m);
n = 2 * N + 1 ;
init();
for ( int i = 1 ;i <= N;i ++ )
{
add( 0 ,i, 1 );
add(i + N,n, 1 );
}
for ( int i = 0 ;i < m;i ++ )
{
int p,q;
scanf( " %d %d " , & p, & q);
add(p,q + N, 1 );
}
int ans = mflow( 0 ,n);
// cout << ans << endl;
printf( " %d\n " ,N - ans);
}
int main()
{
int t;
scanf( " %d " , & t);
for ( int i = 0 ;i < t;i ++ )solve();
}
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3964 | Accepted: 1648 |
Description
For the sake of simplicity, we model a city as a rectangular grid. An address in the city is denoted by two integers: the street and avenue number. The time needed to get from the address a, b to c, d by taxi is |a - c| + |b - d| minutes. A cab may carry out a booked ride if it is its first ride of the day, or if it can get to the source address of the new ride from its latest,at least one minute before the new ride's scheduled departure. Note that some rides may end after midnight.
Input
Output
Sample Input
2
2
08:00 10 11 9 16
08:07 9 16 10 11
2
08:00 10 11 9 16
08:06 9 16 10 11
Sample Output
1 2
Source
同样是一道最小路径覆盖基本题。
对于每个任务,通过输入计算出它的start、end的时间和坐标,然后就能根据,第i个任务的start时间 - 第j个任务的完成时间 - (第j个任务的end坐标和第i个任务的start坐标之间的距离) > 0 来判断是否给i -> j 之间加边了。
加完边就是最小路径覆盖 = n - 二分图最大匹配。
代码:
#include < cstdio >
#include < cstring >
using namespace std;
#define maxn 505
bool map[maxn][maxn],vis[maxn];
int mac[maxn],n;
struct point
{
int x,y;
point(){}
point( int _x, int _y):x(_x),y(_y){}
};
int fab( int x)
{
return (x > 0 ) ? x : - x;
}
int dis( const point & a, const point & b)
{
return fab(a.x - b.x) + fab(a.y - b.y);
}
struct node
{
point s,t;
int a,b;
node(){}
node( int x,point _s,point _t)
{
s = _s;
t = _t;
a = x;
int len = dis(s,t);
b = a + len;
// printf("start:%d\n",a);
// printf("end:%d\n",b);
}
}st[maxn];
bool find( int x)
{
for ( int i = 1 ;i <= n;i ++ )
{
if (map[x][i] && ! vis[i])
{
vis[i] = true ;
if ( ! mac[i] || find(mac[i]))
{
mac[i] = x;
return true ;
}
}
}
return false ;
}
void solve()
{
scanf( " %d " , & n);
for ( int i = 1 ;i <= n;i ++ )
{
int a,b;
int in [ 4 ];
scanf( " %d:%d " , & a, & b);
int temp = a * 60 + b;
for ( int j = 0 ;j < 4 ;j ++ )
scanf( " %d " , & in [j]);
st[i] = node(temp,point( in [ 0 ], in [ 1 ]),point( in [ 2 ], in [ 3 ]));
}
memset(map, 0 , sizeof (map));
memset(mac, 0 , sizeof (mac));
for ( int i = 1 ;i <= n;i ++ )
{
for ( int j = 1 ;j <= n;j ++ )
{
if (st[j].a > st[i].b + dis(st[i].t,st[j].s))
map[i][j] = 1 ;
}
}
int ans = 0 ;
for ( int i = 1 ;i <= n;i ++ )
{
memset(vis, 0 , sizeof (vis));
if (find(i))
ans ++ ;
}
printf( " %d\n " ,n - ans);
}
int main()
{
int t;
scanf( " %d " , & t);
for ( int i = 0 ;i < t;i ++ )solve();
}