uva 208 Firetruck

题目地址:

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.

Input

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.

  • The first line of a test case consists of a single integer which is the number of the streetcorner closest to the fire.
  • The next several lines consist of pairs of positive integers separated by blanks which are the adjacent streetcorners of open streets. (For example, if the pair 4 7 is on a line in the file, then the street between streetcorners 4 and 7 is open. There are no other streetcorners between 4 and 7 on that section of the street.)
  • The final line of each test case consists of a pair of 0's.

Output

For each test case, your output must identify the case by number (CASE #1CASE #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.

Sample Input

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

Sample Output

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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
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(MaxRoute<v1)
			{
				MaxRoute=v1;
			}
			if(MaxRoute<v2)
			{
				MaxRoute=v2;
			}
		}
		memset(Vis,false,sizeof(Vis));
		memset(FlagAim,0,sizeof(FlagAim));
		DFSAim(End);
		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 fa[21+5]={0};//father array

/*find function*/
int find(int x)
{
	if(fa[x]!=x)
	{
		fa[x]=find(fa[x]);
	}
	return(fa[x]);//all return
}


/*main process use 2.strategy union find sets the nodes that connected to the aim node*/
int MainProc1()
{
	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(MaxRoute<v1)
			{
				MaxRoute=v1;
			}
			if(MaxRoute<v2)
			{
				MaxRoute=v2;
			}
		}
		//union find sets
		memset(FlagAim,0,sizeof(FlagAim));
		int i=0,j=0;
		for(i=1;i<=MaxRoute;i++)
		{
			fa[i]=i;//initialize
		}
		for(i=1;i<=MaxRoute;i++)
		{
			for(j=1;j<=MaxRoute;j++)
			{
				if(Graph[i][j])
				{
					fa[find(i)]=fa[find(j)];//union the sets
				}
			}
		}
		for(i=1;i<=MaxRoute;i++)//judge the end node union sets
		{
			if(fa[find(i)]==fa[find(End)])
			{
				FlagAim[i]=1;
			}
		}

		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 TarStack[21+5]={0};
int TarTop=0;
int dfn[21+5]={0};
int low[21+5]={0};
int indexnum=0;//the dfs number for the node

/*min the number*/
int Min(int a,int b)
{
	/*if(a<b)
	{
		return(a);
	}
	else
	{
		return(b);
	}*/
	return(a<b?a:b);
}

/*tarjan algorithm for the undirected gragh*/
int tarjan(int u,int fa)
{
	TarStack[TarTop++]=u;
	dfn[u]=low[u]=++indexnum;//begin with 1 for u and v and index
	int v=0;
	for(v=1;v<=22;v++)
	{
		if(Graph[u][v]&&v!=fa)
		{
			if(!dfn[v])//(u,v) is search tree edge
			{
				tarjan(v,u);
				low[u]=Min(low[u],low[v]);
				if(dfn[u]<low[v])//edge is <   node is <=, note that the difference between the edge  and node  undirected-gragh-tarjan
				{
					TarTop--;
					int TopNode=TarStack[TarTop];
					while(TopNode!=v)//why this is v not u????
					{
						TarTop--;
						TopNode=TarStack[TarTop];
					}
				}
			}
			else//(u,v) is back edge
			{
				low[u]=Min(low[u],dfn[v]);
			}
		}
	}
	return(0);
}

/*main process use 3 strategy use tarjan algorithm to the undirected and connected gragh to find the 1 and end connected gragh-sets*/
int MainProc2()
{
	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(MaxRoute<v1)
			{
				MaxRoute=v1;
			}
			if(MaxRoute<v2)
			{
				MaxRoute=v2;
			}
		}
		//tarjan algorithm,except the tarjan algorithm block  this  main process  is so likely the interface. we only care about the flagaim[]  then others we do not care about 
		//initialize
		memset(FlagAim,0,sizeof(FlagAim));
		Graph[1][22]=true;
		Graph[22][1]=true;
		Graph[22][End]=true;
		Graph[End][22]=true;
		memset(TarStack,0,sizeof(TarStack));
		TarTop=0;
		memset(dfn,0,sizeof(dfn));
		memset(low,0,sizeof(low));
		indexnum=0;
		tarjan(1,0);//1 is cur  0 is father
		Graph[1][22]=false;
		Graph[22][1]=false;
		Graph[22][End]=false;
		Graph[End][22]=false;
		int connectflag=0;//1 and end  is  whether  connected
		while(TarTop>0)
		{
			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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值