/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
#define inf 1<<29
int data[100][100],path[100][100];
int n,e;
void inil()
{
int i,j;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
path[i][j]=0;
data[i][j]=inf;
if(i==j)data[i][j]=0;
}
}
}
void warshal()
{
int i,j,k;
for(k=1; k<=n; k++)
{
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(data[i][j]>data[i][k]+data[k][j])
{
data[i][j]=data[i][k]+data[k][j];
path[i][j]=path[k][j];
}
}
}
}
}
void print_path(int s,int e)
{
if(path[s][e]==s)
{
printf("%d",path[s][e]);
return;
}
else
{
print_path(s,path[s][e]);
printf(" %d",path[s][e]);
}
}
int main()
{
int i,j,u,v,w,s,e,cs=1;
while(scanf("%d",&n) && n)
{
inil();
for(i=1; i<=n; i++)
{
scanf("%d",&s);
for(j=1; j<=s; j++)
{
scanf("%d %d",&v,&w);
data[i][v]=w;
}
}
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(data[i][j]!=inf && i!=j)
path[i][j]=i;
}
}
warshal();
scanf("%d %d",&s,&e);
printf("Case %d: Path = ",cs++);
print_path(s,e);
printf(" %d; ",e);
printf("%d second delay\n",data[s][e]);
}
return 0;
}
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
#define inf 1<<29
int data[100][100],path[100][100];
int n,e;
void inil()
{
int i,j;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
path[i][j]=0;
data[i][j]=inf;
if(i==j)data[i][j]=0;
}
}
}
void warshal()
{
int i,j,k;
for(k=1; k<=n; k++)
{
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(data[i][j]>data[i][k]+data[k][j])
{
data[i][j]=data[i][k]+data[k][j];
path[i][j]=path[k][j];
}
}
}
}
}
void print_path(int s,int e)
{
if(path[s][e]==s)
{
printf("%d",path[s][e]);
return;
}
else
{
print_path(s,path[s][e]);
printf(" %d",path[s][e]);
}
}
int main()
{
int i,j,u,v,w,s,e,cs=1;
while(scanf("%d",&n) && n)
{
inil();
for(i=1; i<=n; i++)
{
scanf("%d",&s);
for(j=1; j<=s; j++)
{
scanf("%d %d",&v,&w);
data[i][v]=w;
}
}
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(data[i][j]!=inf && i!=j)
path[i][j]=i;
}
}
warshal();
scanf("%d %d",&s,&e);
printf("Case %d: Path = ",cs++);
print_path(s,e);
printf(" %d; ",e);
printf("%d second delay\n",data[s][e]);
}
return 0;
}
No comments:
Post a Comment