noip2014day2
T1
直接枚举位置,暴力过.
#include <stdio.h> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <vector> using namespace std; int d,n,ans,f,m[130][130]; template <class T> void read(T&x) { x=0;char c=getchar();int f=0; while(c<'0'||c>'9'){f|=(c=='-');c=getchar();} while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^=48),c=getchar(); x=f?-x:x; } int dfs(int x,int y) { int sum=0; for(int i=x-d;i<=x+d;i++) for(int j=y-d;j<=y+d;j++) { if(i<1||j<1||i>129||j>129)continue; sum+=m[i][j]; } return sum; } int main() { freopen("wireless.in","r",stdin); freopen("wireless.out","w",stdout); memset(m,0,sizeof(m)); read(d);read(n); int x,y,k; for(register int i=1;i<=n;i++) { read(x);read(y);read(k); m[x+1][y+1]=k; } int t; for(int i=1;i<=129;i++) for(int j=1;j<=129;j++) { t=dfs(i,j); if(t>ans)ans=t,f=1; else if(t==ans)f++; } printf("%d %d",f,ans); fclose(stdin); fclose(stdout); return 0; }
T2
正反都建边;
反向bfs能到的点,然后正向跑spfa。(居然T了一个点)
#include <stdio.h> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <vector> using namespace std; const int maxn=10000+10; const int maxm=200000+10; const int inf=0x7f/3; struct hh { int v,next; }e[maxm]; hh a[maxm]; int n,m,point,s,t; int head[maxn],vis[maxn],dis[maxn],head2[maxn],gou[maxn],c[maxn]; template <class T> void read(T&x) { x=0;char c=getchar();int f=0; while(c<'0'||c>'9'){f|=(c=='-');c=getchar();} while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^=48),c=getchar(); x=f?-x:x; } void bfs(int s) { queue<int>p; memset(c,0,sizeof(c)); p.push(s);vis[s]=1; while(!p.empty()) { int x=p.front();p.pop();vis[x]=0; gou[x]=1; for(int i=head2[x];i!=-1;i=a[i].next) { if(++c[a[i].v]>n||vis[a[i].v])continue; p.push(a[i].v); vis[a[i].v]=1; } } return; } int pan(int x) { for(int i=head[x];i!=-1;i=e[i].next)if(!gou[e[i].v])return 0; return 1; } void spfa(int p) { queue<int>q; memset(vis,0,sizeof(vis)); memset(dis,inf,sizeof(dis)); memset(c,0,sizeof(c)); vis[p]=1;q.push(p);dis[p]=0; while(!q.empty()) { int x=q.front();q.pop();vis[x]=0; for(int i=head[x];i!=-1;i=e[i].next) { int y=e[i].v; if(pan(y)&&dis[y]>dis[x]+1) { dis[y]=dis[x]+1; if(!vis[y]) { if(++c[y]>n)continue; q.push(y); vis[y]=1; } } } } return; } void add(int u,int v){e[++point].v=v;e[point].next=head[u];head[u]=point;} void add1(int u,int v){a[point].v=v;a[point].next=head2[u];head2[u]=point;} int main() { freopen("road.in","r",stdin); freopen("road.out","w",stdout); memset(head,-1,sizeof(head)); memset(head2,-1,sizeof(head2)); read(n);read(m); int u,v; for(register int i=1;i<=m;i++) { read(u);read(v); add(u,v);add1(v,u); } read(s);read(t); bfs(t); spfa(s); if(dis[t]==707406378)printf("-1"); else printf("%d",dis[t]); fclose(stdin); fclose(stdout); return 0; }
T3
枚举解,选几个质数带进去%一下就过了(5-6)
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int mod[5]={11261,19997,22877,21893,14843}; int n,m; int ans[1000005]; int a[5][105],pre[5][105],res[5][30005]; char ch[10005]; inline int cal(int t,int x) { int sum=0; for(int i=0;i<=n;i++) sum=(sum+a[t][i]*pre[t][i])%mod[t]; if(sum<0)sum+=mod[t]; return sum; } inline bool jud(int x) { for(int t=0;t<5;t++) if(res[t][x%mod[t]]!=0)return 0; return 1; } int main() { n=read();m=read(); for(int i=0;i<=n;i++) { scanf("%s",ch+1); int l=strlen(ch+1); bool flag=0; for(int t=0;t<5;t++) if(ch[1]!='-')a[t][i]=ch[1]-'0'; else a[t][i]=0,flag=1; for(int t=0;t<5;t++) { for(int k=2;k<=l;k++) a[t][i]=(a[t][i]*10+ch[k]-'0')%mod[t]; if(flag)a[t][i]=-a[t][i]; } } for(int t=0;t<5;t++) for(int x=1;x<mod[t];x++) { pre[t][0]=1; for(int i=1;i<=n;i++)pre[t][i]=(pre[t][i-1]*x)%mod[t]; res[t][x]=cal(t,x); } for(int i=1;i<=m;i++) if(jud(i))ans[++ans[0]]=i; printf("%d\n",ans[0]); for(int i=1;i<=ans[0];i++) printf("%d\n",ans[i]); return 0; }