zoj 1053

来源:互联网 发布:国内顶级域名注册证书 编辑:程序博客网 时间:2024/05/20 06:07

WA的很惨,去楼下玩了难得一遇的 “水上乐园” 以后,状态大好,速度过了。。大笑然后发现作业还木有做。。


/*zoj_1053    最短路floyd过的。。再次悲剧的看错题目了,以为通过的路径只能是由每个block最后的一行的数据组成的。。所以特地标记最后一行。。然后floyd再特判。。搞得很复杂然后wa了N次。。还有一个不太明白的是直接输出 printf( "Org     Dest    Time    Path\n" );是会PE的呢。难道是\t在不同的计算机上是不一样的?还有一个就是起点和终点相同时避免重复输出。。过了的时候再回看这道题,其实就是floyd加个路径记录和输出。。居然被自己搞得那么复杂。。*/#include <iostream>#include <iomanip>#include <limits.h>#include <string.h>#include <algorithm>#include <cstdio>#define inf 100000using namespace std;int map[30][30];int dist[30][30][30];int rec[30][30];struct node{    int x,y;}way[30];int n,fire;void inint(){    int i,j;    for( i=0;i<=n;i++ )        for( j=0;j<=n;j++ )            map[i][j]=( i==j?0:inf );}bool cmp( node a,node b ){    return dist[a.x][a.y][n]<dist[b.x][b.y][n];}int deal( char *c,int size ){    int i,temp,first,k;    temp=0;     first=1;    k=0;    for( i=0;i<size;i++ )    {        if( c[i]>='0' && c[i]<='9' )        {            temp=temp*10+c[i]-'0';        }        else if( temp==0 )  continue;        else        {            if( first==1 )  fire=temp;            else            {                way[k].x=temp;                way[k++].y=fire;            }            temp=0;            first++;        }    }    if( temp!=0 )    {        way[k].x=temp;        way[k++].y=fire;    }    return k;}void floyd(){    int i,j,k;    for( i=0;i<=n;i++ )        for( j=0;j<=n;j++ )            dist[i][j][0]=map[i][j],rec[i][j]=0;    for( k=1;k<=n;k++ )        for( i=1;i<=n;i++ )            for( j=1;j<=n;j++ )            {                dist[i][j][k]=dist[i][j][k-1];                if( dist[i][j][k]>dist[i][k][k-1]+dist[k][j][k-1] )                {                    dist[i][j][k]=dist[i][k][k-1]+dist[k][j][k-1];                    rec[i][j]=k;                }            }}void output( int i,int j ){    if( i==j )  return;    if( rec[i][j]==0 )    {        printf( "\t%d",j );        return;    }    else    {        output( i,rec[i][j] );        output( rec[i][j],j );    }}int main(){    //freopen( "a.txt","r",stdin );    int T,i,j,k,temp;    int x,y,first;    char c[1000];    scanf( "%d",&T );    first=1;    while( T-- )    {        scanf( "%d",&n );        inint();        for( i=1;i<=n;i++ )            for( j=1;j<=n;j++ )            {                scanf( "%d",&temp );                if( temp!=-1 )                    map[i][j]=temp;            }        getchar();  gets( c );        k=deal( c,strlen(c) );        floyd();        sort( way,way+k,cmp );        if( first!=1 )  printf( "\n" );        printf( "Org\tDest\tTime\tPath\n" );        for( i=0;i<k;i++ )        {            x=way[i].x;     y=way[i].y;            printf( "%d\t%d\t%d\t%d",x,y,dist[x][y][n],x );            if( x!=y )  output( x,y );            printf( "\n" );        }        first++;    }    return 0;}