POJ 2570 Fiber Network

來源:互聯網
上載者:User

標籤:floyd   最短路變形   

最短路變形。


題意是說不同的點之間有不同的公司建立了不同串連。


詢問 A,B之間如果存在通路,有那些公司。


我用bool  g[][][26] 來表示26個字母。然後Floyd, G++就逾時。C++ 就AC了。


然後看別人代碼才知道還有位元運算……ORZ。。。


自己的代碼:C++ AC。813ms

#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6#define LL long longusing namespace std;bool g[201][201][26];int n;void Flyod(){    for(int k=1;k<=n;k++)    {        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                for(int l=0;l<26;l++)                {                    if(g[i][k][l]&&g[k][j][l])                        g[i][j][l]=1;                }            }        }    }}int main(){    while(scanf("%d",&n),n)    {        int u,v;        char tmp[26];        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)        memset(g[i][j],0,sizeof(g[i][j]));        while(scanf("%d%d",&u,&v))        {            if(u==0&&v==0)break;            scanf("%s",tmp);            for(int i=0;tmp[i]!='\0';i++)            {                g[u][v][tmp[i]-'a']=1;            }        }        Flyod();        while(scanf("%d%d",&u,&v))        {            if(u==0&&v==0)break;            bool flag=0;            for(int k=0;k<26;k++)            {                if(g[u][v][k])                {                    printf("%c",k+'a');                    flag=1;                }            }            if(!flag)                puts("-");            else                puts("");        }        puts("");    }}


後面寫的 位元運算的代碼:G++ 63ms

#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6#define LL long longusing namespace std;int m[201][201];int n;int main( ){    int A, B;    char str[27];    char ch;    while( scanf("%d", &n) && n )    {        memset( m, 0, sizeof(m) );        while( scanf( "%d %d", &A, &B ) )        {            if( A == 0 && B == 0 ) break;            scanf( "%s", str );            for(int i=0; str[i]; i++ )            {                m[A][B] |= 1 << (str[i] - 'a');            }        }        int i,j,k;        for( k=1; k<=n; k++ )        {            for( i=1; i<=n; i++ )            {                for( j=1; j<=n; j++ )                {                    m[i][j] |= m[i][k] & m[k][j];                }            }        }        while( scanf( "%d %d", &A, &B ) )        {            if( A == 0 && B == 0 ) break;            for( ch='a'; ch<='z'; ch++ )            {                if( m[A][B] & (1 << ch-'a') )                    putchar( ch );            }            if( !m[A][B] ) putchar( '-' );            puts("");        }        puts("");    }    return 0;}



POJ 2570 Fiber Network

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.