【网络流24题----03】Air Raid最小路径覆盖

Air Raid

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4591    Accepted Submission(s): 3072


Problem Description
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
 

翻译一下就是:

一个城镇中有n个路口和m条单项的路径,图是无环图。

有一些伞兵可以从任意一个路口出发,要求走不相交的路径并到达所有的路口;

我们的任务就是求出最少要几个伞兵。


那么很显然就是最小路径覆盖问题了(什么你不会最小路径覆盖?)

 样例:

4 3

1 3

2 3

3 4

 图一(样例)

图二(转换为二分图)

然后:有向图的最小路径覆盖=V-二分图最大匹配。


 

代码:我依旧跑的是dinic

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<vector>
 7 #include<cstring>
 8 #define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
 9 #define llg long long
10 #define maxn 2000
11 llg j,k,n,m,y,z,bj[maxn],head,tail,dl[maxn],deep[maxn],ans;
12 bool f,ff;
13 using namespace std;
14 vector <llg> a[maxn],v[maxn],ba[maxn];
15 //a[i][j]表示第i个点所指向的第j个点是a[i][j],v[i][j]表示权值(流量),ba[i][j]表示a[i][j]的反xiangbian
16 llg dfs(llg x,llg low)
17 {
18     llg res=0;llg va=0;
19     if (x==n)  {return low;}
20     llg w=a[x].size();
21     for (llg i=0;i<w;i++)
22         if (deep[x]+1==deep[a[x][i]] && v[x][i]>0 && (va=dfs(a[x][i],min(low,v[x][i]))))
23         {
24             v[x][i]-=va; v[a[x][i]][ba[x][i]]+=va; 
25             return va;
26         }
27     return 0;
28 }
29 void fencen()
30 {
31         memset(bj,0,sizeof(bj));
32         tail=1; head=0; dl[1]=0; bj[0]=1;
33         do{
34             head++;
35             llg x=dl[head];
36             llg w=a[x].size();
37             for (llg i=0;i<w;i++)
38                 if (!bj[a[x][i]] && v[x][i]>0)
39                 {
40                     tail++; dl[tail]=a[x][i];
41                     deep[a[x][i]]=deep[x]+1;
42                     bj[a[x][i]]=1;
43                 }
44         }while (head!=tail);
45 }
46 void insert(llg x,llg y,llg z)
47 {
48     a[x].push_back(y); v[x].push_back(z);
49     a[y].push_back(x); v[y].push_back(0);
50     ba[x].push_back(a[y].size()-1); ba[y].push_back(a[x].size()-1);
51 }
52 int main()
53 {
54     yyj("a");
55     cin>>n>>m; deep[0]=1;
56     for (llg i=1;i<=m;i++)
57     {
58         llg x;
59         cin>>x>>y;
60         insert(x*2-1,y*2,1); insert(x*2,y*2-1,0);
61     }
62     for (llg i=1;i<=n;i++)
63     {
64         insert(0,i*2-1,1); insert(i*2-1,0,0);
65         insert(i*2,n*2+1,1); insert(n*2+1,i*2,0);
66     }
67     llg total=n;
68     n=n*2+1;
69     while (1)
70     {
71         f=true; ff=false;
72         fencen();
73         if (!bj[n]) break;
74         ans+=dfs(0,0x7fffffff);
75     }
76     cout<<total-ans<<endl;
77     return 0;
78 }

再说几句:

  还记得题干中打了红色标记的地方吧!“不相交的路径”

  要是可以相交呢?那我们就要跑一遍floyed闭包传递了。

  什么你搞不清很清粗为什么要这样?(自己去看一看http://www.cnblogs.com/ka200812/archive/2011/07/31/2122641.html)
  图片来自:http://www.cppblog.com/zhangwangcz/archive/2012/03/30/155686.html

 

posted @ 2016-06-21 20:07  №〓→龙光←  阅读(387)  评论(0编辑  收藏  举报