传送门:
http://www.lydsy.com/JudgeOnline/problem.php?id=1066 最大流
http://www.lydsy.com/JudgeOnline/problem.php?id=2875 矩阵乘法
http://www.lydsy.com/JudgeOnline/problem.php?id=1927 费用流
/* ID:zky OJ:BZOJ Index:1066 Language:C++ */ #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<climits> #include<iostream> #include<algorithm> #define clear(x) memset(x,0,sizeof(x)) #define inf(x) memset(x,0x7f,sizeof(x)) #define Size G[u].size() #define read3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define read4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w) using namespace std; int s,t; int n,m,k; const int INF=INT_MAX; struct edge{ int u,v,cap,flow; }; vector<edge>edges; vector<vector<int> >G; int d[1001]; int cur[1001]; int vis[1001]; int prev[1001]; void add(int u,int v,int cap){ edges.push_back((edge){u,v,cap,0}); G[u].push_back(edges.size()-1); edges.push_back((edge){v,u,0,0}); G[v].push_back(edges.size()-1); } bool bfs(){ clear(d);clear(vis); queue<int>q; q.push(s); vis[s]=1; while(!q.empty()){ int u=q.front();q.pop(); for(int i=0;i<Size;i++){ edge &e=edges[G[u][i]]; if(!vis[e.v]&&e.cap>e.flow){ vis[e.v]=1; d[e.v]=d[u]+1; q.push(e.v); } } } return vis[t]; } int dfs(int x,int a){ if(x==t||a==0)return a; int flow=0,f; for(int &i=cur[x];i<G[x].size();i++){ edge &e=edges[G[x][i]]; if(d[e.v]==d[x]+1&&(f=dfs(e.v,min(a,e.cap-e.flow)))>0){ flow+=f; e.flow+=f; edges[G[x][i]^1].flow-=f; a-=f; if(!a)break; } } return flow; } int Dinic(){ int flow=0; while(bfs()){ clear(cur); int x; while(x=dfs(s,INF)){ flow+=x; } //flow+=dfs(s,INF); } return flow; //cout<<flow<<" "; } int r,c,D,sum=0; int map[21][21]; int map2[21][21]; int main(){ cin>>r>>c>>D; G.resize(r*c*2+10); s=0;t=r*c*2+1; string str; int hash[21][21]; int tot=0; for(int i=1;i<=r;i++) for(int j=1;j<=c;j++) hash[i][j]=++tot; for(int i=1;i<=r;i++){ cin>>str; for(int j=1;j<=c;j++) map[i][j]=str[j-1]-'0'; } for(int i=1;i<=r;i++){ cin>>str; for(int j=1;j<=c;j++){ if(str[j-1]=='L'){ map2[i][j]=1; sum++; add(s,hash[i][j],1); } } } for(int i=1;i<=r;i++) for(int j=1;j<=c;j++){ if(!map[i][j])continue; add(hash[i][j],hash[i][j]+tot,map[i][j]); for(int dx=-D;dx<=D;dx++) for(int dy=-D;dy<=D;dy++){ int x=i+dx,y=j+dy; if(abs(dx)+abs(dy)<=D&&x>=1&&y>=1&&x<=r&&y<=c&&map[i][j]&&map[x][y]) add(hash[i][j]+tot,hash[x][y],INF); if(abs(dx)+abs(dy)<=D&&(x<1||y<1||x>r||y>c)&&map[i][j]) add(hash[i][j]+tot,t,INF); } } int deb=0; if(deb) for(int i=0;i<edges.size();i++)if(i%2==0) printf("#%d %d -> %d cap:%d\n", i,edges[i].u,edges[i].v,edges[i].cap); cout<<sum-Dinic()<<endl; return 0; }
/* ID:zky OJ:BZOJ Index:2875 Language:C++ */ #include<cstdio> #include<iostream> #include<algorithm> using namespace std; typedef unsigned long long lld; lld m,a,c,x0,n,g; lld Mul(lld x,lld y,lld m){ if(y==0)return 0; if(y==1)return x%m; lld res; res=Mul(x,y>>1,m); if((y&1)==1)return (res+res+x)%m; else return (res+res)%m; } struct Matrix{ lld a[2][2]; Matrix operator*(const Matrix &x)const{ Matrix res; res.a[0][0]=Mul(a[0][0],x.a[0][0],m)+Mul(a[0][1],x.a[1][0],m); res.a[0][1]=Mul(a[0][0],x.a[0][1],m)+Mul(a[0][1],x.a[1][1],m); res.a[1][0]=Mul(a[1][0],x.a[0][0],m)+Mul(a[1][1],x.a[1][0],m); res.a[1][1]=Mul(a[1][0],x.a[0][1],m)+Mul(a[1][1],x.a[1][1],m); return res; /*00 00 01 10 00 01 01 11 10 00 11 10 10 01 11 11*/ } Matrix operator%(const lld &p){ a[0][0]%=p;a[0][1]%=p; a[1][0]%=p;a[1][1]%=p; return *this; } Matrix(){ a[0][0]=1;a[0][1]=0; a[1][0]=0;a[1][1]=1; } }; lld mul(Matrix x,lld f1,lld f2,lld mod){ lld g=mod; return (Mul(x.a[0][0],f1,mod)+Mul(x.a[0][1],f2,mod))%mod; } Matrix power(Matrix a,lld k,lld p){ Matrix x; if(k==0)return x; x=x%p*(power(a,k/2,p)%p)%p; x=(x%p)*(x%p)%p; if(k&1)x=x*(a%p)%p; return x; } int main(){ cin>>m>>a>>c>>x0>>n>>g; Matrix x; x.a[0][0]=a;x.a[0][1]=1; x.a[1][0]=0;x.a[1][1]=1; x=x%m; x=power(x,n,m)%m; cout<<mul(x,x0,c,m)%m%g<<endl; return 0; }
/* ID:zky OJ:BZOJ Index:1927 Language:C++ */ #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<climits> #include<iostream> #include<algorithm> #define clear(x) memset(x,0,sizeof(x)) #define inf(x) memset(x,0x7f,sizeof(x)) #define Size G[u].size() #define read3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define read4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w) using namespace std; int s,t; int n,m,k; const int INF=INT_MAX; struct edge{ int u,v,cap,flow,cost; }; vector<edge>edges; vector<vector<int> >G; int d[3001]; int cur[3001]; int vis[3001]; int prev[3001]; void add(int u,int v,int cap,int cost){ edges.push_back((edge){u,v,cap,0,cost}); G[u].push_back(edges.size()-1); edges.push_back((edge){v,u,0,0,-cost}); G[v].push_back(edges.size()-1); } int a[3001]; bool spfa(int &flow,int &cost){ queue<int>q; q.push(s); memset(d,0x7f,sizeof(d)); memset(vis,0,sizeof(vis)); int B=d[0]; d[s]=0; vis[s]=1; a[s]=INF; while(!q.empty()){ int u=q.front();q.pop();vis[u]=0; for(int i=0;i<G[u].size();i++){ edge &e=edges[G[u][i]]; if(e.cap-e.flow>0&&d[e.v]>d[u]+e.cost){ d[e.v]=d[u]+e.cost; prev[e.v]=G[u][i]; a[e.v]=min(a[u],e.cap-e.flow); if(!vis[e.v]){ vis[e.v]=1; q.push(e.v); } } } } if(d[t]==B)return false; int u=t; flow+=a[t]; cost+=a[t]*d[t]; while(u!=s){ edges[prev[u]].flow+=a[t]; edges[prev[u]^1].flow-=a[t]; u=edges[prev[u]].u; } return true; } int flow,cost; void costflow(){ flow=0;cost=0; while(spfa(flow,cost)); cout<<cost<<endl; } //int a[888]; int main(){ cin>>n>>m; G.resize(n*2+10); s=0;t=2*n+1; for(int i=1;i<=n;i++){ cin>>a[i]; add(s,i+n,1,a[i]); } for(int i=1;i<=m;i++){ int u,v,w; read3(u,v,w); if(u>v)swap(u,v); add(u,v+n,1,w); } for(int i=1;i<=n;i++){ add(s,i,1,0); add(i+n,t,1,0); } costflow(); return 0; }