//Problem link >>http://uva.onlinejudge.org/external/5/567.html
Graph problem using floyd-warshall ...
#include <bits/stdc++.h>
#define inf 100000
using namespace std ;
int con[24][24] ;
int main ()
{
int n ,cas=1 , line ,m ;
while ( cin>>n && n!=EOF )
{
int i , j ;
for (i=0; i<=20 ; i++)
{
for (j=0; j<=20 ; j++)
{
if (i==j) con[i][j]=0 ;
else con[i][j]=inf ;
}
}
line=1 ;
for (int i=1 ; i<=n ; i++)
{
cin>>m ;
con[m][line]=con[line][m]=1 ;
}
line++ ;
while (line<20)
{
cin>>n ;
for (int j=1 ; j<=n ; j++)
{
int m ;
cin>>m ;
con[m][line]=con[line][m]=1 ;
}
line++ ;
}
int q , k ;
for (k=1 ; k<=20 ; k++)
{
for (i=1; i<=20 ; i++)
{
for (j=1 ; j<=20 ; j++)
{
con[i][j]=min(con[i][j],con[i][k]+con[k][j]) ;
}
}
}
cin>>q ;
cout<<"Test Set #"<<cas++<<endl ;
for (i=1 ; i<=q ; i++)
{
int a , b ;
cin>>a>>b ;
printf ("%2d to %2d: %d\n",a,b,con[a][b]) ;
}
cout<<endl ;
}
return 0 ;
}
Graph problem using floyd-warshall ...
#include <bits/stdc++.h>
#define inf 100000
using namespace std ;
int con[24][24] ;
int main ()
{
int n ,cas=1 , line ,m ;
while ( cin>>n && n!=EOF )
{
int i , j ;
for (i=0; i<=20 ; i++)
{
for (j=0; j<=20 ; j++)
{
if (i==j) con[i][j]=0 ;
else con[i][j]=inf ;
}
}
line=1 ;
for (int i=1 ; i<=n ; i++)
{
cin>>m ;
con[m][line]=con[line][m]=1 ;
}
line++ ;
while (line<20)
{
cin>>n ;
for (int j=1 ; j<=n ; j++)
{
int m ;
cin>>m ;
con[m][line]=con[line][m]=1 ;
}
line++ ;
}
int q , k ;
for (k=1 ; k<=20 ; k++)
{
for (i=1; i<=20 ; i++)
{
for (j=1 ; j<=20 ; j++)
{
con[i][j]=min(con[i][j],con[i][k]+con[k][j]) ;
}
}
}
cin>>q ;
cout<<"Test Set #"<<cas++<<endl ;
for (i=1 ; i<=q ; i++)
{
int a , b ;
cin>>a>>b ;
printf ("%2d to %2d: %d\n",a,b,con[a][b]) ;
}
cout<<endl ;
}
return 0 ;
}
No comments:
Post a Comment