如果用Floyd算法做的话就纯粹是道背诵题,只不过变了一下权值累积的方式而已。
Accepted | 1092 | C++ | 00:00.04 | 436K |
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
float rate[31][31 ];
char c[31][20 ];
int cmp(const void *a,const void * b)
{
return strcmp((char*)a,(char* )b);
}
int binsearch(int p,int r,char key[])
{
while(p <= r)
{
int q = (p + r) / 2 ;
int temp = strcmp(key,c[q]);
if(temp == 0) return q;
else if(temp > 0) p = q + 1 ;
else r = q - 1 ;
}
return -1 ;
}
void solve(int n)
{
int m,i,j,k;
bool res = false ;
for(i = 0; i < n; i++ )
scanf("%s" ,c[i]);
qsort(c,n,20 ,cmp);
scanf("%d",& m);
for(j = 0; j < m; j++ )
{
float r;
char s1[20],s2[20 ];
scanf("%s%f%s",s1,& r,s2);
int row = binsearch(0 ,n,s1);
int col = binsearch(0 ,n,s2);
rate[row][col] = r;
}
for(k = 0; k < n; k++ )
for(i = 0; i < n; i++ )
for(j = 0; j < n; j++ )
if(rate[i][j] < rate[i][k] * rate[k][j])
rate[i][j] = rate[i][k] * rate[k][j];
for(i = 0; i < n && !res; i++ )
if(rate[i][i] > 1) res = true ;
res ? printf("Yes/n") : printf("No/n" );
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1092.txt","r" ,stdin);
#endif
int n,t = 0 ;
while(scanf("%d",&n) != EOF && n != 0 )
{
printf("Case %d: ",++ t);
memset(rate,0,sizeof (rate));
solve(n);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}
#include <string.h>
#include <stdlib.h>
float rate[31][31 ];
char c[31][20 ];
int cmp(const void *a,const void * b)
{
return strcmp((char*)a,(char* )b);
}
int binsearch(int p,int r,char key[])
{
while(p <= r)
{
int q = (p + r) / 2 ;
int temp = strcmp(key,c[q]);
if(temp == 0) return q;
else if(temp > 0) p = q + 1 ;
else r = q - 1 ;
}
return -1 ;
}
void solve(int n)
{
int m,i,j,k;
bool res = false ;
for(i = 0; i < n; i++ )
scanf("%s" ,c[i]);
qsort(c,n,20 ,cmp);
scanf("%d",& m);
for(j = 0; j < m; j++ )
{
float r;
char s1[20],s2[20 ];
scanf("%s%f%s",s1,& r,s2);
int row = binsearch(0 ,n,s1);
int col = binsearch(0 ,n,s2);
rate[row][col] = r;
}
for(k = 0; k < n; k++ )
for(i = 0; i < n; i++ )
for(j = 0; j < n; j++ )
if(rate[i][j] < rate[i][k] * rate[k][j])
rate[i][j] = rate[i][k] * rate[k][j];
for(i = 0; i < n && !res; i++ )
if(rate[i][i] > 1) res = true ;
res ? printf("Yes/n") : printf("No/n" );
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1092.txt","r" ,stdin);
#endif
int n,t = 0 ;
while(scanf("%d",&n) != EOF && n != 0 )
{
printf("Case %d: ",++ t);
memset(rate,0,sizeof (rate));
solve(n);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}