网络流问题 poj 1422 hdu 1151

来源:互联网 发布:布鲁斯口琴调音软件 编辑:程序博客网 时间:2024/04/27 19:11
</pre>Problem Description</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">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.</div><div class="panel_bottom" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"> </div><p style="margin:10px auto; line-height:19px; font-size:13px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; background-color:rgb(40,85,126)"> </p><div class="panel_title" align="left" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">Input</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">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_intersectionsno_of_streetsS1 E1S2 E2......Sno_of_streets Eno_of_streetsThe 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.</div><div class="panel_bottom" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"> </div><p style="margin:10px auto; line-height:19px; font-size:13px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; background-color:rgb(40,85,126)"> </p><div class="panel_title" align="left" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">Output</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">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.</div><div class="panel_bottom" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"> </div><p style="margin:10px auto; line-height:19px; font-size:13px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; background-color:rgb(40,85,126)"> </p><div class="panel_title" align="left" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">Sample Input</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"><div style="font-family:'Courier New',Courier,monospace">2 4 3 3 4 1 3 2 3 3 3 1 3 1 2 2 3</div></div><div class="panel_bottom" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"> </div><p style="margin:10px auto; line-height:19px; font-size:13px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; background-color:rgb(40,85,126)"> </p><div class="panel_title" align="left" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">Sample Output</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"><div style="font-family:'Courier New',Courier,monospace">2 1</div></div><div class="panel_bottom" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"> </div><p style="margin:10px auto; line-height:19px; font-size:13px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; background-color:rgb(40,85,126)"> </p><div class="panel_title" align="left" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">Source</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"><a target=_blank target="_blank" href="http://acm.hdu.edu.cn/search.php?field=problem&key=Asia%202002,%20Dhaka%20(Bengal)&source=1&searchmode=source" style="color:rgb(51,102,153); text-decoration:none; background-color:transparent">Asia 2002, Dhaka (Bengal)</a></div><div class="panel_bottom" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"> </div><p style="margin:10px auto; line-height:19px; font-size:13px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; background-color:rgb(40,85,126)"> </p><div class="panel_title" align="left" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">Recommend</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">Ignatius.L</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"> 分析:</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">题意可能不难但是你要看懂题的话,英语还是要比较高的水平的,看懂题意之后,你就会发现这是一个关于最短路径覆盖的题。我参考了几位大神的代码:大概可能呢有三个代码可能比较好吧:</div><div class="panel_content" style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)">第一个<p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px; color:rgb(170,127,114); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(254,254,255); text-indent:2em">在一个有向图上,每一个点上都可以放一个伞兵,问至少放多少个伞兵可以遍历整个图?</p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px; color:rgb(170,127,114); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(254,254,255); text-indent:2em"></p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px; color:rgb(170,127,114); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(254,254,255); text-indent:2em">选题原因:这是一道求二分图最大匹配的变形题,求图的最小路径覆盖,题目难度不大。</p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px; color:rgb(170,127,114); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(254,254,255); text-indent:2em"></p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px; color:rgb(170,127,114); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(254,254,255); text-indent:2em">思路:</p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px; color:rgb(170,127,114); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(254,254,255); text-indent:2em">问题可以看成,在有向图中找到最少的路径,使之能够覆盖所有的顶点,即求最小路径覆盖数。由于最小路径覆盖数=定点数-最大匹配数,因此此题的实质还是求图的最大二分匹配数。</p><pre name="code" class="cpp">#include <stdio.h>#include <string.h>const int MAX=150;int map[MAX][MAX];  //图int vis[MAX];     //访问标志int match[MAX];   //匹配数组int inter,street;  //inter:顶点数  street:边数(街道数)bool Dfs(int u)//寻找增广路径{     for (int v = 1; v <= inter; v++)     {         if (!vis[v] && map[u][v])         {             vis[v] = 1;             if (match[v] == 0 || Dfs(match[v]))             {                  match[v] = u;                  return true;             }         }     }     return false;}int main(){     int t;     int i;     int x, y;     scanf("%d", &t);  //测试数据的组数     while (t--)     {          memset(map, 0, sizeof(map));//各数组清零          memset(match, 0, sizeof(match));          scanf("%d%d", &inter, &street);  //输入点数和边数          for (i = 1; i <= street; i++)          {               scanf("%d%d", &x, &y);               map[x][y] = 1;          }          int sum = 0;//记录最大匹配数          for (i = 1; i <= inter; i++)          {               memset(vis, 0, sizeof(vis));//访问标志数组清零               if (Dfs(i))                    sum++;//若存在增广路径,匹配数加1          }          printf("%d\n", inter - sum);//inter-sum:最小路径覆盖数     }     return 0;}
大神的操作就不用说了,写得很清晰。

第二个:

/*HDU 1151*/#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;/* **************************************************************************//二分图匹配(匈牙利算法的DFS实现)//初始化:g[][]两边顶点的划分情况//建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配//g没有边相连则初始化为0//uN是匹配左边的顶点数,vN是匹配右边的顶点数//调用:res=hungary();输出最大匹配数//优点:适用于稠密图,DFS找增广路,实现简洁易于理解//时间复杂度:O(VE)//***************************************************************************///顶点编号从0开始的const int MAXN=150;int uN,vN;//u,v数目int g[MAXN][MAXN];int linker[MAXN];bool used[MAXN];bool dfs(int u)//从左边开始找增广路径{    int v;    for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改      if(g[u][v]&&!used[v])      {          used[v]=true;          if(linker[v]==-1||dfs(linker[v]))          {//找增广路,反向              linker[v]=u;              return true;          }      }    return false;//这个不要忘了,经常忘记这句}int hungary(){    int res=0;    int u;    memset(linker,-1,sizeof(linker));    for(u=0;u<uN;u++)    {        memset(used,0,sizeof(used));        if(dfs(u)) res++;    }    return res;}//******************************************************************************/int main(){    int k;    int n;    int u,v;    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&k);        memset(g,0,sizeof(g));        while(k--)        {            scanf("%d%d",&u,&v);            u--;            v--;            g[u][v]=1;        }        uN=vN=n;        printf("%d\n",n-hungary());    }    return 0;}

第三个:

/*由题意可知要找出最大的匹配数,必须构建二分图n条街道,即每个数组为1-n,*/#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>#define max 150using namespace std;#define maxn 150int un,vn;bool g[maxn][maxn];//判断是否被用过int xm[maxn],ym[maxn];bool chk[maxn];int n,m;/*  模板:找最大匹配组的模板*/bool searchpath(int u){  int v;  for(v=0;v<vn;v++)//vn=4;  if(g[u][v]&&!chk[v])  {    chk[v]=true;if(ym[v]==-1||searchpath(ym[v])){  ym[v]=u;  xm[u]=v;  return true;}  }  return false;}int maxmatch(){  int u,ret=0;  memset(xm,-1,sizeof(xm));  memset(ym,-1,sizeof(ym));  for(u=0;u<un;u++)  if(xm[u]==-1)  {    memset(chk,false,sizeof(chk));if(searchpath(u))           ret++;  }  return ret;}void input(){  memset(g,0,sizeof(g));  scanf("%d%d",&n,&m);//4 3  un=vn=n;//构建二分图//un=vn=4;  for(int i=0;i<m;i++)  {    int a,b;scanf("%d%d",&a,&b);a--;b--;g[a][b]=true;  }}int main(){  int t;  scanf("%d",&t);  while(t--)  {    input();int ans=n-maxmatch();printf("%d\n",ans);  }  return 0;}

第三个是我仿照写的。

我提供一个代码模板吧:

关于找到增广路径的:

bool Dfs(int u)//寻找增广路径{     for (int v = 1; v <= inter; v++)     {         if (!vis[v] && map[u][v])         {             vis[v] = 1;             if (match[v] == 0 || Dfs(match[v]))             {                  match[v] = u;                  return true;             }         }     }     return false;}


 
0 0
原创粉丝点击