UVA 208(p214)----Firetruck

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int g[100][100],v[100],en,n,a[100],sum,m[100][100];
void floyd()
{
    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                g[i][j]=g[i][j]||(g[i][k]&&g[k][j]);
}
void solve(int u,int num)
{
    if(u==en)
    {
        sum++;
        for(int i=0; i<num-1; i++)
            printf("%d ",a[i]);
        printf("%d\n",a[num-1]);
        return;
    }
    else
        for(int i=1; i<=n; i++)
        {
            if(!v[i]&&g[i][en]&&m[u][i])
            {
                v[i]=true;
                a[num]=i;
                solve(i,num+1);
                a[num]=0;
                v[i]=false;
            }
        }
}
int main()
{
    // freopen("out.out","w",stdout);
    int cas=0,x,y;
    while(scanf("%d",&en)!=EOF)
    {
        sum=0;
        cas++;
        n=0;
        memset(g,0,sizeof(g));
        memset(v,0,sizeof(v));
        memset(a,0,sizeof(a));
        memset(m,0,sizeof(m));
        v[1]=true;
        a[0]=1;
        while(scanf("%d%d",&x,&y)==2&&x)
        {
            // cout<<x<<" flj "<<y<<endl;
            g[x][y]=true;
            g[y][x]=true;
            m[x][y]=true;
            m[y][x]=true;
            n=max(max(n,x),y);
        }
        // cout<<n<<endl;
        floyd();
        printf("CASE %d:\n",cas);
        solve(1,1);
        printf("There are %d routes from the firestation to streetcorner %d.\n",sum,en);
    }
    return 0;
}
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=144

你可能感兴趣的:(UVA 208(p214)----Firetruck)