Chieh's Blog

Because Of Coding

ZOJ Problem Set - 1967 Fiber Network

Chieh posted @ 2015年7月27日 17:53 in ZOJ , 291 阅读

飞机票:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=967

题意:给你个有向图,每条有向边有字母标识,然后有查询,每个给你u,v,从u到v路径上出现了那些字母,并且字母必须在每条路径的边上。

分析:假设只有一个字母的话,那么就很简单,因为n不是特别大,如果u到v有字母,那么就标记u可以到v,接着直接flyod跑一边,接着查询,就可以根据flyod跑出来是否联通给出答案。那么因为这里是有26个字母,所以可以跑26次flyod,然后对于每次询问,对26个字母都进行一遍查找,最后输出就好。最后啪啪啪就可以AC了

/*
Author:Chieh
Because Of Coding
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define LL long long
#define INF 1e9
#define EPS 1e-9
using namespace std;
int n;
const int maxn=234;
int ai[27][maxn][maxn];
char ch[27];
void init()
{
    memset(ai,0,sizeof(ai));
    int u,v;
    while(scanf("%d%d",&u,&v)!=EOF&&u)
    {
        scanf("%s",ch+1);
        int len=strlen(ch+1);
        for(int i=1; i<=len; i++)
        {
            ai[ch[i]-'a'+1][u][v]=1;
        }

    }
    for(int l=1; l<=26; l++)
    {
        for(int k=1; k<=n; k++)
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    if(ai[l][i][k]&&ai[l][k][j])ai[l][i][j]=1;
                }
            }
        }
    }
}

void play()
{
    int u,v;
    while(scanf("%d%d",&u,&v)!=EOF&&u)
    {
        bool flag=1;
        for(int i=1; i<=26; i++)
        {
            if(ai[i][u][v])
            {
                printf("%c",'a'+i-1);
                flag=0;

            }
        }
        if(flag)printf("-");
        printf("\n");
    }
    printf("\n");
}
int main()
{

    while(scanf("%d",&n)!=EOF&&n)
    {
        init();
        play();

    }
    //   cout << "Hello world!" << endl;
    return 0;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter