复习下费用流
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(int i=l;i<=r;i++) 3 #define dec(i,l,r) for(int i=l;i>=r;i--) 4 #define link(x) for(edge *j=h[x];j;j=j->next) 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define inf 1e9 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define NM 500 10 #define nm 100000 11 using namespace std; 12 int read(){ 13 int x=0,f=1;char ch=getchar(); 14 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 15 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 16 return x*f; 17 } 18 struct edge{ 19 int t,v,w; 20 edge *next,*rev; 21 }e[nm],*h[NM],*p[NM],*o=e; 22 void _add(int x,int y,int w,int v){ 23 o->w=w;o->v=v;o->t=y;o->next=h[x];h[x]=o;o++; 24 } 25 void add(int x,int y,int w,int v){ 26 _add(x,y,w,v);_add(y,x,0,-v); 27 h[x]->rev=h[y];h[y]->rev=h[x]; 28 } 29 int n,m,s,ans,d[NM],w[NM],_x,_y,_t; 30 bool v[NM]; 31 queue<int >q; 32 int spfa(){ 33 mem(d);mem(v);mem(w);mem(p); 34 inc(i,1,n*2)d[i]=inf; 35 d[1+n]=0;v[1+n]++;w[1+n]=inf;q.push(1+n); 36 while(!q.empty()){ 37 int t=q.front();q.pop();v[t]=false; 38 link(t) 39 if(j->w&&d[j->t]>d[t]+j->v){ 40 d[j->t]=d[t]+j->v; 41 w[j->t]=min(w[t],j->w); 42 p[j->t]=j; 43 if(!v[j->t])v[j->t]++,q.push(j->t); 44 } 45 } 46 return w[n]; 47 } 48 int main(){ 49 // freopen("data.in","r",stdin); 50 n=read();m=read(); 51 inc(i,1,n-1)add(i,i+n,1,0); 52 inc(i,1,m){ 53 _x=read();_y=read();_t=read(); 54 add(_x+n,_y,1,_t); 55 } 56 while(spfa()){ 57 ans+=w[n];s+=w[n]*d[n]; 58 for(edge *j=p[n];j;j=p[j->rev->t]) 59 j->w-=w[n],j->rev->w+=w[n]; 60 } 61 printf("%d %d ",ans,s); 62 return 0; 63 }