poj刷题记录
Posted yuzzzz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj刷题记录相关的知识,希望对你有一定的参考价值。
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #define ll long long 6 using namespace std; 7 int gcd(int a,int b) 8 9 return b?gcd(b,a%b):a; 10 11 bool vis[1000000]; 12 int main() 13 14 int n; 15 while(cin>>n) 16 17 memset(vis,0,sizeof(vis)); 18 int ans1 = 0,ans2 = 0; 19 for(ll i=1;i*i<=2*n;i+=2) 20 for(ll j=i+2;j*j<=2*n;j+=2) 21 22 if(i*i+j*j>2*n)break; 23 if(gcd(i,j)!=1 ||i==j)continue; 24 ans1++; 25 ll a = i*j; 26 ll b = (j*j-i*i)/2; 27 ll c = (i*i+j*j)/2; 28 for(ll k=1;k*c<=n;++k) 29 vis[k*a]=vis[k*b]=vis[k*c]=1; 30 31 for(int i=1;i<=n;++i) 32 if(vis[i]) 33 ans2++; 34 cout<<ans1<<" "<<n-ans2<<endl; 35 36 return 0; 37
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 #include<map> 9 #include<string> 10 #include<cstdlib> 11 #include<ctime> 12 #define rep(i,a,n) for(int i=(int)(a);i<(int)(n);++i) 13 #define repb(i,a,n) for(int i=(int)(a);i<=(int)(n);++i) 14 #define ll long long 15 #define inf 0x3f3f3f3f 16 using namespace std; 17 struct 18 19 int x,y; 20 arr[20000]; 21 map<int,int>mp; 22 int fa[20000],size[20000]; 23 int find(int x) 24 25 return x==fa[x]?x:fa[x]=find(fa[x]); 26 27 void merge(int a,int b) 28 29 int fx = find(a),fy = find(b); 30 if(fx==fy)return; 31 fa[fy] = fx; 32 size[fx] += size[fy]; 33 34 int jh[20000]; 35 int main() 36 37 int n,m; 38 scanf("%d%d",&n,&m); 39 for(int i=1;i<=n;++i) 40 41 scanf("%d%d",&arr[i].x,&arr[i].y); 42 mp[arr[i].x*10001+arr[i].y] = i; 43 size[i]=1;fa[i]=i; 44 45 for(int i=1;i<=n;++i) 46 47 int mid = arr[i].x*10001+arr[i].y; 48 int dd1 = mp[mid+1],dd2 = mp[mid+10001]; 49 if(dd1) 50 merge(dd1,i); 51 if(dd2) 52 merge(dd2,i); 53 54 int ans = 0;int cnt =0 ; 55 for(int i=1;i<=n;++i) 56 if(i==fa[i]) 57 jh[cnt++] = size[i]; 58 sort(jh,jh+cnt); 59 for(int i=0;i<m;++i) 60 ans+=jh[cnt-1-i]; 61 printf("%d\n",ans); 62 return 0; 63
1 #include<cstdio> 2 #include<queue> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 const int N = 1e4+10; 7 int head[N],cnt; 8 struct Edg 9 10 int v,w,nxt; 11 e[N*200]; 12 void addedge(int u,int v,int w) 13 14 e[cnt].v = v; 15 e[cnt].w = w; 16 e[cnt].nxt = head[u]; 17 head[u] = cnt++; 18 19 int dis[N],vis[N],t[N]; 20 queue<int>q; 21 bool spfa(int x,int n) 22 23 for(int i=1;i<=n;++i) 24 dis[i] = (1<<30); 25 q.push(x); 26 vis[x]=1; 27 int gs = 0; 28 dis[x]=0; 29 while(!q.empty()) 30 31 //if(gs>n)return 0; 32 int u = q.front();q.pop(); 33 t[u]++; 34 vis[u]=0; 35 if(t[u]>n)return 0; 36 for(int i=head[u];~i;i=e[i].nxt) 37 38 int to = e[i].v; 39 if(dis[to]>dis[u]+e[i].w) 40 41 dis[to]=dis[u]+e[i].w; 42 if(!vis[to]) 43 44 vis[to]=1; 45 q.push(to); 46 47 48 49 50 return 1; 51 52 int main() 53 54 ios::sync_with_stdio(false); 55 cin.tie(0); 56 int n,m,q;cin>>n>>m>>q; 57 for(int i=1;i<=n;++i) 58 head[i] = -1; 59 while(m--) 60 61 int a,b,w;cin>>a>>b>>w; 62 addedge(a,b,w); 63 64 while(q--) 65 66 int a,b,w;cin>>a>>b>>w; 67 addedge(b,a,-w); 68 69 if(spfa(1,n)==0) 70 cout<<-1<<endl; 71 else if(dis[n]==(1<<30)) 72 cout<<-2<<endl; 73 else 74 cout<<dis[n]<<endl; 75 return 0; 76
以上是关于poj刷题记录的主要内容,如果未能解决你的问题,请参考以下文章