图论中的sb错误集锦
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图论中的sb错误集锦相关的知识,希望对你有一定的参考价值。
1.floyd——jzyzoj1213 最优乘车
策略非常简单,就是建立权值为1的边就好了,然而处理数据的时候翻了个sb错误:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 using namespace std; 6 #define MAX 510 7 int m,n,a[510][510],queue[51000]; 8 string s[110]; 9 void work() 10 { 11 cin>>m>>n; 12 for (int i=0;i<=m;i++) 13 getline(cin,s[i]); 14 for (int i=1;i<=m;i++) 15 { 16 int len=0; 17 for (int j=0;j<s[i].size();j++) 18 if (s[i][j]!=‘ ‘) 19 queue[++len]=s[i][j]-‘0‘; 20 for (int j=1;j<=len;j++) 21 for (int k=j+1;k<=len;k++) 22 a[queue[j]][queue[k]]=1; 23 } 24 } 25 26 void floyd() 27 { 28 for (int k=1;k<=n;k++) 29 for (int i=1;i<=n;i++) 30 for (int j=1;j<=n;j++) 31 if (a[i][j]>a[i][k]+a[k][j]) 32 a[i][j]=a[i][k]+a[k][j]; 33 } 34 int main() 35 { 36 //freopen("add.in","r",stdin); 37 //freopen("add.out","w",stdout); 38 memset(a,10,sizeof(a)); 39 work(); 40 floyd(); 41 if (a[1][n]>MAX) cout<<"NO"<<endl; 42 else cout<<a[1][n]-1<<endl; 43 return 0; 44 }
因为读入的是string,所以当string中出现大于9的数时是无法处理的。其实正确的处理方法加一个while即可
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 using namespace std; 6 #define MAX 510 7 int m,n,a[510][510],queue[51000]; 8 string s[110]; 9 void work() 10 { 11 cin>>m>>n; 12 for (int i=0;i<=m;i++) 13 getline(cin,s[i]); 14 for (int i=1;i<=m;i++) 15 { 16 int len=0; 17 int lens=s[i].size(); 18 for (int j=0;j<lens;j++) 19 if (s[i][j]!=‘ ‘) 20 { 21 int num=0; 22 while(s[i][j]!=‘ ‘&&j<lens) 23 { 24 num*=10; 25 num+=s[i][j]-‘0‘; 26 j++; 27 } 28 j--; 29 queue[++len]=num; 30 } 31 for (int j=1;j<=len;j++) 32 for (int k=j+1;k<=len;k++) 33 a[queue[j]][queue[k]]=1; 34 } 35 } 36 37 void floyd() 38 { 39 for (int k=1;k<=n;k++) 40 for (int i=1;i<=n;i++) 41 for (int j=1;j<=n;j++) 42 if (a[i][j]>a[i][k]+a[k][j]) 43 a[i][j]=a[i][k]+a[k][j]; 44 } 45 int main() 46 { 47 //freopen("add.in","r",stdin); 48 //freopen("add.out","w",stdout); 49 memset(a,1,sizeof(a)); 50 work(); 51 floyd(); 52 if (a[1][n]>MAX) cout<<"NO"<<endl; 53 else cout<<a[1][n]-1<<endl; 54 return 0; 55 }
2.floyd——jzyzoj1214 bessie come home
floyd的模板题,啊啊啊啊为什么我做一道题就错一道题。
注意双向边还是单向边啊啊啊啊啊
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int n,a[56][56],len; char st,en; void init() { cin>>n; for (int i=1;i<=n;i++) { cin>>st>>en>>len; //cout<<st<<‘ ‘<<en<<‘ ‘<<len<<endl; int j,k; if (st<=‘Z‘) j=st-‘A‘+1; else j=st-‘a‘+27; if (en<=‘Z‘) k=en-‘A‘+1; else k=en-‘a‘+27; //cout<<j<<‘ ‘<<k<<endl; a[j][k]=min(len,a[j][k]); } } void floyd() { for (int i=1;i<=52;i++) a[i][i]=0; for (int k=1;k<=52;k++) for (int i=1;i<=52;i++) for (int j=1;j<=52;j++) if (a[i][j]>a[i][k]+a[j][k]) a[i][j]=a[i][k]+a[j][k]; } int work() { int st,minn=999999999; for (int i=1;i<=25;i++) if (a[i][26]<minn) { minn=a[i][26]; st=i; } return st; } int main() { ios::sync_with_stdio(false); //freopen("add.in","r",stdin); //freopen("add.out","w",stdout); memset(a,1,sizeof(a)); init(); floyd(); int ans=work(); char answer; if (ans<26) answer=char(ans+‘A‘-1); else answer=char(ans-27+‘a‘); cout<<answer<<‘ ‘<<a[ans][26]<<endl; return 0; }
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int n,a[56][56],len; 7 char st,en; 8 void init() 9 { 10 cin>>n; 11 for (int i=1;i<=n;i++) 12 { 13 cin>>st>>en>>len; 14 //cout<<st<<‘ ‘<<en<<‘ ‘<<len<<endl; 15 int j,k; 16 if (st<=‘Z‘) 17 j=st-‘A‘+1; 18 else 19 j=st-‘a‘+27; 20 if (en<=‘Z‘) 21 k=en-‘A‘+1; 22 else 23 k=en-‘a‘+27; 24 //cout<<j<<‘ ‘<<k<<endl; 25 a[j][k]=min(len,a[j][k]); 26 a[k][j]=a[j][k]; 27 } 28 } 29 30 void floyd() 31 { 32 for (int i=1;i<=52;i++) 33 a[i][i]=0; 34 for (int k=1;k<=52;k++) 35 for (int i=1;i<=52;i++) 36 for (int j=1;j<=52;j++) 37 if (a[i][j]>a[i][k]+a[j][k]) 38 a[i][j]=a[i][k]+a[j][k]; 39 } 40 41 int work() 42 { 43 int st,minn=999999999; 44 for (int i=1;i<=25;i++) 45 if (a[i][26]<minn) 46 { 47 minn=a[i][26]; 48 st=i; 49 } 50 return st; 51 } 52 53 int main() 54 { 55 ios::sync_with_stdio(false); 56 //freopen("add.in","r",stdin); 57 //freopen("add.out","w",stdout); 58 memset(a,1,sizeof(a)); 59 init(); 60 floyd(); 61 int ans=work(); 62 char answer; 63 answer=char(ans+‘A‘-1); 64 cout<<answer<<‘ ‘<<a[ans][26]<<endl; 65 return 0; 66 }
以上是关于图论中的sb错误集锦的主要内容,如果未能解决你的问题,请参考以下文章
算法笔记:图论中的单源最短路径算法——Bellman-Ford 算法