# 网络流24题

### T1 飞行员配对方案

#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
struct edge{
int x,y,next;
edge(){}
edge(int _x,int _y,int _nt):x(_x),y(_y),next(_nt){}
}e[20005];
}
int n,m;
bool vst[maxn];
int match[maxn];
int DFS(int x){
int y=e[i].y;
if(vst[y])continue;vst[y]=1;
if(!match[y] || DFS(match[y])){
match[y]=x;
return 1;
}
}
return 0;
}
int main(){
scanf("%d%d",&n,&m);
int x,y;

int ans=0;
for(int i=1;i<=n;i++){
memset(vst,0,sizeof(vst));
ans+=DFS(i);
}
printf("%d\n",ans);
}

### T2 太空飞行计划

S向实验i连一条流量为Ci的边，仪器i向T连一条流量为Ci的边，每组实验i与仪器j的关系连一条流量为INF的边

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<queue>
using namespace std;
const int maxn=110;
const int maxm=10010;
const int INF=0x3f3f3f3f;
struct edge{
int x,y,next,v;
edge(){}
edge(int _x,int a,int b,int c):x(_x),y(a),next(b),v(c){}
} e[maxm<<1];
int n,m,S,T;
}
bool BFS(){
queue<int>q;
for(int i=1;i<=T;i++)h[i]=-1;

h[S]=0;q.push(S);
while(!q.empty()){
int x=q.front();q.pop();
int y=e[i].y;int v=e[i].v;
if(h[y]==-1 && v){
h[y]=h[x]+1;
q.push(y);
}
}
}
return h[T]!=-1;
}
int DFS(int x,int f){
int tmp,used=0;
if(x==T)return f;
for(int i=cur[x];i;i=e[i].next){
int y=e[i].y,v=e[i].v;
if(h[y]==h[x]+1 && v){
tmp=DFS(y,min(v,f-used));
e[i].v-=tmp;if(e[i].v)cur[x]=i;
e[i^1].v+=tmp;
used+=tmp;
if(used==f)return used;
}
}
if(!used)h[x]=-1;
return used;
}
int maxf(){
int ret=0;
while(BFS()){

ret+=DFS(S,INF);
}
return ret;
}
int main(){
scanf("%d%d",&n,&m);
S=n+m+1,T=S+1;
int ans=0;
for(int i=1;i<=n;i++){
int c;
scanf("%d",&c);
ans+=c;
int d;
while(scanf("%d",&d)&&(d!=0)){
}
}
for(int i=1;i<=m;i++){
int c;
scanf("%d",&c);
}

ans-=maxf();
for(int i=1;i<=n;i++)if(h[i]!=-1)cout<<i<<" ";cout<<endl;
for(int i=1;i<=m;i++)
if(h[i+n]!=-1)cout<<i<<" ";cout<<endl;
cout<<ans<<endl;
}

### T3 最小路径覆盖

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn=40010;
const int maxm=80010;
const int INF=0x3f3f3f3f;
struct edge{
int y,next,v;
edge(){}
edge(int a,int b,int c):y(a),next(b),v(c){}
} e[maxm<<1];

int n,m,S,T;
}
int cur[maxn],h[maxn];

#include<queue>
queue<int>q;
bool BFS(){
memset(h,-1,sizeof(h));
q.push(S);h[S]=0;
while(!q.empty()){
int x=q.front();q.pop();
int y=e[i].y,v=e[i].v;
if(h[y]==-1 && v){
q.push(y);
h[y]=h[x]+1;
}
}
}
return h[T]!=-1;
}
int mark[maxn],to[maxn];
int DFS(int x,int f){
if(x==T)return f;
int tmp,used=0;
for(int i=cur[x];i;i=e[i].next){
int y=e[i].y;
int v=e[i].v;
if(v && h[y]==h[x]+1){
tmp=DFS(y,min(v,f-used));
e[i].v-=tmp;
e[i^1].v+=tmp;
used+=tmp;
if(tmp){
to[x]=y;
if(y>n)mark[y-n]=1;
}
if(e[i].v)cur[x]=i;
if(used==f)return f;
}
}
if(!used)h[x]=-1;
return used;
}
int maxf(){
int ret=0;
while(BFS()){
ret+=DFS(S,INF);
}
return ret;
}
int main(){
scanf("%d%d",&n,&m);

S=n*2+1;T=S+1;
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
}
for(int i=1;i<=n;i++){
}
memset(to,0,sizeof(to));
memset(mark,0,sizeof(mark));
printf("%d\n",n-maxf());
for(int i=1;i<=n;i++){
if(mark[i])continue;
printf("%d",i);
int y=i;
while(to[y]){
printf(" %d",to[y]-n);
y=to[y]-n;
}
printf(" 0\n");
}
}

### T4 魔术球问题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
using namespace std;
const int maxn=4010;
const int maxm=200010;
const int INF=0x3f3f3f3f;
const int offset=2000;
struct edge{
int x,y,next,v;
edge(){}
edge(int _x,int a,int b,int c):x(_x),y(a),next(b),v(c){}
} e[maxm<<1];

}
bool issquare[maxn];
int S,T,n;
#include<queue>
queue<int>q;
int h[maxn];
bool BFS(){
memset(h,-1,sizeof(h));
h[S]=0;q.push(S);
while(!q.empty()){
int x=q.front();q.pop();
int y=e[i].y;
int v=e[i].v;
if(v && h[y]==-1){
h[y]=h[x]+1;
q.push(y);
}
}
}
return h[T]!=-1;
}
int DFS(int x,int f){
if(x==T)return f;
int used=0,tmp;
int y=e[i].y;
int v=e[i].v;
if(v && h[y]==h[x]+1){
tmp=DFS(y,min(v,f-used));
e[i].v-=tmp;
e[i^1].v+=tmp;
used+=tmp;
if(used==f)return f;
}
}
if(!used)h[x]=-1;
return used;
}
int ret=0;
int maxf(){

while(BFS())ret+=DFS(S,INF);
return ret;
}
int main(){
scanf("%d",&n);
memset(issquare,0,sizeof(issquare));
for(int i=1;i<=60;i++)issquare[i*i]=1;
S=0;T=maxn-1;
int tmp,A;
for(A=1;A-maxf() <= n;){
++A;
for(int j=1;j<A;j++){
if(issquare[A+j])
}
}
int ANS=A-1;
printf("%d\n",ANS);
}

### T5 圆桌问题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cstdlib>
using namespace std;
const int maxn=2010;
const int maxm=400010;
const int INF=0x3f3f3f3f;
struct edge{
int x,y,next,v;
edge(){}
edge(int _x,int _y,int _nt,int _v):
x(_x),y(_y),next(_nt),v(_v){}
} e[maxm];
}
int cur[maxn],h[maxn];
int n,m;
int a[maxn],b[maxn];
int S,T;
queue<int>q;
bool BFS(){
memset(h,-1,sizeof(h));
q.push(S);h[S]=0;
while(!q.empty()){
int x=q.front();q.pop();
int y=e[i].y;
int v=e[i].v;
if( v && h[y]==-1){
h[y]=h[x]+1;
q.push(y);
}
}
}
return h[T]!=-1;
}
int DFS(int x,int f){
if(x==T)return f;
int used=0,tmp;
for(int i=cur[x];i;i=e[i].next){
int y=e[i].y;
int v=e[i].v;
if(v && h[y]==h[x]+1){
tmp=DFS(y,min(v,f-used));
e[i].v-=tmp;
e[i^1].v+=tmp;
used+=tmp;
if(e[i].v)cur[x]=i;
if(used==f)return f;
}
}
if(!used)h[x]=-1;
return used;
}
int maxf(){
int ret=0;
while(BFS()){
ret+=DFS(S,INF);
}
return ret;
}
int main(){
scanf("%d%d",&n,&m);
S=0;T=n+m+1;
int sum=0;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++)scanf("%d",&b[i]);
for(int i=1;i<=n;i++)sum+=a[i];
for(int i=1;i<=n;i++)
for(int i=1;i<=m;i++)
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)

int tmp=maxf();
if(tmp == sum)puts("1");else puts("0");
}

### T6 最长递增子序列

DP+最大流

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cstdlib>
using namespace std;
const int maxn=2010;
const int maxm=400010;
const int INF=0x3f3f3f3f;
struct edge{
int x,y,next,v;
edge(){}
edge(int a,int b,int c,int d):x(a),y(b),next(c),v(d){}
}e[maxm];
int S,T;
}
int h[maxn],cur[maxn];
int a[maxn],f[maxn],n;

queue<int>q;
bool BFS(){
memset(h,-1,sizeof(h));
h[S]=0;q.push(S);
while(!q.empty()){
int x=q.front();q.pop();
int y=e[i].y;
int v=e[i].v;
if(h[y]==-1 && v){
h[y]=h[x]+1;
q.push(y);
}
}
}
return h[T]!=-1;
}
int DFS(int x,int f){
if(x==T)return f;
int used=0,tmp;
for(int i=cur[x];i;i=e[i].next){
int y=e[i].y;
int v=e[i].v;
if(h[y]==h[x]+1 && v){
tmp=DFS(y,min(v,f-used));
e[i].v-=tmp;
e[i^1].v+=tmp;
used+=tmp;
if(e[i].v)cur[x]=i;
if(used==f)return f;
}
}
if(!used)h[x]=-1;
return used;
}
int ret=0;
int maxf(){
while(BFS()){
ret+=DFS(S,INF);
}
return ret;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
f[i]=1;
}
for(int i=n-1;i>=1;i--){
for(int j=i+1;j<=n;j++)
if(a[i]<a[j])f[i]=max(f[i],f[j]+1);
}
int ans1=0;
for(int i=1;i<=n;i++)ans1=max(ans1,f[i]);
printf("%d\n",ans1);

tot=1;
S=0;T=n*2+8;

for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++)
if(f[i]==f[j]+1 && a[i]<a[j])
}

printf("%d\n",maxf());

printf("%d\n",maxf());
}

### T7 试题库问题

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=2010;
const int maxm=400010;
const int INF=0x3f3f3f3f;
struct edge{
int x,y,next,v;
edge(){}
edge(int a,int b,int c,int d)
:x(a),y(b),next(c),v(d){}
} e[maxm];
}
int S,T;
int n,m,k;
int cur[maxn],h[maxn];
queue<int>q;
bool BFS(){
memset(h,-1,sizeof(h));
h[S]=0;q.push(S);
while(!q.empty()){
int x=q.front();q.pop();
int y=e[i].y;
int v=e[i].v;
if(v && h[y]==-1){
h[y]=h[x]+1;
q.push(y);
}
}
}
return h[T]!=-1;
}
int DFS(int x,int f){
if(x==T)return f;
int used=0,tmp;
for(int i=cur[x];i;i=e[i].next){
int y=e[i].y;
int v=e[i].v;
if(v && h[y]==h[x]+1){
tmp=DFS(y,min(v,f-used));
e[i].v-=tmp;
e[i^1].v+=tmp;
used+=tmp;
if(e[i].v)cur[x]=i;
if(used==f)return f;
}
}
if(!used)h[x]=-1;
return used;
}
int ret=0;
int maxf(){
while(BFS()){
ret+=DFS(S,INF);
}
return ret;
}

int main(){
scanf("%d%d",&k,&n);
S=0;T=n+k+1;m=0;
for(int i=1;i<=k;i++){
int v;
scanf("%d",&v);
m+=v;
}
for(int i=1;i<=n;i++){
int p,y;
scanf("%d",&p);
for(int j=1;j<=p;j++){
scanf("%d",&y);
}
}
if(maxf()>=m)puts("YES");
else puts("No Solution!");
}

### T9 方格取数问题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<queue>
using namespace std;
const int maxn=510;
const int maxm=1000010;
const int INF=0x3f3f3f3f;

struct edge{
int x,y,next,v;
edge(){}
edge(int a,int b,int c,int d)
:x(a),y(b),next(c),v(d){}
} e[maxm];
}
int S,T,n,m;
int cur[maxn],h[maxn];
int ret=0;
queue<int>q;
bool BFS(){
memset(h,-1,sizeof(h));
h[S]=0;q.push(S);
while(!q.empty()){
int x=q.front();q.pop();
int y=e[i].y;
int v=e[i].v;
if(h[y]==-1 && v){
h[y]=h[x]+1;
q.push(y);
}
}
}
return h[T]!=-1;
}
int DFS(int x,int f){
if(x==T)return f;
int used=0,tmp;
for(int i=cur[x];i;i=e[i].next){
int y=e[i].y;
int v=e[i].v;
if(h[y]==h[x]+1 && v){
tmp=DFS(y,min(v,f-used));
e[i].v-=tmp;
e[i^1].v+=tmp;
used+=tmp;
if(e[i].v)cur[x]=i;
if(used==f)return f;
}
}
if(!used)h[x]=-1;
return used;
}
int maxf(){
while(BFS()){
ret+=DFS(S,INF);
}
return ret;
}

const int mx[]={0,1,-1,0};
const int my[]={1,0,0,-1};
int num(int x,int y){
return (x-1)*m+y;
}
int main(){

scanf("%d%d",&n,&m);
S=0;T=n*m+1;
int sum=0;

for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
int v;
scanf("%d",&v);
sum+=v;
int id=num(i,j);
if((i+j)%2==0){
for(int k=0;k<4;k++){
int nx=i+mx[k];
int ny=j+my[k];
if(nx >=1 && nx<=n && ny>=1 && ny<=m){
}
}
}
else
}
printf("%d\n",sum-maxf());
}

### T10 餐巾问题

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 2010;
const int maxm = 4000010;
const int INF = 0x3f3f3f3f;
struct edge{
int x,y,next,v,c;
edge(){}
edge(int _x,int _y,int _nt,int _v,int _c)
:x(_x),y(_y),next(_nt),v(_v),c(_c){}
}e[maxm];
bool inq[maxn];
int S,T,n,m,p,f,tt,ss;
void addedge(int x,int y,int v,int c){
}
queue<int>q;
bool SPFA(){
for(int i=S;i<=T;i++){
dis[i]=INF;
inq[i]=0;
}
q.push(S);inq[S]=1;
dis[S]=0;
while(!q.empty()){
int x=q.front();q.pop();
inq[x]=0;
// cout<<x<<":"<<endl;;
int y=e[i].y;
// cout<<y<<endl;
int v=e[i].v;
int c=e[i].c;
if(v && dis[x]+c<dis[y]){
dis[y]=dis[x]+c;
fro[y]=i;
if(!inq[y]){
inq[y]=1;
q.push(y);
}
}
}
}
return dis[T]!=INF;
}
int mcf(){
int ret=0;
while(SPFA()){
int tmp=INF;
for(int i=fro[T];i;i=fro[e[i].x]){
tmp=min(tmp,e[i].v);
}
ret+=tmp*dis[T];
for(int i=fro[T];i;i=fro[e[i].x]){
e[i].v-=tmp;
e[i^1].v+=tmp;
}
}
return ret;
}
int main(){
scanf("%d%d%d%d%d%d",&n,&p,&m,&f,&tt,&ss);

for(int i=1;i<=n;i++)scanf("%d",&a[i]);

S=0;T=n*2+4;
for(int i=1;i<=n;i++){
}
printf("%d\n",mcf());

}


### T11 航空路线问题

#include<bits/stdc++.h>
using namespace std;
int hash(char *str){
int ret=0,seed=131;
for(;*str;ret=ret*seed+*str++);
return ret & 0x7fffffff;
}
map<int,int>mp;
struct edge{
int x,y,next,v,c;
edge(){}
edge(int _x,int _y,int _nt,int _v,int _c):x(_x),y(_y),next(_nt),v(_v),c(_c){}
}e[20005];
bool inq[105];
queue<int>q;
char str[1000];
}
int S,T,n,m;
bool SPFA(){
for(int i=S;i<=T;i++)dis[i]=inq[i]=0;
q.push(S);inq[S]=1;
dis[S]=0;
while(!q.empty()){
int x=q.front();q.pop();
inq[x]=0;
int y=e[i].y;
int v=e[i].v;
int c=e[i].c;
if(v && dis[x]+c>dis[y]){
dis[y]=dis[x]+c;
fro[y]=i;
if(!inq[y]){
inq[y]=1;q.push(y);
}
}
}
}
return dis[T]!=0;
}
int mcf(){
int ret=0;
while(SPFA()){
int tmp=100;
for(int i=fro[T];i;i=fro[e[i].x])tmp=min(tmp,e[i].v);
ret+=tmp*dis[T];
for(int i=fro[T];i;i=fro[e[i].x]){
e[i].v-=tmp;
e[i^1].v+=tmp;
}
}
return ret;
}
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);

scanf("%d%d",&n,&m);
S=1;T=n*2;
for(int i=1;i<=n;i++){
scanf("%s",str);
mp[hash(str)]=i;
if(i==1 || i==n){
}else{
}

}
for(int x,y,i=1;i<=m;i++){
scanf("%s",str);x=mp[hash(str)];
scanf("%s",str);y=mp[hash(str)];
if(x==1 && y==n){
}
if(x==n && y==1){
}
if(x < y){
}else{
}
}
int ans=mcf()-2;
if(e[2].v==0 && e[n<<1].v==0)
printf("%d\n",ans);
else puts("No Solution!");

}

### T13 星际转移问题

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int INF  = 0x3f3f3f3f;
int f[maxn];
int find(int x){return x==f[x]?x:f[x]=find(f[x]);}

struct edge{
int x,y,next,v;
edge(){}
edge(int _x,int _y,int _nt,int _v)
:x(_x),y(_y),next(_nt),v(_v){}
}e[maxn<<1];
int maxf=0;
}
queue<int>q;
bool BFS(){
memset(h,-1,sizeof(h));
q.push(S);h[S]=0;
while(!q.empty()){
int x=q.front();q.pop();
y=e[i].y;v=e[i].v;
if(v && h[y]==-1){
h[y]=h[x]+1;
q.push(y);
}
}
}
return h[T]!=-1;
}
int DFS(int x,int f){
if(x==T)return f;
int used=0,tmp;
for(int y,v,i=cur[x];i;i=e[i].next){
y=e[i].y;v=e[i].v;
if(v && h[y]==h[x]+1){
tmp=DFS(y, min(v,f-used));
e[i].v-=tmp;
e[i^1].v+=tmp;
used+=tmp;
if(e[i].v)cur[x]=i;
if(used==f)return f;
}
}
if(!used)h[x]=-1;
return used;
}
void dinic(){
while(BFS()){
maxf+=DFS(S,INF);
}
}
struct ship{
int p,r;
int s[25];
}p[maxn];
int getnum(int N,int DAY){
return DAY*(n+2)+N;
}
scanf("%d%d%d",&n,&m,&k);

for(int i=1;i<=n+2;i++)f[i]=i;
S=0;T=20005;
for(int i=1;i<=m;i++){
scanf("%d%d",&p[i].p,&p[i].r);
int c;
for(int j=1;j<=p[i].r;j++){
scanf("%d",&c);

if(c== 0)c=n+1;
if(c==-1)c=n+2;
p[i].s[j]=c;
if(j>1){
int x=p[i].s[j-1];
int y=p[i].s[j];
x=find(x);y=find(y);
f[x]=y;
}
}
}
if(find(n+1)!=find(n+2)){puts("0");return 0;}

int DAY;maxf=0;
for(DAY=1;maxf<k;DAY++){
for(int i=1;i<=n+2;i++)
for(int i=1;i<=m;i++){
int x=p[i].s[ (DAY-1) % p[i].r +1 ];
int y=p[i].s[ ( DAY ) % p[i].r +1 ];
}
dinic();
}
printf("%d\n",DAY-1);
}

### T14 孤岛营救

dis[p,x]记录钥匙获得情况为p,当前到达x号点的最小时间，BFS转移即可

#include<bits/stdc++.h>
using namespace std;
const int maxn = 20;
const int INF = 0x3f3f3f3f;
int n,m,p,k,s;
const int mx[]={1,0,-1,0};
const int my[]={0,1,0,-1};
int g[255][255];
vector<int> key[20][20];
int dis[3200][255];
typedef pair<int,int>pii;
queue<pii>q;
int encode(int x,int y){
return (x-1)*m+y;
}
void decode(int d,int &x,int &y){
y=d%m;if(y==0)y=m;
x=(d-y)/m+1;
}
int main(){
scanf("%d%d%d",&n,&m,&p);
scanf("%d",&k);
memset(g,0,sizeof(g));
for(int i=1;i<=k;i++){
int x1,y1,x2,y2,G;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&G);
int p=encode(x1,y1),q=encode(x2,y2);
g[ p ][ q ]=g[ q ][ p ]=G?G:INF;
}
scanf("%d",&s);
memset(key,0,sizeof(key));
for(int i=1;i<=s;i++){
int x,y,Q;
scanf("%d%d%d",&x,&y,&Q);
key[x][y].push_back(Q);
}
memset(dis,-1,sizeof(dis));

for(int i=1;i<=n*m;i++)
for(int j=1;j<=n*m;j++){
if(i==j)continue;
int x1,y1,x2,y2;
if(g[i][j]==0)continue;
decode(i,x1,y1);decode(j,x2,y2);
}

int tmp=1;
for(int i=0;i<key[1][1].size();i++)tmp|=1<<key[1][1][i];
dis[ tmp ][ encode(1,1) ]=0;
q.push(pii(tmp,encode(1,1)));

while(!q.empty()){
pii u=q.front();q.pop();
int x=u.second,p=u.first;
int tx,ty;
decode(x,tx,ty);

if(tx==n && ty==m){
cout<<dis[p][x]<<endl;
return 0;
}
for(int i=0;i<4;i++){
int nx=tx+mx[i];
int ny=ty+my[i];
if(nx<1 || nx>n || ny<1 || ny>m)continue;

int y=encode(nx,ny);

if(g[x][y]==INF)continue;

if(g[x][y]==0 || (p & (1<<g[x][y]))){
int np=p;
for(int j=0;j<key[nx][ny].size();j++)
np|=(1<<key[nx][ny][j]);
if(dis[np][y]==-1){
dis[np][y]=dis[p][x]+1;
q.push(pii(np,y));
}
}
}
}
puts("-1");
}

### T15 汽车加油行驶

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int INF= 0x3f3f3f3f;

int N,K,A,B,C;
int g[105][105],cnt=0;
int num(int x,int y,int h){
return h*N*N + (x-1) * N +y;
}
struct edge{
int x,y,next,c;
edge(){}
edge(int _x,int _y,int _nt,int _c):x(_x),y(_y),next(_nt),c(_c){}
}e[maxn<<2];
}
const int mx[]={1,0,-1,0};
const int my[]={0,1,0,-1};
typedef pair<int,int>pii;
struct heap{
pii data[maxn<<2];
int siz;
public:
heap(){siz=0;}
void push(pii x);
void pop();
pii top(){return data[1];}
bool empty(){return siz==0;}
};
heap q;
void Dijkstra(){
int S=num(1,1,K);
int T=num(N,N,K)+1;
for(int i=1;i<=T;i++)dis[i]=i==S?0:INF;
q.push(pii(dis[S],S));
while(!q.empty()){
pii tmp=q.top();q.pop();
int x=tmp.second;
if(tmp.first > dis[x])continue;
y=e[i].y;c=e[i].c;
if(dis[y] > dis[x]+c){
dis[y]=dis[x]+c;
q.push(pii(dis[y],y));
}
}
}
}
int main(){
// freopen("trav.in","r",stdin);
// freopen("trav2.out","w",stdout);

scanf("%d%d%d%d%d",&N,&K,&A,&B,&C);
for(int i=1;i<=N;i++)for(int j=1;j<=N;j++){
scanf("%d",&g[i][j]);
}

for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
for(int p=0;p<=K;p++){
int c=g[i][j]?A:A+C;
if( (!g[i][j] && p>0) || p==K){
for(int q=0;q<4;q++){
int x=i+mx[q];
int y=j+my[q];
c=q<2 ? 0 : B;
if(x>=1 && x<=N && y>=1 && y<=N)
}
}
}
Dijkstra();
int ans=INF;
for(int i=0;i<=K;i++)ans=min(ans,dis[num(N,N,i)]);
printf("%d\n",ans);
}

void heap::push(pii x){
data[++siz]=x;int now=siz;
while(now>>1){
if(data[now].first<data[now>>1].first){
swap(data[now],data[now>>1]);
now>>=1;
}else break;
}
}
void heap::pop(){
data[1]=data[siz--];int now=1,next;
while(now<<1 < siz){
if(data[now<<1].first<data[now<<1|1].first)
next=now<<1;else next=now<<1|1;
if(data[now].first>data[next].first){
swap(data[now],data[next]);now=next;
}else break;
}
}

### T16 数字梯形问题

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 50;
const int INF = 0x3f3f3f3f;
int n,m,cnt;
int a[maxn][maxn],num[maxn][maxn];
struct edge{
int x,y,next,v,c;
edge(){}
edge(int _x,int _y,int _nt,int _v,int _c):x(_x),y(_y),next(_nt),v(_v),c(_c){}
}e[maxn<<6];
LL dis[maxn*maxn*2];
bool inq[maxn*maxn*2];
queue<int>q;
inline void addedge(int x,int y,int v,int c){
// cout<<tot<<endl;
}
bool SPFA(){
for(int i=S;i<=T;i++)dis[i]=-INF;
dis[S]=0;
q.push(S);inq[S]=1;
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
y=e[i].y;v=e[i].v;c=e[i].c;
if(v && dis[x]+c > dis[y]){
dis[y]=dis[x]+c;
fro[y]=i;
if(!inq[y]){
q.push(y);inq[y]=1;
}
}
}
}
return dis[T]!=-INF;
}
LL mcf(){
LL ret=0;
while(SPFA()){
int tmp=INF;
for(int i=fro[T];i;i=fro[e[i].x])tmp=min(tmp,e[i].v);
ret+=tmp*dis[T];
for(int i=fro[T];i;i=fro[e[i].x])e[i].v-=tmp,e[i^1].v+=tmp;
}
return ret;
}
int main(){
scanf("%d%d",&m,&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=m+i-1;j++)
scanf("%d",&a[i][j]);
cnt=0;
for(int i=1;i<=n;i++)for(int j=1;j<=m+i-1;j++)num[i][j]=++cnt;
S=0;T=cnt*2+1;
/*PROBLEM 1*/
for(int i=1;i<n;i++)for(int j=1;j<=m+i-1;j++){
}
// for(int i=2;i<=tot;i+=2)cout<<e[i].x<<" "<<e[i].y<<" "<<e[i].v<<" "<<e[i].c<<endl;
printf("%lld\n",mcf());

/*PROBLEM 2*/
for(int i=1;i<n;i++)for(int j=1;j<=m+i-1;j++){
}
printf("%lld\n",mcf());
/*PROBLEM 3*/
for(int i=1;i<=m+n-1;i++){
}
for(int i=1;i<n;i++)for(int j=1;j<=m+i-1;j++){
}
printf("%lld\n",mcf());
}
/*
2 5
2 3
3 4 5
9 10 9 1
1 1 10 1 1
1 1 10 12 1 1

66
75
77
*/

### T17 运输问题

#include<bits/stdc++.h>
using namespace std;
const int maxn = 205;
const int maxm = 50005;
const int INF  = 0x3f3f3f3f;
struct edge{
int x,y,next,v,c;
edge(){}
edge(int _x,int _y,int _nt,int _v,int _c)
:x(_x),y(_y),next(_nt),v(_v),c(_c){}
}e[maxm<<1];
int fro[maxn],dis[maxn];bool inq[maxn];
void addedge(int x,int y,int v,int c){
}
int n,m,S,T;
int a[maxn],b[maxn],g[105][105];
bool SPFA(int fg);
int mcf(int fg);
void build(){
for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)

// for(int i=2;i<=tot;i+=2)
// cout<<e[i].x<<" "<<e[i].y<<" "<<e[i].v<<" "<<e[i].c<<endl;
}
int main(){
scanf("%d%d",&m,&n);
S=0;T=n+m+1;
for(int i=1;i<=m;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)scanf("%d",&g[i][j]);

build();
printf("%d\n",mcf(1));
build();
printf("%d\n",mcf(-1));
}

queue<int>q;
bool SPFA(int fg){
memset(inq,0,sizeof(inq));
for(int i=S;i<=T;i++)dis[i]=INF*fg;
dis[S]=0;q.push(S);inq[S]=1;
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
y=e[i].y;v=e[i].v;c=e[i].c;
if(v && dis[y]*fg > (dis[x]+c)*fg){
dis[y]=dis[x]+c;
fro[y]=i;
if(!inq[y]){
inq[y]=1;
q.push(y);
}
}
}
}
return dis[T]!=INF*fg;
}
int mcf(int fg){
int ret=0;
memset(fro,0,sizeof(fro));
while(SPFA(fg)){
int tmp=INF;
for(int i=fro[T];i;i=fro[e[i].x])tmp=min(tmp,e[i].v);
ret+=tmp*dis[T];
for(int i=fro[T];i;i=fro[e[i].x])e[i].v-=tmp,e[i^1].v+=tmp;
}
return ret;
}

### T18 分配问题

#include<bits/stdc++.h>
using namespace std;
const int maxn = 205;
const int maxm = 50005;
const int INF  = 0x3f3f3f3f;
struct edge{
int x,y,next,v,c;
edge(){}
edge(int _x,int _y,int _nt,int _v,int _c)
:x(_x),y(_y),next(_nt),v(_v),c(_c){}
}e[maxm];
int g[105][105];
bool inq[maxn];
inline void addedge(int x,int y,int v,int c){
}
bool SPFA(int fg);
int mcf(int fg);
void build(){
S=0;T=n*2+1;
for(int i=1;i<=n;i++){
}
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)scanf("%d",&g[i][j]);

build();
printf("%d\n",mcf(1));
build();
printf("%d\n",mcf(-1));
}
queue<int>q;
bool SPFA(int fg){
for(int i=S;i<=T;i++)dis[i]=INF*fg;
dis[S]=0;inq[S]=1;q.push(S);
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
y=e[i].y;v=e[i].v;c=e[i].c;
if(v && dis[y]*fg > (dis[x]+c)*fg ){
dis[y]=dis[x]+c;
fro[y]=i;
if(!inq[y]){
inq[y]=1;q.push(y);
}
}
}
}
return dis[T]!=INF*fg;
}
int mcf(int fg){
int ret=0;
memset(fro,0,sizeof(fro));
while(SPFA(fg)){
int tmp=INF;
for(int i=fro[T];i;i=fro[e[i].x])tmp=min(tmp,e[i].v);
ret+=tmp*dis[T];
for(int i=fro[T];i;i=fro[e[i].x])e[i].v-=tmp,e[i^1].v+=tmp;
}
return ret;
}

### T19 负载平衡问题

#include<bits/stdc++.h>
using namespace std;
const int maxn = 500;
const int INF  = 0x3f3f3f3f;
struct edge{
int x,y,next,v,c;
edge(){}
edge(int _x,int _y,int _nt,int _v,int _c):x(_x),y(_y),next(_nt),v(_v),c(_c){}
}e[ 2400 ];
int dis[maxn],fro[maxn],a[maxn];
bool inq[maxn];
queue<int>q;
int S,T,n;
inline void addedge(int x,int y,int v,int c){
}
bool SPFA(){
for(int i=S;i<=T;i++)dis[i]=INF,inq[i]=0;
q.push(S);inq[S]=1;
dis[S]=0;
while(!q.empty()){
int x=q.front();q.pop();
inq[x]=0;
y=e[i].y;v=e[i].v;c=e[i].c;
if(v && dis[x]+c<dis[y]){
dis[y]=dis[x]+c;fro[y]=i;
if(!inq[y]){
inq[y]=1;q.push(y);
}
}
}
}
return dis[T]!=INF;
}
int mcf(){
int ret=0;
while(SPFA()){
int tmp=INF;
for(int i=fro[T];i;i=fro[e[i].x])tmp=min(tmp,e[i].v);
ret+=tmp*dis[T];
for(int i=fro[T];i;i=fro[e[i].x])e[i].v-=tmp,e[i^1].v+=tmp;
}
return ret;
}

int main(){
scanf("%d",&n);
int sum=0;
for(int i=1;i<=n;i++){scanf("%d",&a[i]);sum+=a[i];}
sum/=n;
for(int i=1;i<=n;i++)a[i]=a[i]-sum;
S=0;T=n*2+1;
for(int i=1;i<=n;i++){
if(a[i] > 0)
else
int t;
t=i>1?i-1:n;
t=i<n?i+1:1;
}
cout<<mcf()<<endl;
}

### T20 深海机器人问题

#include<bits/stdc++.h>
using namespace std;
const int maxn = 500;
const int maxm = 50005;
const int INF  = 0x3f3f3f3f;
struct edge{
int x,y,next,v,c;
edge(){}
edge(int _x,int _y,int _nt,int _v,int _c)
:x(_x),y(_y),next(_nt),v(_v),c(_c){}
}e[maxm];
int num[25][25];
bool inq[maxn];
void addedge(int x,int y,int v,int c){
}
queue<int>q;
bool SPFA(){
for(int i=S;i<=T;i++)dis[i]=-INF;
q.push(S);dis[S]=0;inq[S]=1;
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
y=e[i].y,v=e[i].v,c=e[i].c;
if(v && dis[y] < dis[x]+c){
dis[y]=dis[x]+c;
fro[y]=i;
if(!inq[y]){
inq[y]=1;q.push(y);
}
}
}
}
return dis[T]!=-INF;
}
int mcf(){
int ret=0;
while(SPFA()){
int tmp=INF;
for(int i=fro[T];i;i=fro[e[i].x])tmp=min(tmp,e[i].v);
ret+=dis[T]*tmp;
for(int i=fro[T];i;i=fro[e[i].x])e[i].v-=tmp,e[i^1].v+=tmp;
}
return ret;
}
int main(){
scanf("%d%d",&a,&b);
scanf("%d%d",&P,&Q);P++;Q++;
T=0;
for(int i=1;i<=P;i++)
for(int j=1;j<=Q;j++)
num[i][j]=++T;
S=0;T++;

for(int i=1;i<=P;i++)
for(int j=1;j<=Q-1;j++){
int c;
scanf("%d",&c);
int x=num[i][j],y=num[i][j+1];
}
for(int i=1;i<=Q;i++)
for(int j=1;j<=P-1;j++){
int c;
scanf("%d",&c);
int x=num[j][i],y=num[j+1][i];
}
for(int i=1;i<=a;i++){
int k,x,y;
scanf("%d%d%d",&k,&x,&y);x++;y++;
}
for(int i=1;i<=b;i++){
int k,x,y;
scanf("%d%d%d",&k,&x,&y);x++;y++;
}
printf("%d\n",mcf());
}

### T21 最长K可重区间集问题

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int maxm = 100005;
const int INF  = 0x3f3f3f3f;
struct edge{
int x,y,next,v,c;
edge(){}
edge(int _x,int _y,int _nt,int _v,int _c)
:x(_x),y(_y),next(_nt),v(_v),c(_c){}
}e[maxm];
bool inq[maxn];
struct node{
int k,p;
}a[maxn];
int b[maxn<<1],L[maxn];
bool cmp(node x,node y){return x.k<y.k;}
inline int addedge(int x,int y,int v,int c){
}
queue<int>q;
bool SPFA(){
for(int i=S;i<=T;i++)dis[i]=-INF;
dis[S]=0;inq[S]=1;q.push(S);
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
y=e[i].y;v=e[i].v;c=e[i].c;
if( v && dis[y] < dis[x]+c){
dis[y]=dis[x]+c;
fro[y]=i;
if(!inq[y]){
inq[y]=1;q.push(y);
}
}
}
}
return dis[T]!=-INF;
}
int mcf(){
int ret=0;
while(SPFA()){
int tmp=INF;
for(int i=fro[T];i;i=fro[e[i].x])tmp=min(tmp,e[i].v);
ret+=tmp*dis[T];
for(int i=fro[T];i;i=fro[e[i].x])e[i].v-=tmp,e[i^1].v+=tmp;
}
return ret;
}
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d%d",&b[i],&b[i+n]);L[i]=b[i+n]-b[i];
a[i].k=b[i];
a[i+n].k=b[i+n];
a[i].p=i;
a[i+n].p=i+n;
}
sort(a+1,a+1+n+n,cmp);
int cnt=0;
b[a[1].p]=++T;
for(int i=2;i<=n<<1;i++)
b[a[i].p]=a[i].k==a[i-1].k?T:++T;
T++;
S=0;
printf("%d\n",mcf());
}

### T24 骑士共存问题

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
const int maxn = 205;
const int INF  = 0x3f3f3f3f;
int g[maxn][maxn];
int n,m,S=0,T=0;
const int mx[]={1,2,2,1,-1,-2,-2,-1};
const int my[]={-2,-1,1,2,2,1,-1,-2};
struct edge{
int x,y,next,v;
edge(){}
edge(int _x,int _y,int _nt,int _v)
:x(_x),y(_y),next(_nt),v(_v){}
}e[maxn*maxn*16];
inline void addedge(int x,int y,int v){
}
int q[maxn*maxn],l,r;
bool BFS(){
memset(h,-1,sizeof(h));
h[S]=0;
q[l=r=0]=S;
while(l<=r){
int x=q[l++];
if(x==T)return 1;
y=e[i].y,v=e[i].v;
if(v && h[y]==-1){
h[y]=h[x]+1;
if(y==T)return 1;
q[++r]=y;
}
}
}
return 0;
}
int DFS(int x,int f){
if(x==T)return f;
int used=0,tmp;
for(int v,y,i=cur[x];i;i=e[i].next){
y=e[i].y;v=e[i].v;
if(v && h[y]==h[x]+1){
tmp=DFS(y,min(v,f-used));
e[i].v-=tmp;e[i^1].v+=tmp;
used+=tmp;
if(e[i].v)cur[x]=i;
if(f==used)return f;
}
}
if(!used)h[x]=-1;
return used;
}
int maxf(){
int ret=0;
while(BFS()){
ret+=DFS(S,INF);
}
return ret;
}
int main(){
scanf("%d%d",&n,&m);
memset(g,0,sizeof(g));
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
g[x][y]=-1;
}

for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
if(g[i][j] == 0)g[i][j]=++T;
S=0;T=n*n+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(g[i][j]==-1)continue;
if((i+j) % 2 == 0){
for(int k=0;k<8;k++){
int x=i+mx[k],y=j+my[k];
if(x<1 || x>n || y<1 || y>n)continue;
if(g[x][y]==-1)continue;
}
}else{
}
}
printf("%d\n",n*n-m-maxf());
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120