标签:memset mem struct 成功 tor iostream 欧拉回路 clu .net
这场比赛可以说是灰常的水了,涨信心场??
今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~
之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小时连题解都出来了···
A-烤肉拌饭 ( uva 572)
就是求联通块的数量啊,刚学dfs的时候做的那种!
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 #include <queue> 6 7 using namespace std; 8 const int maxn=100+5; 9 const int dx[]={1,-1,1,-1,1,-1,0,0}; 10 const int dy[]={0,0,1,1,-1,-1,1,-1}; 11 char G[maxn][maxn]; 12 int vis[maxn][maxn]; 13 int n,m; 14 int ans; 15 void dfs(int x,int y){ 16 vis[x][y]=1; 17 for(int i=0;i<8;i++){ 18 int nx=x+dx[i]; 19 int ny=y+dy[i]; 20 if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&G[nx][ny]==‘@‘&&!vis[nx][ny]){ 21 dfs(nx,ny); 22 } 23 } 24 return ; 25 } 26 int main(){ 27 while(scanf("%d%d",&n,&m)&&n&&m){ 28 memset(vis,0,sizeof(vis)); 29 ans=0; 30 for(int i=1;i<=n;i++){ 31 for(int j=1;j<=m;j++){ 32 scanf(" %c",&G[i][j]); 33 } 34 } 35 for(int i=1;i<=n;i++){ 36 for(int j=1;j<=m;j++){ 37 if(!vis[i][j]&&G[i][j]==‘@‘){ 38 ans++; 39 dfs(i,j); 40 } 41 } 42 } 43 cout<<ans<<endl; 44 } 45 return 0; 46 }
B-麻辣香锅 HihoCoder - 1632
模拟,emmm模拟可以提高代码能力??
C-啵啵鱼 poj2387
是个很裸的最短路,一个spfa或者dijsktra就可以
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 #include <queue> 6 7 using namespace std; 8 const int maxn=2000+10; 9 const int maxm=4000+10; 10 const int INF=2147483600; 11 typedef long long LL; 12 int head[maxn],Next[maxm],to[maxm]; 13 LL val[maxm]; 14 int sz; 15 int n,m; 16 void add_edge(int a,int b,LL w){ 17 ++sz; 18 val[sz]=w; 19 to[sz]=b; 20 Next[sz]=head[a]; 21 head[a]=sz; 22 } 23 LL d[maxn]; 24 int spfa(int s){ 25 for(int i=1;i<=n;i++)d[i]=INF; 26 int vis[maxn]; 27 memset(vis,0,sizeof(vis)); 28 queue<int>q; 29 q.push(s); 30 vis[s]=1; 31 d[s]=0; 32 while(!q.empty()){ 33 int u=q.front();q.pop();vis[u]=0; 34 for(int i=head[u];i;i=Next[i]){ 35 int v=to[i]; 36 if(d[v]>d[u]+val[i]){ 37 d[v]=d[u]+val[i]; 38 if(!vis[v]){ 39 vis[v]=1; 40 q.push(v); 41 } 42 } 43 } 44 } 45 return d[1]; 46 } 47 int main(){ 48 sz=0; 49 memset(head,0,sizeof(head)); 50 scanf("%d%d",&m,&n); 51 int a,b;LL c; 52 for(int i=1;i<=m;i++){ 53 scanf("%d %d%lld",&a,&b,&c); 54 // cout<<a<<" "<<b<<" "<<c<<endl; 55 add_edge(a,b,c); 56 add_edge(b,a,c); 57 } 58 cout<<spfa(n); 59 return 0; 60 }
D-海底捞 POJ2236
卢老师一句话点醒梦中人,直接并查集就可以~
1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 const int maxn=1000+10; 7 int n,d; 8 int a,b; 9 char c; 10 int p[maxn],vis[maxn]; 11 struct Node{ 12 int x,y; 13 }node[maxn]; 14 int find(int x){ 15 return p[x]==x?x:p[x]=find(p[x]); 16 } 17 bool dist(Node A,Node B){ 18 if((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)<=d*d) 19 return true; 20 return false; 21 } 22 int main(){ 23 scanf("%d%d",&n,&d); 24 for(int i=1;i<=n;i++){ 25 scanf("%d%d",&node[i].x,&node[i].y); 26 } 27 for(int i=0;i<=n;i++)p[i]=i; 28 while(scanf(" %c",&c)!=EOF){ 29 if(c==‘O‘){ 30 scanf("%d",&a); 31 vis[a]=1; 32 for(int i=1;i<=n;i++){ 33 if(vis[i]&&i!=a&&dist(node[a],node[i])){ 34 // cout<<a<<"-->"<<i<<endl; 35 int x=find(a),y=find(i); 36 if(x!=y)p[x]=y; 37 } 38 } 39 } 40 if(c==‘S‘){ 41 scanf("%d%d",&a,&b); 42 int x=find(a),y=find(b); 43 //cout<<":"<<x<<y<<endl; 44 if(x==y){ 45 printf("SUCCESS\n"); 46 }else{ 47 printf("FAIL\n"); 48 } 49 } 50 } 51 return 0; 52 }
E-黄焖鸡 uva10054
这个题以前做过,欧拉回路,注意打印的时候逆序输出
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 #include <queue> 6 7 using namespace std; 8 const int maxn=1000+20; 9 10 int T; 11 int G[maxn][maxn]; 12 int n; 13 int deg[maxn]; 14 int a,b; 15 int M; 16 int p[maxn]; 17 int find(int x){ 18 return p[x]==x?x:find(p[x]); 19 } 20 void dfs(int u){ 21 for(int i=1;i<=M;i++){ 22 if(G[u][i]){ 23 G[u][i]--; 24 G[i][u]--; 25 dfs(i); 26 cout<<i<<" "<<u<<endl; 27 } 28 } 29 return; 30 } 31 int main(){ 32 scanf("%d",&T); 33 for(int t=1;t<=T;t++){ 34 if(t!=1)cout<<""<<endl; 35 memset(G,0,sizeof(G)); 36 memset(deg,0,sizeof(deg)); 37 printf("Case #%d\n",t); 38 M=-1; 39 scanf("%d",&n); 40 for(int i=1;i<=55;i++)p[i]=i; 41 for(int i=1;i<=n;i++){ 42 scanf("%d%d",&a,&b); 43 M=max(M,max(a,b)); 44 G[a][b]++; 45 G[b][a]++; 46 deg[a]++; 47 deg[b]++; 48 int x=find(a),y=find(b); 49 if(x!=y)p[x]=y; 50 51 } 52 bool judge=1; 53 for(int i=1;i<=M;i++){ 54 if(deg[i]){ 55 if(find(i)!=find(M)||deg[i]%2){ 56 judge=0; 57 break; 58 } 59 } 60 } 61 if(!judge){ 62 cout<<"some beads may be lost"<<endl; 63 }else{ 64 for(int i=1;i<=M;i++) 65 dfs(i); 66 } 67 68 } 69 return 0; 70 }
F- 减肥成功指日可待 hdu2141
用set或者二分。但是set再G++下会MLE必须要选择C++才能AC,雾···
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 #include <set> 6 #include <vector> 7 using namespace std; 8 const int maxn=500+10; 9 typedef long long LL; 10 int A,B,C,s,x; 11 LL a[maxn],b[maxn],c[maxn]; 12 int kase; 13 int main(){ 14 while(scanf("%d%d%d",&A,&B,&C)!=EOF){ 15 kase++; 16 set<LL>S; 17 printf("Case %d:\n",kase); 18 for(int i=1;i<=A;i++){ 19 scanf("%lld",&a[i]); 20 } 21 for(int i=1;i<=B;i++){ 22 scanf("%lld",&b[i]); 23 } 24 for(int i=1;i<=C;i++){ 25 scanf("%lld",&c[i]); 26 } 27 for(int i=1;i<=A;i++){ 28 for(int j=1;j<=B;j++){ 29 //for(int l=1;l<=C;l++){ 30 LL X=a[i]+b[j]; 31 if(!S.count(X)) 32 S.insert(X); 33 // } 34 } 35 } 36 scanf("%d",&s); 37 LL x; 38 for(int i=1;i<=s;i++){ 39 scanf("%lld",&x); 40 bool ok=0; 41 for(int i=1;i<=C;i++){ 42 LL pos=x-c[i]; 43 //if(pos<0)continue; 44 if(S.count(pos)){ 45 ok=1; 46 break; 47 } 48 } 49 //if(S.count(x))cout<<"YES"<<endl; 50 if(ok)cout<<"YES"<<endl; 51 else 52 cout<<"NO"<<endl; 53 } 54 } 55 return 0; 56 }
2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)
标签:memset mem struct 成功 tor iostream 欧拉回路 clu .net
原文地址:https://www.cnblogs.com/LQLlulu/p/8810035.html