题解,求关注
2024-07-01 10:04:03
发布于:浙江
1阅读
0回复
0点赞
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
const int maxn=10000+10;
const int INF=1000000000;
int n,m,s,t,st[maxn],ed[maxn],ans,cur[maxn],vis[maxn],dis[maxn];
struct edge
{
int from,to,cap,flow;
};
vector<int> g[maxn];
vector<edge> edges;
void addedge(int from,int to,int cap)
{
edges.push_back((edge){from,to,cap,0});
edges.push_back((edge){to,from,0,0});
int m1=edges.size();
g[from].push_back(m1-2);
g[to].push_back(m1-1);
}
bool bfs()
{
memset(vis,0,sizeof(vis));
queue<int> Q;Q.push(s);vis[s]=1;dis[s]=0;
while(!Q.empty())
{
int x=Q.front();Q.pop();
for(int i=0;i<g[x].size();i++)
{
edge e=edges[g[x][i]];
if(e.cap>e.flow&&!vis[e.to])
{
vis[e.to]=1;
dis[e.to]=dis[x]+1;
Q.push(e.to);
}
}
}
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(dis[e.to]==dis[x]+1&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
flow+=f;
e.flow+=f;
edges[g[x][i]^1].flow-=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int maxflow()
{
int flow=0;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
int main()
{
//freopen("3511.in","r",stdin);
//freopen("3511.out","w",stdout);
scanf("%d%d",&n,&m);s=0,t=n+1;st[1]=INF;ed[n]=INF;
for(int i=2;i<=n-1;i++)
{
int x;scanf("%d",&x);
st[i]+=2*x;
ans+=2*x;
}
for(int i=2;i<=n-1;i++)
{
int x;scanf("%d",&x);
ed[i]+=2*x;ans+=2*x;
}
for(int i=1;i<=m;i++)
{
int x,y,ea,eb,ec;
scanf("%d%d%d%d%d",&x,&y,&ea,&eb,&ec);
ans+=(ea+eb)*2;st[x]+=ea;st[y]+=ea;ed[x]+=eb;ed[y]+=eb;
addedge(x,y,ea+eb+2*ec);addedge(y,x,ea+eb+2*ec);
}
for(int i=1;i<=n;i++)
{
addedge(s,i,st[i]);
addedge(i,t,ed[i]);
}
printf("%d\n",(ans-maxflow())/2);
return 0;
}
这里空空如也
有帮助,赞一个