uva 208 Firetruck

本文介绍了一种优化后的路径搜索算法,通过剪枝技术减少无效路径的搜索,提高搜索效率。文章详细解释了如何利用并查集判断连通性和剪枝过程,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题:
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 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.
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.

中文:
给你一个无向图和n,起点是1,终点是n。现在让你把所有从起点到终点的不同路径都输出出来。节点数最多不会超过21个,输出的路径要按照数字的大小的字典序的顺序输出。

#include<bits/stdc++.h>
using namespace std;
int n,ind;
bool vis[30];
int father[30],Rank[30];
vector<int> G[30];
vector<vector<int> > ans;
vector<int> road;
int mark[500][2];
void ini()
{
    for(int i=1;i<30;i++)
    G[i].clear();
    memset(vis,0,sizeof(vis));
    memset(Rank,0,sizeof(Rank));
    ans.clear();
    road.clear();
    for(int i=1;i<30;i++)
    father[i]=i;
    ind=1;
}
int cmp(const vector<int> &a,const vector<int> &b)
{
    for(int i=0;i<min(a.size(),b.size());i++)
    {
        if(a[i]!=b[i])
        return a[i]<b[i];
    }
    return a.size()<b.size();
}
int Find(int x)
{
    if(father[x]==x)
    return x;
    else
    return father[x]=Find(father[x]);
}
void merge(int a,int b)
{
    int x=Find(a);
    int y=Find(b);
    if(x!=y)
    {
        if(Rank[x]<Rank[y])
        father[x]=y;
        else
        father[y]=x;
        if(Rank[x]==Rank[y])
        Rank[x]++;
    }
}
void dfs(int x)
{
    if(x==n)
    {
        ans.push_back(road);
        return;
    }
    for(int i=0;i<G[x].size();i++)
    {
        if(!vis[G[x][i]])
        {
            vis[G[x][i]]=true;
            road.push_back(G[x][i]);
            dfs(G[x][i]);
            vis[G[x][i]]=false;
            road.pop_back();
        }
    }
}
void prune()//剪枝,把所有能连接到起点和终点的节点全找出来,从新建图
{
    vector<int> one;
    for(int i=1;i<ind;i++)
    {
        if(mark[i][0]==1||mark[i][1]==1)
        {
            if(mark[i][0]==1)
            one.push_back(mark[i][1]);
            if(mark[i][1]==1)
            one.push_back(mark[i][0]);
        }
        else
        merge(mark[i][0],mark[i][1]);
    }
    for(int i=1;i<30;i++)
    father[i]=Find(father[i]);
    int aim=Find(n);
    for(int i=0;i<one.size();i++)
    {
        if(Find(one[i])==aim)
        {
            G[1].push_back(one[i]);
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int k=1;
    while(cin>>n)
    {
        ini();
        int a,b;
        while(cin>>a>>b,a+b)
        {
            mark[ind][0]=a;
            mark[ind][1]=b;
            if(a!=1&&b!=1)
            {
                G[a].push_back(b);
                G[b].push_back(a);
            }
            ind++;
        }
        prune();
        vis[1]=true;
        dfs(1);
        cout<<"CASE "<<k++<<':'<<endl;
        sort(ans.begin(),ans.end(),cmp);
        for(int i=0;i<ans.size();i++)
        {
            cout<<1;
            for(int j=0;j<ans[i].size();j++)
            cout<<" "<<ans[i][j];
            cout<<endl;
        }
        cout<<"There are "<<ans.size()<<" routes from the firestation to streetcorner "<<n<<'.'<<endl;
    }

    return 0;
}

解答:

上来直接暴力搜索路径超时了,考虑剪枝优化。
如下图所示,起点是1,终点是7。那么只需要枚举1 3 5 7这几个节点的路径即可。
这里写图片描述

那么如何操作呢?
首先,起点一定是能路过一次某些节点能够到达终点位置,如图中1走向2就不可能再走向7(所有路径只走一次)。
这里使用并查集来来判断连通性(也可以用floyed)

1.首先,把起点扣掉,也就是节点1。
2.接下来使用并查集操作,判断除了节点1以外,有多少个连通分量?如上图所示,去掉节点1以后有两个连同分量,分别是2 4 6和3 5 7。当然,如果使用并查集得到的连同分量只有一个,那说明这个图就是一个连同分量,那么1节点可能是能遍历到所有节点并走向终点的。
3.判断终点在哪个连同分量当中,剩下的连通分量都丢弃。如图当中,终点7在右边的连通分量当中,那么2 4 6就可以不要了。
4.把起点1加上右边的连同分量重新建图,同邻接表表示,起点1到2只用单向的1到2的边即可,因为不可能从2回溯到1去-_-

如何用并查集判断连通分量的个数呢?并查集操作图关系以后会得到n个子树,有多少个子树就有多少个连通分量了。

最后注意,输出顺序要用字典序!这里的字典序不要把数字转换成字符串,直接用数字比较大小的方式,否则下面的样例输出会得到错误的结果。
14
1 8
2 11
3 4
3 6
4 14
5 6
5 8
6 11
6 12
8 14
9 14
10 14
11 14
0 0

CASE 1:
1 8 5 6 3 4 14
1 8 5 6 11 14
1 8 14
There are 3 routes from the firestation to streetcorner 14.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值