题目地址:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=144
题目描述:
Firetruck |
The Center City fire department collaborates with the transportation department to maintain maps of the city which reflects the current status of the city streets. On any given day, several streets are closed for repairs or construction. Firefighters need to be able to select routes from the firestations to fires that do not use closed streets.
Central City is divided into non-overlapping fire districts, each containing a single firestation. When a fire is reported, a central dispatcher alerts the firestation of the district where the fire is located and gives a list of possible routes from the firestation to the fire. You must write a program that the central dispatcher can use to generate routes from the district firestations to the fires.
The city has a separate map for each fire district. Streetcorners of each map are identified by positive integers less than 21, with the firestation always on corner #1. The input file contains several test cases representing different fires in different districts.
For each test case, your output must identify the case by number (CASE #1, CASE #2, etc). It must list each route on a separate line, with the streetcorners written in the order in which they appear on the route. And it must give the total number routes from firestation to the fire. Include only routes which do not pass through any streetcorner more than once. (For obvious reasons, the fire department doesn't want its trucks driving around in circles.)
Output from separate cases must appear on separate lines.
The following sample input and corresponding correct output represents two test cases.
6 1 2 1 3 3 4 3 5 4 6 5 6 2 3 2 4 0 0 4 2 3 3 4 5 1 1 6 7 8 8 9 2 5 5 7 3 1 1 8 4 6 6 9 0 0
CASE 1: 1 2 3 4 6 1 2 3 5 6 1 2 4 3 5 6 1 2 4 6 1 3 2 4 6 1 3 4 6 1 3 5 6 There are 7 routes from the firestation to streetcorner 6. CASE 2: 1 3 2 5 7 8 9 6 4 1 3 4 1 5 2 3 4 1 5 7 8 9 6 4 1 6 4 1 6 9 8 7 5 2 3 4 1 8 7 5 2 3 4 1 8 9 6 4 There are 8 routes from the firestation to streetcorner 4.
题意:
给定一个无向图,求出能从起点出发到终点的不同路径。
题解:
刚开始直接的用简单的DFS超时。综合看了解题报告,超时的原因有这样几点:1、起点与终点没有相连,即存在有0条路径的情况。2、起点与终点相连,但与一个稠密图相连,但该稠密图有不能到达终点,这样也很容易超时。
所以深搜前,我们要确定我们搜到的这个点对我们是有用的,即这个点是能够到达终点的(与终点连通的点),不能够到达终点的我们剪枝掉,从而节省时间的开销。
三种解法找到“有用”点:
一、利用逆向深搜,即正向深搜前,先在终点逆向深搜,标记出与终点连通的点,然后正向深搜时只搜索标记过的点,其他的点剪枝掉。
二、利用并查集合并图中连通的点,然后与终点在同一个集合里的,我们都标记上,然后再正向搜索标记点。
三、求无向图边连通分量的tarjan算法,利用tarjan算法求连通分量,对于有终点的连通分量的顶点我们都标记,然后再正向搜索标记点。
代码:
说明:代码中包含了全部3种解法,MainProc()对应第一种 MainProc1()对应第二种 MainProc2()对应第三种 只要在main函数运行相应的MainProc系列函数即可。
/*
two DFSs and the answer should be sorted
we have three strategy to flag the node that connected to the aim node
1.DFS the aim node and flag the nodes
2.use union find sets flag the nodes
3.use tarjan algorithm to flag the nodes
*/
#include
#include
#include
#include
#include
#include
using namespace std;
bool Graph[21+5][21+5]={false};//begin with 1
bool Vis[21+5]={false};//begin with 1
int Cases=0;
int Start=0;
int End=0;
int LineNum=0;
int Route[21+5]={0};//begin with 0
int Rtop=0;
int MaxRoute=0;
int FlagAim[21+5]={0};//1 means the i point can be reached to the aim point,0 means it is not reachable for the aim point
//begin with 1
/*DFS the route*/
int DFS(int cur)
{
if(cur==End)
{
LineNum++;
Route[Rtop]=cur;
Vis[cur]=true;
Rtop++;
//output the result
printf("1");
int i=0;
for(i=1;i<=Rtop-1;i++)
{
printf(" %d",Route[i] );
}
printf("\n");
Rtop--;
Vis[cur]=false;
}
else
{
int i=0;
for(i=1;i<=MaxRoute;i++)
{
if(Graph[cur][i]&&!Vis[i]&&FlagAim[i]==1)
{
Route[Rtop]=cur;
Vis[cur]=true;
Rtop++;
DFS(i);
Rtop--;
Vis[cur]=false;
}
}
}
return(0);
}
/*DFS the aim point reachable*/
int DFSAim(int cur)
{
int i=0;
for(i=1;i<=MaxRoute;i++)
{
if(Graph[cur][i]&&!Vis[i])
{
Vis[i]=true;
FlagAim[i]=1;//flag the point or node
DFSAim(i);
Vis[i]=false;
}
}
return(0);
}
/*for test*/
int test()
{
return(0);
}
/*main process use 1.strategy DFS the nodes that connected to the aim node*/
int MainProc()
{
while(scanf("%d",&End)!=EOF&&End>0)
{
Cases++;
int v1=0,v2=0;//v1 to v2
memset(Graph,false,sizeof(Graph));
memset(Vis,false,sizeof(Vis));
Start=1;
LineNum=0;
Rtop=0;
MaxRoute=0;
while(scanf("%d%d",&v1,&v2)!=EOF&&(v1+v2)>0)
{
Graph[v1][v2]=true;
Graph[v2][v1]=true;
if(MaxRoute0)
{
Cases++;
int v1=0,v2=0;//v1 to v2
memset(Graph,false,sizeof(Graph));
memset(Vis,false,sizeof(Vis));
Start=1;
LineNum=0;
Rtop=0;
MaxRoute=0;
while(scanf("%d%d",&v1,&v2)!=EOF&&(v1+v2)>0)
{
Graph[v1][v2]=true;
Graph[v2][v1]=true;
if(MaxRoute0)
{
Cases++;
int v1=0,v2=0;//v1 to v2
memset(Graph,false,sizeof(Graph));
memset(Vis,false,sizeof(Vis));
Start=1;
LineNum=0;
Rtop=0;
MaxRoute=0;
while(scanf("%d%d",&v1,&v2)!=EOF&&(v1+v2)>0)
{
Graph[v1][v2]=true;
Graph[v2][v1]=true;
if(MaxRoute0)
{
TarTop--;
int TopNode=TarStack[TarTop];
FlagAim[TopNode]=1;
if(TopNode==End)
{
connectflag=1;
}
}
if(!connectflag)
{
printf("CASE %d:\n",Cases );
printf("There are %d routes from the firestation to streetcorner %d.\n",LineNum,End );
continue;
}
printf("CASE %d:\n",Cases );
memset(Vis,false,sizeof(Vis));
DFS(1);
printf("There are %d routes from the firestation to streetcorner %d.\n",LineNum,End );
}
return(0);
}
int main(int argc, char const *argv[])
{
/* code */
//MainProc();
//MainProc1();
MainProc2();
return 0;
}