#include<bits/stdc++.h>
#define mx 109
#define inf 1e8
#define nil -999999999
using namespace std;
typedef long long ll;
ll graph[mx][mx];
ll path[mx][mx];
ll edge,node;
int main()
{
//ofstream o("op.txt");
ll i,j,k,l,m,n,idx;
idx=1;
while(1)
{
cin>>node;
if(node==0)
break;
ll start;
cin>>start;
for(i=0;i<mx;i++)
{
for(j=0;j<mx;j++)
{
graph[i][j]=nil;
}
}
for(i=1;i<=mx;i++)
{
graph[i][i]=0;
}
for(i=1;;i++)
{
ll a,b,cost;
cin>>a>>b;
if(a==0&&b==0)
break;
graph[a][b]=1;
}
for(k=1;k<=node;k++)
{
for(i=1;i<=node;i++)
{
for(j=1;j<=node;j++)
{
graph[i][j]=max(graph[i][j],graph[i][k]+graph[k][j]);
}
}
}
ll dis,nde;
dis=-999;
for(i=1;i<=node;i++)
{
if(graph[start][i]>dis)
{
nde=i;
dis=graph[start][i];
}
}
cout<<"Case "<<idx<<": The longest path from "<<start<<" has length "<<dis<<", finishing at "<<nde<<"."<<endl<<endl;
idx++;
}
return 0;
}
#define mx 109
#define inf 1e8
#define nil -999999999
using namespace std;
typedef long long ll;
ll graph[mx][mx];
ll path[mx][mx];
ll edge,node;
int main()
{
//ofstream o("op.txt");
ll i,j,k,l,m,n,idx;
idx=1;
while(1)
{
cin>>node;
if(node==0)
break;
ll start;
cin>>start;
for(i=0;i<mx;i++)
{
for(j=0;j<mx;j++)
{
graph[i][j]=nil;
}
}
for(i=1;i<=mx;i++)
{
graph[i][i]=0;
}
for(i=1;;i++)
{
ll a,b,cost;
cin>>a>>b;
if(a==0&&b==0)
break;
graph[a][b]=1;
}
for(k=1;k<=node;k++)
{
for(i=1;i<=node;i++)
{
for(j=1;j<=node;j++)
{
graph[i][j]=max(graph[i][j],graph[i][k]+graph[k][j]);
}
}
}
ll dis,nde;
dis=-999;
for(i=1;i<=node;i++)
{
if(graph[start][i]>dis)
{
nde=i;
dis=graph[start][i];
}
}
cout<<"Case "<<idx<<": The longest path from "<<start<<" has length "<<dis<<", finishing at "<<nde<<"."<<endl<<endl;
idx++;
}
return 0;
}
No comments:
Post a Comment