首页 > 代码库 > hdu 5154 Harry and Magical Computer
hdu 5154 Harry and Magical Computer
http://acm.hdu.edu.cn/showproblem.php?pid=5154
思路:有向图判断有没有环,可以用floyd。。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int inf=1<<20; 6 7 int g[200][200]; 8 int main() 9 {10 int n,m;11 while(scanf("%d%d",&n,&m)!=EOF)12 {13 memset(g,0,sizeof(g));14 for(int i=1; i<=n; i++)15 {16 for(int j=1; j<=n; j++)17 {18 g[i][j]=inf;19 }20 }21 for(int i=0; i<m; i++)22 {23 int a,b;24 scanf("%d%d",&a,&b);25 g[b][a]=1;26 }27 for(int k=1; k<=n; k++)28 {29 for(int i=1; i<=n; i++)30 {31 for(int j=1; j<=n; j++)32 {33 if(i==k||j==k) continue;34 g[i][j]=min(g[i][j],g[i][k]+g[k][j]);35 }36 }37 }38 bool flag=true;39 for(int i=1; i<=n; i++)40 {41 if(g[i][i]<inf)42 {43 flag=false;44 break;45 }46 }47 if(flag) printf("YES\n");48 else printf("NO\n");49 }50 return 0;51 }
hdu 5154 Harry and Magical Computer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。