题目
标准的网络流裸题
只需要跑一遍网络流的模板就好了
不知道网络流是什么的请看这道题
附上代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=10010,MAXM=100010;
int N,M,S,T;
int to[MAXM<<1],edge[MAXM<<1],nxt[MAXM<<1],head[MAXN],tot=1;
void addedge(int u,int v,int c){
to[++tot]=v;
edge[tot]=c;
nxt[tot]=head[u];
head[u]=tot;
}
int d[MAXN];
map<string,int> m;\\将字母变成数字
int tot2;
queue<int> q;
bool bfs(){
memset(d,0,sizeof(d));
while(!q.empty()) q.pop();
q.push(S);
d[S]=1;
while(!q.empty()){
int t=q.front();
q.pop();
for(int i=head[t];i;i=nxt[i]){
int v=to[i];
if(edge[i] && !d[v]) {
d[v]=d[t]+1;
q.push(v);
if(v==T) return true;
}
}
}
return false;
}
int dinic(int now,int flow){
if(now==T) return flow;
int rest=flow;
for(int i=head[now];i;i=nxt[i]){
int v=to[i];
if(edge[i] && d[v]==d[now]+1) {
int k=dinic(v,min(rest,edge[i]));
rest-=k;
edge[i]-=k;
edge[i^1]+=k;
if(k==0) d[v]=0;
}
}
return flow-rest;
}
int main(){
scanf("%d%d",&N,&M);
string t1,t2;
int t3;
while(cin>>t1>>t2>>t3){
// scanf("%d%d%d",&t1,&t2,&t3);
if(m.count(t1)==0) m[t1]=++tot2;
if(m.count(t2)==0) m[t2]=++tot2;
addedge(m[t1],m[t2],t3);
addedge(m[t2],m[t1],t3);
}
if(m.count("A")==0 || m.count("Z")==0){
printf("%d\n",0);
system("pause");
return 0;
}
S=m["A"],T=m["Z"];
int maxflow=0;
while(bfs()){
int flow;
while(flow=dinic(S,0x3f3f3f3f)) maxflow+=flow;
}
printf("%d\n",maxflow);
// system("pause");
return 0;
}